Created
July 15, 2013 16:01
-
-
Save petrovg/6001138 to your computer and use it in GitHub Desktop.
Going through this description of monads - http://blog.jcoglan.com/2011/03/05/translation-from-haskell-to-javascript-of-selected-portions-of-the-best-introduction-to-monads-ive-ever-read/
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
| <html> | |
| <head></head> | |
| <body> | |
| <script> | |
| var A = {who: "A"}; | |
| A.sine = function(x) { return Math.sin(x); } | |
| A.cube = function(x) { return x*x*x; } | |
| A.compose = function(f, g) { | |
| return function(x) { return f(g(x)); } | |
| } | |
| A.sincube = A.compose (A.sine, A.cube); | |
| var B = {who: "B"}; | |
| B.sine = function(x) { return [Math.sin(x), 'Sine result']; } | |
| B.cube = function(x) { return [x * x * x, 'Cube result']; } | |
| B.compose = function(f, g) { | |
| return function(x) { | |
| var gresult = g(x); | |
| var fresult = f( gresult[0] ); | |
| return [fresult[0], gresult[1] + ',' + fresult[1]]; | |
| } | |
| } | |
| B.sincube = B.compose(B.sine, B.cube); | |
| var C = {who: "C"}; | |
| C.sine = function(x) { return [Math.sin(x[0]), x[1] + 'Sine result']; } | |
| C.cube = function(x) { return [x[0] * x[0] * x[0], x[1] + 'Cube result']; } | |
| C.sincube = A.compose(C.sine, C.cube); | |
| var bind = function(f) { | |
| return function(tuple) { | |
| var x = tuple[0], | |
| s = tuple[1], | |
| fx = f(x), | |
| y = fx[0], | |
| t = fx[1]; | |
| return [y, s + t]; | |
| } | |
| } | |
| var D = {who: "D"}; | |
| D.sincube = A.compose(bind(A.sine), bind(A.cube)); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment