Skip to content

Instantly share code, notes, and snippets.

@petrovg
Created July 15, 2013 16:01
Show Gist options
  • Select an option

  • Save petrovg/6001138 to your computer and use it in GitHub Desktop.

Select an option

Save petrovg/6001138 to your computer and use it in GitHub Desktop.
<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