Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Function chaining in Erlang

branch: master

expm

latest commit e6eede57b7
Saša Jurić authored April 17, 2013
Octocat-spinner-32 src error handling January 09, 2013
Octocat-spinner-32 test testing of compilation errors January 09, 2013
Octocat-spinner-32 .gitignore basic functionality + unit test December 21, 2012
Octocat-spinner-32 LICENSE license April 09, 2013
Octocat-spinner-32 README.md fixed typo December 29, 2012
Octocat-spinner-32 package.exs expm April 17, 2013
Octocat-spinner-32 rebar.config dynamic generation of tests + code coverage January 08, 2013
README.md

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.

Something went wrong with that request. Please try again.