Last active
November 7, 2015 19:21
-
-
Save Heimdell/1ea87c90ed8a9513b673 to your computer and use it in GitHub Desktop.
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
not_implemented = function () { | |
throw "Implement me!" | |
} | |
map_list = not_implemented | |
map_nullable = not_implemented | |
map_function = not_implemented | |
assert_equal = function (x, y) { | |
console.log(JSON.stringify(x) == JSON.stringify(y)? "Yes" : "NO") | |
} | |
var inc = function (x) { return x + 1 } | |
assert_equal(map_list([1,2,3], inc), [2,3,4]) | |
assert_equal(map_nullable(5, inc), 6) | |
assert_equal(map_nullable(null, inc), null) | |
assert_equal(map_function(JSON.stringify, inc)(1), "2") | |
//--- | |
pure_list = not_implemented | |
pure_nullable = not_implemented | |
pure_function = not_implemented | |
assert_equal(pure_list(5), [5]) | |
assert_equal(pure_nullable(5), 5) | |
assert_equal(pure_function(5)("whatever"), 5) | |
//--- | |
// bind is flat_map | |
bind_list = not_implemented | |
bind_nullable = not_implemented | |
bind_function = not_implemented | |
non_null_inv = function (x) { | |
if (x == 0) | |
return [] | |
else | |
return [x, 1/x] | |
} | |
non_null_inv1 = function (x) { | |
if (x == 0) | |
return null | |
else | |
return 1/x | |
} | |
assert_equal(bind_list([-2,0,2], non_null_inv), [-2, -0.5, 2, 0.5]) | |
assert_equal(bind_nullable(null, non_null_inv1), null) | |
assert_equal(bind_nullable(0, non_null_inv1), null) | |
assert_equal(bind_nullable(2, non_null_inv1), 0.5) | |
assert_equal( | |
bind_function( | |
function (env) { return 1 + env["count"] }, | |
function (newcount) { | |
return function (env) { | |
return "count was " + env["count"] + ", became " + newcount | |
} | |
} | |
)({count: 5}), | |
"count was 5, became 6" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment