4 ms·
https://github.com/lodash/lodash/wiki/FP-Guide https://github.com/lodash/lodash/wiki/FP-Guide Basically flipping around the args in a function and returning a
by eddythompson80 3y ago
https://github.com/lodash/lodash/wiki/FP-Guide https://github.com/lodash/lodash/wiki/FP-Guide
Basically flipping around the args in a function and returning a "curried" function call. So instead of
_.map(["1"], parseInt)
it's the other way around,
_.map(parseInt, ["1"])
That way you can do something like
const parseArrayFunc = _.map(parseInt)
const parsed = parseArrayFunc(["1"])
This pattern is called currying in functional programming.
- kristjansson 3y agoThe complaint, I would think, being with the machinery to make _.map return a function or a value depending on the number of args, not with the flipping/arg order stuff?
- deleted 3y ago[deleted]
- eddythompson80 3y agoYeah I’d think so too. FP languages like Haskell or F# has partial application as part of the language itself and you get it for free by just ordering the parameters that way.
- gorgoiler 3y agoPartial application makes sense to me as a useful computer science concept. Reasoning about functions when they always have 1-arity makes proofs possible. In software though, it just seems like code golf for people who can’t or won’t write a function as a way of sharing code between call sites: def serialize(fn, data, path): … yamlize = partial( serialize, yaml.dump ) jsonize = partial( serialize, json.dumps ) Presumably some of these people wake up in a cold sweat about the shame of typing “partial(serialize, …)” twice and do this: def serialize(fn, data, path): … a = lambda f: partial(serialize, f) yamlize = a(yaml.dump) jsonize = a(json.dumps) A good example of Don’t Repeat Yourself mutating into Never Repeat Yourself with ill effect, because the developer has self flagellated so much that the following code is deemed to have too much repetition in it: def write(data, path): … def write_yaml(data, path): write(yaml.dump(data), path) def write_json(data, path): write(json.dumps(data), path)