We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 557e445 commit 5aae33bCopy full SHA for 5aae33b
JavaScript/Y_Combinator/main.js
@@ -0,0 +1,36 @@
1
+'use strict';
2
+
3
+//------------------------------
4
+// Y combinator
5
+function Y(f) {
6
+ return (function(g) {
7
+ return g(g);
8
+ })(function(g) {
9
+ return f(function() { return g(g).apply(null, arguments); });
10
+ });
11
+}
12
13
14
+// via Y-combinator
15
+var f = Y(function(self) {
16
+ return function(n) {
17
+ return n < 2 ? n : self(n - 1) + self(n - 2);
18
+ }
19
+});
20
21
+// via function expression
22
+var f2 = (function f(n) {
23
+ return n < 2 ? n : f(n - 1) + f(n - 2);
24
+})
25
26
+// via expand function expression
27
+var f3 = (function () {
28
+ var f = function(n) {
29
30
+ };
31
+ return f;
32
+})();
33
34
+console.log(f(30));
35
+console.log(f2(30));
36
+console.log(f3(30));
0 commit comments