反转 JavaScript 中默认参数的顺序

Reversing the order of default arguments in JavaScript

我有以下递归 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 and 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 在他的回答中建议的方法。

有关详细信息,请阅读: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)))

What's the most elegant way to write such functions where the default arguments come first?

仅使用默认初始化程序需要一些神秘的技巧:

function demo(n, f = [n, n = 1][0]) {
    console.log(n, f);
}
demo(2, "f"); // 2 f
demo("g"); // 1 g
console.log(demo.length) // 1

最直接的方法是使用条件运算符进行解构:

function demo(...args) {
    const [n, f] = args.length < 2 ? [1, ...args] : args;
    console.log(n, f);
}
demo(2, "f"); // 2 f
demo("g"); // 1 g
console.log(demo.length) // 0

更多本着 "reversing the order of arguments" 的精神可能是字面意思:

function demo(...args) {
    const [f, n = 1] = args.reverse();
    console.log(n, f);
}
demo(2, "f"); // 2 f
demo("g"); // 1 g
console.log(demo.length) // 0

后两种尝试的缺点是需要额外的声明(阻止我们使用简洁的箭头函数)并且也没有反映 .length.[=15= 中的实际数量或所需参数]