Created
August 19, 2014 16:23
-
-
Save davidjbeveridge/6196d24313cab801522a to your computer and use it in GitHub Desktop.
Partial Application
This file contains 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
// Some assertion functions we can play with | |
function assert(condition, message) { | |
if(!condition) { | |
throw new Error(message); | |
} | |
} | |
function assert_equal(obj1, obj2) { | |
assert(obj1 === obj2, 'Expected '+obj1+' to equal '+obj2+'.'); | |
} | |
/*************************************************************************** | |
* Implement partial application: | |
*/ | |
function partial(fn) { | |
// fill in here | |
} | |
function sum3(a,b,c) { return a+b+c; } | |
var sumWith5 = partial(sum3, 5); | |
assert_equal(typeof sumWith5, 'function'); | |
assert_equal(sumWith5(0,0), 5); | |
assert_equal(sumWith5(10,0), 15); | |
assert_equal(sumWith5(3,3), 11); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment