Ramda 如何将参数传递给高阶函数?

Ramda how to pass parameter into higher order function?

我正在学习 here 的函数式编程,遇到以下代码

const wasBornInCountry = person => person.birthCountry === OUR_COUNTRY
const wasNaturalized = person => Boolean(person.naturalizationDate)
const isOver18 = person => person.age >= 18

const isCitizen = person => wasBornInCountry(person) || wasNaturalized(person)

const isEligibleToVote = person => isOver18(person) && isCitizen(person)

可以缩短到下面

const isCitizen = either(wasBornInCountry, wasNaturalized)
const isEligibleToVote = both(isOver18, isCitizen)

我似乎无法理解

const isCitizen = person => wasBornInCountry(person) || wasNaturalized(person)

可译为:

const isCitizen = either(wasBornInCountry, wasNaturalized)

我们如何将参数person传递给wasBornInCountrywasNaturalized?如果我想用两个参数调用 isCitizen 怎么办?我们如何知道哪个参数将传递给 wasBornInCountry 以及哪个参数传递给 wasNaturalized?

How do we pass the parameter person into wasBornInCountry and wasNaturalized?

你不知道。 either 生成的函数可以。

What if I wish to call isCitizen with two parameters?

然后确保您传递给 either 的两个函数中的每一个都接受两个参数。

const same = (x, y) => x == y;
const ten = (x, y) => x == 10 || y == 10;
const f = R.either(same, ten);
console.log([f(1, 1), f(2, 1), f(10, 3)])

How we know which parameter gonna be passed to wasBornInCountry and which parameter to wasNaturalized?

去看看你的原始代码:

const isCitizen = person => wasBornInCountry(person) || wasNaturalized(person)
  • person参数传递给wasBornInCountry
  • person参数传递给wasNaturalized

只有一个参数。它被传递给两个函数。

如果有多个参数,那么它们将全部传递给两个函数。