Last active
November 11, 2019 22:20
-
-
Save gunar/1268c997ca66343f060dbca07aee67bd to your computer and use it in GitHub Desktop.
Currying Functions with Named Parameters in JavaScript
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
/** | |
* Currying Functions with Named Parameters. | |
* @gunar, @drboolean, @dtipson 2016 | |
* | |
* Why does it return a thunk, though? | |
* Because in JS named arguments are opaque. There is no way of getting a function's named arguments list. | |
* e.g. | |
* const foo = function ({ a, b }) { } | |
* console.log(foo.arguments) // Throws. Ideally would return ['a', 'b']. | |
* | |
* What would be an alternative? | |
* Providing a "spec". curryNamed would have to be told what the named arguments were upfront | |
* (as curryN does with numbered arguments). I personally think that's less useful. | |
*/ | |
'use strict' | |
function curryNamed(fn, acc = {}) { | |
return args => { | |
if (!args) return fn(acc) | |
return curryNamed(fn, Object.assign({}, acc, args)) | |
} | |
} | |
const foo = ({ a, b, c } = {}) => console.log({ a, b, c }) | |
const bar = curryNamed(foo) | |
bar() // { a: undefined, b: undefined, c: undefined } | |
const a = bar({ a: 1 }) | |
a() // { a: 1, b: undefined, c: undefined } | |
const ab = a({ b: 2 }) | |
ab() // { a: 1, b: 2, c: undefined } | |
bar({ a: 1, b: 2, c: 3 })() // { a: 1, b: 2, c: 3 } |
You are correct. This should be the format:
const curried = curryNamed(fn, ['user', 'client']);
curried({ user: 1 });
curried({ client: 2 }) // evaluates "fn";
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this desired though?
bar({a:6,b:2,z:7});//->Object {a: 6, b: 2, c: undefined}
If the number of props is all it knows, then there's nothing guaranteeing that the named props used in the function will all be there, so they can't be relied on either in the function or any functions this one is composed with.