我有以下递归compose
函数:
const compose = (f, n = 1) => n > 1 ?
compose(compose(f), n - 1) :
g => x => f(g(x));
const length = a => a.length;
const filter = p => a => a.filter(p);
const countWhere = compose(length, 2)(filter);
const odd = n => n % 2 === 1;
console.log(countWhere(odd)([1,2,3,4,5,6,7,8,9])); // 5
现在,我想做的是翻转 compose
的参数,以便默认参数位于第一个:
const compose = (n = 1, f) => n > 1 ? // wishful thinking
compose(n - 1, compose(f)) : // compose(f) is the same as compose(1, f)
g => x => f(g(x));
const length = a => a.length;
const filter = p => a => a.filter(p);
const countWhere = compose(2, length)(filter); // I want to call it like this
const odd = n => n % 2 === 1;
console.log(countWhere(odd)([1,2,3,4,5,6,7,8,9])); // 5
编写默认参数优先的函数的最优雅的方法是什么?
<小时/>编辑:我实际上想创建 map
和 ap
各种函数的方法,这样我就可以写:
const length = a => a.length;
const filter = p => a => a.filter(p);
const countWhere = length.map(2, filter); // length <$> filter
const pair = x => y => [x, y];
const add = x => y => x + y;
const mul = x => y => x * y;
const addAndMul = pair.map(2, add).ap(2, mul); // (,) <$> (+) <*> (*)
因此,我不想像 Bergi 在他的回答中建议的那样柯里化(Currying)方法。
有关更多信息,请阅读:Is implicit wrapping and unwrapping of newtypes in Haskell a sound idea?
最佳答案
我建议不要重载您的函数或使用默认参数:
const compose = n => f => n > 1
? compose(n - 1)(composeOne(f))
: g => x => f(g(x));
const composeOne = compose(1);
在这种情况下,您也可以将其内联,因为看起来 composeOne
不会在其他任何地方调用:
const compose = n => f => n > 1
? compose(n - 1)(compose(1)(f))
: g => x => f(g(x));
或者甚至根本不进行递归调用,但始终创建 g => x => ...
lambda 并有条件地对其进行转换:
const compose = n => f => {
const l = g => x => f(g(x));
return n > 1 ? compose(n - 1)(l) : l;
};
// same without temporary variables:
const id = x => x;
const compose = n => f => (n > 1 ? compose(n-1) : id)(g => x => f(g(x)))
关于javascript - 反转 JavaScript 中默认参数的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51686639/