Skip to content

Instantly share code, notes, and snippets.

@davidjbeveridge
Last active August 29, 2015 14:05
Show Gist options
  • Save davidjbeveridge/b35b6c7c9751afc0a93f to your computer and use it in GitHub Desktop.
Save davidjbeveridge/b35b6c7c9751afc0a93f to your computer and use it in GitHub Desktop.
Right-associative parital application
// 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 right-associateive partial application: *
**************************************************************************/
function partial_right(fn) {
// fill in here
}
function complex(a,b,c,d) { return (a+b+c)/d; }
var complexWith2 = partial_right(complex, 2);
// complexWith2(1,2,3) === complex(1,2,3,2);
var complexWith4_2 = partial_right(complex, 4,2);
// complexWith4_2(1,2) === complex(1,2,4,2);
assert_equal(typeof complexWith2, 'function');
assert_equal(complexWith2(1,2,3), 3);
assert_equal(complexWith2(33,34,33), 50);
assert_equal(complexWith2(4,4,4), 6);
assert_equal(typeof complexWith4_2, 'function');
assert_equal(complexWith2(4,4), 6);
assert_equal(complexWith2(12, 16), 16);
assert_equal(complexWith2(7, 7), 8);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment