Wiki

Clone wiki

lambdascript / Home

LambdaScript

Yet another Javascript functional library. It defines the standard higher-order functions such as map, reduce (aka foldl), and filter (aka select) as well as other utilities.

Features

  • high order functions
  • just one global symbol by default (LambdaScript), optionally imports in the global namespace all the functions
  • already used in several projects
  • pure functions: no monkey patching on String/Object/Function/Array prototypes
  • maven project (unit tests based on rhino 1.7 + junit)

Examples

LambdaScript.install(); // optionally install function in the global namespace 

// map
map([1, 2, 3], lambda('a*a')) // [1, 4, 9]

// filter 
filter([-3, 4, 5, -6, -42], lambda('a>0')) // [4, 5] 

// reduce
function sum() {
   return reduce(arguments, lambda('a+b'));
}

sum(1,2,3)  // 6

// factorial
function fact(n) {
  return reduce(range(n), lambda('a*b'));
}

fact(5) // 120

// curry
var mul = lambda('a*b');
var mul_by2 = curry(mul, 2);
var mul_by3 = curry(mul, 3);
mul_by2(21) // 42
mul_by3(14) // 42

// range
range(3)           // [1, 2, 3] 
range(5, 7)       // [5, 6, 7]
range(1, 10, 2) // [1, 3, 5, 7, 9]

Updated