Created
September 10, 2019 17:53
-
-
Save bkyrlach/00669e85d71f35cd6cb5f91767096e1e to your computer and use it in GitHub Desktop.
Introduction to FP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function compose(f,g) { | |
| return function (a) { | |
| return g(f(a)); | |
| }; | |
| } | |
| function add1(x) { | |
| return x + 1; | |
| } | |
| function times2(x) { | |
| return x * 2; | |
| } | |
| function stringLength(s) { | |
| return s.length; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function curry(f) { | |
| return function (a) { | |
| return function (b) { | |
| return f(a, b); | |
| }; | |
| }; | |
| } | |
| function add(x, y) { | |
| return x + y; | |
| } | |
| function repeat(n,s) { | |
| return [...Array(n).keys()].map(function (x) { return s; }).reduce(function (a, b) { return a + b; }); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function foo(x, zs) { | |
| var a = bar(x); | |
| var b = bar(x); | |
| baz(a, b); | |
| var c = a + b; | |
| var d = zs.map(function (x) { return f(1,x); }).map(g); | |
| return q(c, d); | |
| } | |
| function main() { | |
| console.log("foo: " + foo(4, input)); | |
| //console.log("foo1: " + foo1(4, input)); | |
| } | |
| var input = [1,2,3,4,5,6]; | |
| function compose(f,g) { | |
| return function (a) { | |
| return g(f(a)); | |
| }; | |
| } | |
| function curry(f) { | |
| return function (a) { | |
| return function (b) { | |
| return f(a, b); | |
| }; | |
| }; | |
| } | |
| function bar(x) { | |
| return Math.sqrt(x); | |
| } | |
| function baz(a, b) { | |
| return a + b; | |
| } | |
| function f(n, x) { | |
| return n + x; | |
| } | |
| function g(x) { | |
| return x * 2; | |
| } | |
| function q(b, xs) { | |
| return xs.map(function (y) { | |
| return Math.pow(y, b); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment