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:
1.1 KB
|
Line | |
---|
1 | "use strict";
|
---|
2 |
|
---|
3 | exports.__esModule = true;
|
---|
4 | exports["default"] = void 0;
|
---|
5 | var _ramda = require("ramda");
|
---|
6 | /**
|
---|
7 | * Y-combinator
|
---|
8 | *
|
---|
9 | * The Y combinator is an interesting function which only works with functional languages,
|
---|
10 | * showing how recursion can still be done even without any variable or function declarations,
|
---|
11 | * only functions and parameters
|
---|
12 | *
|
---|
13 | * @func Y
|
---|
14 | * @memberOf RA
|
---|
15 | * @since {@link https://char0n.github.io/ramda-adjunct/2.3.0|v2.3.0}
|
---|
16 | * @category Function
|
---|
17 | * @sig (a, ... -> b -> b) -> (a, ... -> b)
|
---|
18 | * @param {Function} le Recursive function maker
|
---|
19 | * @return {Function}
|
---|
20 | * @see {@link http://kestas.kuliukas.com/YCombinatorExplained/|Y combinator explained}
|
---|
21 | * @example
|
---|
22 | *
|
---|
23 | * const makeFact = givenFact => (n) => {
|
---|
24 | * if (n < 2) { return 1 }
|
---|
25 | * return n * givenFact(n - 1);
|
---|
26 | * };
|
---|
27 | *
|
---|
28 | * const factorial = RA.Y(makeFact);
|
---|
29 | *
|
---|
30 | * factorial(5); //=> 120
|
---|
31 | */
|
---|
32 |
|
---|
33 | var Y = (0, _ramda.curryN)(1, function (le) {
|
---|
34 | return function (f) {
|
---|
35 | return f(f);
|
---|
36 | }(function (g) {
|
---|
37 | return le(function (x) {
|
---|
38 | return g(g)(x);
|
---|
39 | });
|
---|
40 | });
|
---|
41 | });
|
---|
42 | var _default = Y;
|
---|
43 | exports["default"] = _default; |
---|
Note:
See
TracBrowser
for help on using the repository browser.