Skip to content

Instantly share code, notes, and snippets.

@koozdra
Last active May 4, 2017 16:46
Show Gist options
  • Save koozdra/87f6901295dcf3c70f1269aaef49c5d4 to your computer and use it in GitHub Desktop.
Save koozdra/87f6901295dcf3c70f1269aaef49c5d4 to your computer and use it in GitHub Desktop.
hash parameter functions with partial application
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