Skip to content

sasa1977/fun_chain

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Introduction

This parse transform simplifies function chaining in Erlang.

Normally in Erlang when one needs to chain dict operations, one can write it as:

D = dict:new(),
D1 = dict:store(a, 1, D),
D2 = dict:erase(a, D1)

or as:

dict:erase(a, dict:store(a, 1, dict:new()))

The fun_chain parse transforms enables simpler syntax:

fun_chain:last(dict:new(),
  dict:store(a, 1),
  dict:erase(a)
)

The first argument of a fun_chain:last is an expression. All other arguments are function calls. The result of a previous call is automatically included as the last argument in the next call. This eliminates the need for temp variables or staircasing.

The dict here serves only as an example. The fun_chain:last can be used for any set of function calls where the return value of the last call is fed as the last argument to the next call.

Similarly, the fun_chain:first is provided:

inc(X, Increment) -> X + Increment.

9 = fun_chain:first(1,
  inc(3),
  inc(5)
)

Usage

Erlang R15 or higher required. Compile with rebar:

rebar compile

or manually:

mkdir -p ebin; erlc -o ebin/ src/*.erl

Once beams are compiled, make sure they are in the load path for erlc. Add following to your .erl files:

-compile({parse_transform, fun_chain}).

See tests for an example.

About

Function chaining in Erlang

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published