我正在研究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
传递到wasBornInCountry
和wasNaturalized
中?如果我想用两个参数调用isCitizen
怎么办?我们如何知道哪个参数将传递给wasBornInCountry
和哪个参数传递给wasNaturalized
?
答案 0 :(得分:4)
我们如何将参数person传递给wasBornInCountry和wasNaturalized?
你不知道。 either
生成的函数可以。
如果我想用两个参数调用isCitizen怎么办?
然后确保传递给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)])
我们怎么知道哪个参数将被传递给wasBornInCountry以及哪个参数将被wasNaturalized?
去看看您的原始代码:
const isCitizen = person => wasBornInCountry(person) || wasNaturalized(person)
person
参数传递给wasBornInCountry
person
参数传递给wasNaturalized
只有一个参数。它被传递到两个函数。
如果有多个参数,则它们将全部传递给两个函数。