[erlang-questions] Pipe, maybe, and other silly parse transforms

Fred Hebert <>
Sun Apr 2 00:38:28 CEST 2017


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.


More information about the erlang-questions mailing list