Pipe, maybe, and other silly parse transforms

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Pipe, maybe, and other silly parse transforms

Fred Hebert-2
Hi everyone!

On this April 1st, I decided to do a few foolish things, and implement
an Erlang equivalent to Elixir's Pipe along with a 'maybe' construct.

The first ideas for this I had are almost 2 years old now and are
archived at
http://erlang.org/pipermail/erlang-questions/2015-July/085109.html

Well since today's a silly day, I spent a bit of time and made an actual
version of it that can be toyed with, and put it in a library called
'fancyflow' (https://github.com/ferd/fancyflow)

You can now do fancy control flow refactorings to move from code like:

    String = "a b c d e f",
    string:join(
      lists:map(fun string:to_upper/1, string:tokens(String, " ")),
      ","
    ).

into:

    [pipe]("a b c d e f",
           string:tokens(_, " "),
           lists:map(fun string:to_upper/1, _),
           string:join(_, ",")).

and change conditionals like:

    case file:get_cwd() of
        {ok, Dir} ->
            case file:read_file(filename:join([Dir, "demo", "data.txt"])) of
                {ok, Bin} ->
                    {ok, {byte_size(Bin), Bin}};
                {error, Reason} ->
                    {error, Reason}
            end;
        {error, Reason} ->
            {error, Reason}
    end.

into:

    [maybe](undefined,
            file:get_cwd(),
            file:read_file(filename:join([_, "demo", "data.txt"])),
            {ok, {byte_size(_), _}}).

More details in the README.

I can't actually advise or recommend using the library, but I felt like
sharing it anyway :)

Regards,
Fred.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Pipe, maybe, and other silly parse transforms

Andreas Schultz
Hi Fred,

----- On Apr 2, 2017, at 12:38 AM, Fred Hebert [hidden email] wrote:

> Hi everyone!
>
> On this April 1st, I decided to do a few foolish things, and implement
> an Erlang equivalent to Elixir's Pipe along with a 'maybe' construct.

Out of curiosity, how does this differ from erlando's
(https://github.com/rabbitmq/erlando) `state` monad ?

The syntax does seem to a bit more pleasing, but as far as I can tell the
result is about the same.

Thanks,
Andreas

> The first ideas for this I had are almost 2 years old now and are
> archived at
> http://erlang.org/pipermail/erlang-questions/2015-July/085109.html
>
> Well since today's a silly day, I spent a bit of time and made an actual
> version of it that can be toyed with, and put it in a library called
> 'fancyflow' (https://github.com/ferd/fancyflow)
>
> You can now do fancy control flow refactorings to move from code like:
>
>    String = "a b c d e f",
>    string:join(
>      lists:map(fun string:to_upper/1, string:tokens(String, " ")),
>      ","
>    ).
>
> into:
>
>    [pipe]("a b c d e f",
>           string:tokens(_, " "),
>           lists:map(fun string:to_upper/1, _),
>           string:join(_, ",")).
>
> and change conditionals like:
>
>    case file:get_cwd() of
>        {ok, Dir} ->
>            case file:read_file(filename:join([Dir, "demo", "data.txt"])) of
>                {ok, Bin} ->
>                    {ok, {byte_size(Bin), Bin}};
>                {error, Reason} ->
>                    {error, Reason}
>            end;
>        {error, Reason} ->
>            {error, Reason}
>    end.
>
> into:
>
>    [maybe](undefined,
>            file:get_cwd(),
>            file:read_file(filename:join([_, "demo", "data.txt"])),
>            {ok, {byte_size(_), _}}).
>
> More details in the README.
>
> I can't actually advise or recommend using the library, but I felt like
> sharing it anyway :)
>
> Regards,
> Fred.
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Pipe, maybe, and other silly parse transforms

Fred Hebert-2
On 04/02, Andreas Schultz wrote:

>Hi Fred,
>
>Out of curiosity, how does this differ from erlando's
>(https://github.com/rabbitmq/erlando) `state` monad ?
>
>The syntax does seem to a bit more pleasing, but as far as I can tell the
>result is about the same.
>
>Thanks,
>Andreas
>

There's a few differences:

- by tripping onto [term](...) as accepted syntax for an obviously
  invalid function call, we can *add* semantics to an expression that
  had none rather than overloading those of an exisiting construct
- by virtue of not being a monad fancyflow is also far less generic and
  doesn't allow overloading of the >>= nor return operators, which gives
  Erlando a lot of flexibility that fancyflow does not have
- fancyflow does not use monad terminology, and hence should be immune
  to burrito-related tutorials
- we avoid all of that pesky imperative programming by banishing the
  word 'do' and instead using the descriptive words 'pipe' and 'maybe',
  which totally express what is going to happen
- the monadic approach can be composed and nested, fancyflow isn't that
  fancy; this probably brings it to the same place as 'fancy feast' cat
  food, which I am told isn't that fancy of a feast*
- the 'cut' syntax is bound in context a bit more, which lets me work
  around this Erlando issue I had opened in 2011
  https://github.com/rabbitmq/erlando/issues/2 (props to them for never
  closing it in anger)
- Erlando was possibly intended to actually be used by its authors,
  whereas fancyflow was just me trying to toy with that idea I had
  posted in the mailing list a few years ago

Again, I can't really advise using either of these libraries in a code
base. One of the big strengths of Erlang is its simplicity and ease of
jumping into code, any code, and easily understand what it does. The use
of parse transforms to overload or add new semantic meaning to existing
constructs is a thing that should be done with care.

There was certainly not that much care involved in fancyflow's case,
since the RFC process involved me writing on a mailing list and then
saying "screw this it's april 1st let's try it" about 2 years after.

Regards,
Fred.

* as a general rule, if a thing needs to assert what it is in the name,
  it likely does not bear that property. To wit:

  - 'fancy feast' does not make for fancy feasts, as mentioned earlier
  - the S in SOAP stands for 'simple' which by now should have us all
    laughing; see also a bunch of amazon services
  - restaurants with 'gourmet' in their names (or brightly flashing neon
    signs) tend not to inspire much confidence
  - calling yourself a classy person likely means you are not that
    classy to begin with, though if you refer to marxist ideals then it
    may be an easier point to debate
  - I guess if you have an object oriented language you could also
    describe it as classy and it would be technically correct
  - flame decals and/or stripes on a car do not make the car go faster,
    no matter what my former 4 years old self would like to assert.  
    Dodge vipers ain't that cool, 4 years old Fred! You'll drive a
    humble grey versa for at least 10 years anyway when you're older so
    don't pretend to be that hot
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Pipe, maybe, and other silly parse transforms

Richard A. O'Keefe-2

> On 3/04/2017, at 12:23 AM, Fred Hebert <[hidden email]> wrote:
> - the 'cut' syntax is bound in context a bit more, which lets me work  around this Erlando issue I had opened in 2011  https://github.com/rabbitmq/erlando/issues/2 (props to them for never  closing it in anger)

I have a resurrection of Pop-2 that I've been working on in my spare time.
(Nostalgia isn't what it used to be.  Back then it was lime-flavoured.)
Think of a cross between Lisp data, Algol syntax, and a Forth/Postscript-
like visible stack designed in the 1960s.

As part of spiffing that up, I've been looking through the SRFIs for ideas
to steal.  I can across 'cut' and 'cute' and thought, hmmm.
It turns out that there is one lambda-expression every 11 SLOC
Only about 5% of those could use 'cut' or 'cute'.

I came to the conclusion that it is MUCH better not to have 'cut'/'cute'.


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Loading...