Last active
February 21, 2017 10:49
-
-
Save renaudtertrais/5edcd8aac27355325d43986c3ca81589 to your computer and use it in GitHub Desktop.
Cap function n params. Useful to cap iteratee for example.
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
const capTo = n => fn => (...args) => fn(...args.slice(0, n)); | |
const capRightTo = n => fn => (...args) => fn(...args.slice(-n)); | |
const cap = capTo(0); | |
const add = (a, b) => a + b; | |
const sum = (...args) => args.reduce(add, 0); | |
// Simple Example | |
sum(1, 2, 3, 4); // --> 10 | |
cap(sum)(); // --> 0 | |
capTo(1)(sum)(1, 2, 3, 4); // --> 1 | |
capTo(2)(sum)(1, 2, 3, 4); // --> 3 | |
capTo(3)(sum)(1, 2, 3, 4); // --> 6 | |
capTo(4)(sum)(1, 2, 3, 4); // --> 10 | |
capRightTo(1)(sum)(1, 2, 3, 4); // --> 4 | |
capRightTo(2)(sum)(1, 2, 3, 4); // --> 7 | |
capRightTo(3)(sum)(1, 2, 3, 4); // --> 9 | |
capRightTo(4)(sum)(1, 2, 3, 4); // --> 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment