npm install lambda-js
For browsers, just copy
./lib/lambda.min.js (generated by UglifyJS)and include it in a
npm test to check your changes (using nodeunit) haven't broken existing functionality. Literate programming documentation can be found inside of the
/docs directory, generated by docco.
Until ECMAScript 6 gives us the fat-arrow syntax for one-liners in functional style evaluation,
12345678910mapx => x*2reducesum val => sum + val;
Such one-liners include a lot of ugly noise:
12345678910map return x*2reduce return sum + val ;
var adder = "a,b" "return a+b";
And with that capability, and caching already-constructed lambdas so there's no performance penalty beyond the initial construction, we can have nice one-liners even before ES6:
var l = require'lambda-js';12345678910mapl"x" "x*2"reducel"sum, val" "sum + val";
Lambdas are "pure" functions, not closures, so the following won't work:
var l = require'lambda-js';var foo = 'bar';var impureLambda = l'baz' 'foo + baz'; // Will throw a ReferenceError, foo not defined
But pure functions are perfectly predictable because their output is totally determined by their inputs, and the closures where they are defined cannot affect their behavior, so it can also be useful to "scrub" a larger function with lambda-js:
var l = require'lambda-js';var scrubbedFunction = lvar foosin = Mathsinfoo*foo;var barrep = barreplace/bar/g 'baz';return foosin + barrep;;
Lambda-js attaches a
pure property to the lambdas (with a value of
true), which can be used by other libraries to determine if the function they have been provided is a pure function or a closure.
If you need to transmit lambdas over the wire, Lambda-js provides a simple serialization and deserialization mechanism.
var l = require'lambda-js';var funcObj = lserializevar foosin = Mathsinfoo*foo;var barrep = barreplace/bar/g 'baz';return foosin + barrep;;var funcObjStr = JSONstringifyfuncObj; // Doesn't throwvar func = ldeserializefuncObj;
Copyright (C) 2012-2013 by David Ellis
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.