Last active
May 4, 2017 16:46
-
-
Save koozdra/87f6901295dcf3c70f1269aaef49c5d4 to your computer and use it in GitHub Desktop.
hash parameter functions with partial application
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 _ = require('lodash'); | |
function Point(x, y) { | |
return {x, y}; | |
}; | |
const Color = { | |
black: {r: 255, g: 255, b: 255, y: 1} | |
} | |
function wrap(defaults, f) { | |
const k = _.curry((keys, f, params, passedParams) => { | |
const remainingKeys = _.without(keys, ..._.keys(passedParams)); | |
const newParams = _.assign({}, params, passedParams); | |
if (remainingKeys.length === 0) { | |
f(newParams); | |
} else { | |
return k(remainingKeys, f, newParams); | |
} | |
}); | |
return k(_.keys(defaults), f, defaults); | |
} | |
const drawCircle = wrap({ | |
radius: 10, | |
color: Color.black, | |
center: Point(1,1) | |
}, | |
console.log | |
); | |
const smallCircles = drawCircle({radius: 20}); // partially applied | |
smallCircles({color: 1, center: 1}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment