main
Last change
on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
890 bytes
|
Line | |
---|
1 | import { curryN } from 'ramda';
|
---|
2 |
|
---|
3 | /**
|
---|
4 | * Y-combinator
|
---|
5 | *
|
---|
6 | * The Y combinator is an interesting function which only works with functional languages,
|
---|
7 | * showing how recursion can still be done even without any variable or function declarations,
|
---|
8 | * only functions and parameters
|
---|
9 | *
|
---|
10 | * @func Y
|
---|
11 | * @memberOf RA
|
---|
12 | * @since {@link https://char0n.github.io/ramda-adjunct/2.3.0|v2.3.0}
|
---|
13 | * @category Function
|
---|
14 | * @sig (a, ... -> b -> b) -> (a, ... -> b)
|
---|
15 | * @param {Function} le Recursive function maker
|
---|
16 | * @return {Function}
|
---|
17 | * @see {@link http://kestas.kuliukas.com/YCombinatorExplained/|Y combinator explained}
|
---|
18 | * @example
|
---|
19 | *
|
---|
20 | * const makeFact = givenFact => (n) => {
|
---|
21 | * if (n < 2) { return 1 }
|
---|
22 | * return n * givenFact(n - 1);
|
---|
23 | * };
|
---|
24 | *
|
---|
25 | * const factorial = RA.Y(makeFact);
|
---|
26 | *
|
---|
27 | * factorial(5); //=> 120
|
---|
28 | */
|
---|
29 |
|
---|
30 | const Y = curryN(1, (le) => ((f) => f(f))((g) => le((x) => g(g)(x))));
|
---|
31 |
|
---|
32 | export default Y;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.