Functional.js
Functional syntax sugar and more, for JavaScript
Why Functional?
1 .Spead up your develop!
2. Build a high-level logic!
3. For fun, isn't it?
Install
Of course. Install by npm with one line
npm install functionaljs
Build
Because ECMAScript 6 Standard has released, so we all use this to build. But not all runtime support the standard.
So, if you use Node.js v5.0.0
or higher, you can use pure *-ec6.js
to run. Otherwise use the compiled js(without -ec6
) by Babel
You can install Babel
through npm
and use node-babel
instead of node
to run example.
sudo npm install -g babel-cli
We recommend to use Sublime Text with Babel plugin. (Which support EC6 and JSX syntax better) and here is a build-system for Sublime in Other/Babel.sublime-build
. One short-key Command + Shift + B
to run.
Async
What is async Wikipedia About future and promise
- Async foreach call
var ForEach = ForEach;var fs = ;var arr = '/etc/hosts' '/etc/paths' '/etc/donthavethisfile'; var { var done = this; fs}
Promise
- Promise
Just use Node.js built-in Promise(from
0.12
), or use Promise for ancient version Node.js
- Promisify
Convert callback function to promise object
var promisify = Promisify;var fs = ;var readFile = ;;
Monad
var Monad = Monad;//Usevar monad = ;var monad = 10; monad;monad; monad;var result = monad console;//60
Optional
What is optional? Wikipedia Optional in Swift
var Optional = Optional;//Usevar op = 1; op; var result = op; if result result;console;//4
Curry
Attention. Use
Function.curry
call may be conflict with your custom functioncurry
prototype(if you do so). Please set it toundefined
if you don't want
var curry = Curry;//Use by invokingvar { return a * b;}var double = ; //Use by function prototypevar square = multiplyconsole;//12console;//6console;//16
Combinator
What is Y-combinator? Wikipedia 知乎来源
Attention: for strict languages(which means function call by value) such as
JavaScript
, using originalY-combinator
will cause stack overflow because callingf(x(x))
means the compiler(interpreter) will try to generateaccuracy
defination with infinity stack alloc. So we useZ-combinator
to actually implementY-combinator
. For more, see: Z-combinator
- Y-combinator
/*We can build a `pure` lambda(anonymous function) with Y-combinator.See `y-test.js` and learn from step by step what `Y-combinator` is*/ var Y = Y;var fibonacci = console
Lazy
Thanks to @pkrumins
var Lazy = Lazy; //Lazy rangeLazy ;//0 4 8 12 16
Retroactive(Data Structure)
What is retroactive? Wikipedia Paper Reference
At now just implement
retoractive queue
var retroactive = Retroactive; //partial construct with false or null, fully with true var partial = false;var fully = true; //next use property to generate new object var partialQueue = ; { queue; queue; queue; //[1,2] queue; queue; //[2] queue; queue; //[1,2] queue; queue; //[2]}