Ramda Adjunct 4.0.0

Y.js

  1. import { curryN } from 'ramda';
  2. /**
  3. * Y-combinator
  4. *
  5. * The Y combinator is an interesting function which only works with functional languages,
  6. * showing how recursion can still be done even without any variable or function declarations,
  7. * only functions and parameters
  8. *
  9. * @func Y
  10. * @memberOf RA
  11. * @since {@link https://char0n.github.io/ramda-adjunct/2.3.0|v2.3.0}
  12. * @category Function
  13. * @sig (a, ... -> b -> b) -> (a, ... -> b)
  14. * @param {Function} le Recursive function maker
  15. * @return {Function}
  16. * @see {@link http://kestas.kuliukas.com/YCombinatorExplained/|Y combinator explained}
  17. * @example
  18. *
  19. * const makeFact = givenFact => (n) => {
  20. * if (n < 2) { return 1 }
  21. * return n * givenFact(n - 1);
  22. * };
  23. *
  24. * const factorial = RA.Y(makeFact);
  25. *
  26. * factorial(5); //=> 120
  27. */
  28. const Y = curryN(1, (le) => ((f) => f(f))((g) => le((x) => g(g)(x))));
  29. export default Y;