Module erlpipe

Authors: Serge Aleynikov (saleyn(at)gmail(dot)com).


Erlang pipeline parse transform

This transform implements a parser syntax extension that enables application of cascading function calls using the / operator.

In the LHS / RHS / ... Last. notation, the result of evaluation of the LHS expression is passed as an argument to the RHS expression. This process continues until the Last expression is evaluated. The head element of the pipeline must be either a term to which the arithmetic division /` operator cannot apply (i.e. not integers, floats, functions), or if you need to pass integer(s) or float(s), wrap them in a list brackets. This transfor is inspired by the similar functionality in Linux (i.e. `| pipe) and Elixir (|> pipe).

When using this as a parse transform, include the {parse_transform,erlpipe} compiler option.

The following examples illustrate the work of the transform, in which:
   test1(A)   -> [A]   / fun1 / mod:fun2 / fun3.
   test2(A,B) -> [A,B] / fun4 / fun5() / io:format("~p\n", [_]).
will be transformed to:
   test1(A)   -> fun3(mod:fun2(fun1(A))).
   test2(A,B) -> io:format("~p\n", [fun5(fun4(A,B))]).
Similarly to Elixir, a special tap/2 function is implemented, which passes the given argument to an anonymous function, returning the argument itself. The following:
   f(A) -> A+1.
   test_tap() ->
     [10] / tap(f)
          / tap(fun f/1)
          / tap(fun(I) -> I+1 end).
is equivalent to:
   test_tap() ->
           (fun(I) -> I end)(10)
For debugging the AST of the resulting transform, pass the following options to the erlc compiler:
  • -Derlpipe_orig - print the original AST before the transform
  • -Derlpipe_ast - print the transformed AST
  • -Derlpipe_src - print the resulting source code after the transform
  • Function Index

    parse_transform/2parse_transform entry point.

    Function Details


    parse_transform(AST, Options) -> any()

    parse_transform entry point