fn-proxy
Simple function proxying. Allows for point-free recursion in JavaScrpipt, and simulation of lazy function evaluation.
Install
$ npm install fn-proxy
Run the specs (make sure jasmine-node
is installed)
$ npm test
Usage
Require the module
var proxy = require('fn-proxy');
Examples
All examples show a point free recursive strategy for finding the length of a list. This would normally not be possible with regular JavaScript. Also note: examples use point-free style functions, something like ramda
or lodash
would provide.
- Explicit proxy - save proxy after declaration
var length = ifElse(isEmpty, always(0), compose(inc, proxy('length'), tail));
proxy('length', length);
length([])
length([1, 2, 3, 4])
- Short-hand proxy chaining
var length = ifElse(
isEmpty, always(0), compose(inc, proxy('length'), tail)
).proxy('length');
length([])
length([1, 2, 3, 4])
- Wrap declaration with proxy
var length = proxy('length', ifElse(
isEmpty, always(0), compose(inc, proxy('length'), tail)
));
length([])
length([1, 2, 3, 4])
- Invoke the proxy directly
proxy('length', ifElse(isEmpty, always(0), compose(inc, proxy('length'), tail)));
proxy('length')([])
proxy('length')([1, 2, 3, 4])