我正在学习函数式编程 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
?
最佳答案
How do we pass the parameter person into wasBornInCountry and wasNaturalized?
你不知道。由任一
生成的函数都可以。
What if I wish to call isCitizen with two parameters?
然后确保您传递给任一
的两个函数都接受两个参数。
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
只有一个参数。它被传递给两个函数。
如果有多个参数,那么它们都会传递给两个函数。
关于javascript - Ramda如何将参数传递给高阶函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54423105/