Yoyo
Yoyo is a tiny trampoline (iteratively invokes thunk-returning functions) library with partial application support.
Install
Available via bower:
bower install yoyo
Available via npm:
npm install yoyojs
Usage
Factorial example:
var yoyo = ; { if n < 2 return acc; return fac;} ; // 631; // 631; // 6
Is even/odd example:
var yoyo = ; { return n === 0 ? true : odd;} { return n === 0 ? false : even;} ; // true2; // true
Why
Without trampolining, a new stack frame is created on each iteration call and thus overflowing the maximum call stack size:
With trampolining, there is not a new stack created because of tail-call elimation, meaning that after each iteration the result is returned and the trampoline takes care of calling it again with the new result if the returned result is thunk:
For large values, you can use the BigInteger library.
Test
npm test
Credits
JavaScript Allongé by Reg "Raganwald" Braithwaite.
License
Released under the MIT License.