【发布时间】:2018-06-23 09:44:08
【问题描述】:
我使用以下函数通过将一些数据包装在高阶组件中来将一些数据发送到 React 组件:
import { equals, filter, isNil, prop, where } from 'ramda'
const example = getChapter => ownProps =>
filter(
where({
sectionId: equals(prop('section', ownProps)),
subsectionId: isNil,
sub2sectionId: isNil,
sub3sectionId: isNil
}),
prop('getContents', getChapter)
)
我熟悉函数式编程的概念,但对 Ramda 库还是比较陌生。现在我想用 Ramda 重写这段代码,这样ownProps 和getChapter 就可以被消除,即使只是作为一个学术练习。
由于参数的顺序,我似乎应该首先消除ownProps?虽然我什至不确定。
任何帮助将不胜感激!
PS。我知道在上面的代码中ownProps.section 将更具可读性并且比prop('section', ownProps) 更受欢迎,但我希望prop 函数位于与上面代码等效的无点路径上,因此它们被包含在内。
【问题讨论】:
-
无点样式不会增加可重用性。
x => f (x)和无点f是等价的,但用不同的 style 表示。一个并不比另一个“更可重用”。 -
感谢您的澄清,我更新了问题。
-
我认为它看起来很糟糕,并且与最佳实践相去甚远,使其完全无点。它必须涉及许多步骤。一个步骤是使用 R.assoccreate 发送到 where 的对象。
-
因为函数不可交换是什么意思?如果你的意思是不能改变
example = getChapter => ownProps => ...中getChapter和ownProps的顺序,我认为是不正确的。我不明白你为什么不能把它改成example = ownProps => getChapter => ... -
哎呀,我的错,你是对的!
标签: javascript functional-programming ramda.js pointfree tacit-programming