Module mapreduce

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

Description

Erlang map-reduce parse transform

This transform introduces two modifications of the list comprehension syntax that allow to perform a fold and mapfold on a list.

Fold Comprehension

To invoke the fold comprehension transform include the initial state assignment into a comprehension that returns a non-tuple expression:
   [S+I || S = 1, I <- L].
    ^^^    ^^^^^
In this example the S variable gets assigned the initial state 1, and the S+I expression represents the body of the fold function that is passed the iteration variable I and the state variable S:
   lists:foldl(fun(I, S) -> S+I end, 1, L).

MapFold Comprehension

To invoke the mapfold comprehension transform include the initial state assignment into a comprehension, and return a tuple expression:
   [{I, S+I} || S = 1, I <- L].
    ^^^^^^^^    ^^^^^
In this example the S variable gets assigned the initial state 1, and the {I, S+I} two-elements tuple expression represents the body of the fold function that is passed the iteration variable I and the state variable S:
   lists:mapfoldl(fun(I, S) -> S+I end, 1, L).

Compilation

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

For debugging the AST of the resulting transform, pass the following options to the erlc compiler:
  • -Dmapreduce_orig - print the original AST before the transform
  • -Dmapreduce_ast - print the transformed AST
  • -Dmapreduce_src - print the resulting source code after the transform
  • Function Index

    parse_transform/2parse_transform entry point.

    Function Details

    parse_transform/2

    parse_transform(AST, Options) -> any()

    parse_transform entry point