[英]Ramda: rewriting to point free style
I use the following function to send some data to a React component by wrapping it in a higher-order component: 我使用以下函数通过将数据包装到高阶组件中来将一些数据发送到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)
)
I'm familiar with the concepts of functional programming, but still relatively new to the Ramda library. 我对函数式编程的概念很熟悉,但是对于Ramda库来说还是比较陌生的。 Now I'd like to rewrite this code using Ramda so that
ownProps
and getChapter
can get eliminated, if only as an academic exercise. 现在,我想使用
ownProps
重写此代码,以便仅作为学术练习就可以消除ownProps
和getChapter
。
It seems like I should eliminate ownProps
first because of the order of the arguments? 由于参数的顺序,我似乎应该首先消除
ownProps
? Although I'm not even sure of that. 尽管我什至不确定。
Any help would be greatly appreciated! 任何帮助将不胜感激!
PS. PS。 I'm aware that in the code above
ownProps.section
would be more readable and preferred to prop('section', ownProps)
, ceteri paribus, but I expect that the prop
function lies on the path to a point-free equivalent of the code above, hence their inclusion. 我知道在上述代码中,
ownProps.section
更具可读性,并且比prop('section', ownProps)
,ceteri paribus更具可读性,但我希望prop
函数位于通向无符号等效点的路径上上面的代码,因此将其包含在内。
Could this be done? 能做到吗? Sure.
当然。 Here's one version:
这是一个版本:
const example2 = useWith(flip(filter), [prop('getContents'), pipe(
prop('section'),
equals,
objOf('sectionId'),
where,
both(where({
subsectionId: isNil,
sub2sectionId: isNil,
sub3sectionId: isNil
}))
)])
The most unusual function in there is Ramda's useWith
, which acts approximately like useWith(f, [g, h])(x, y) ~> f(g(x, y), h(x, y))
. useWith
的useWith
是其中最不常见的函数,其作用类似于useWith(f, [g, h])(x, y) ~> f(g(x, y), h(x, y))
。 This is not a standard functional language construct, only one that helped make point-free some functions that were difficult to do so. 这不是一种标准的功能语言构造,只是一种有助于使一些难以做到的功能变得没有意义的功能。 It was mostly useful, though, before ES6/ES2015 became ubiquitous.
不过,在ES6 / ES2015普及之前,它最有用。
You can see this in action on the Ramda REPL . 您可以在Ramda REPL上看到这一点。
Should you do this? 你应该这样做吗? Well, you said it's an academic exercise, so perhaps.
好吧,你说这是一项学术活动,也许吧。 But I find this much less readable than the original.
但是我发现它比原始的可读性差得多。 This seems to me useful only as a way to learn some of the features of Ramda.
在我看来,这仅作为学习Ramda某些功能的一种方式。 I wouldn't put it into production.
我不会投入生产。
In addition to Scott's answer this is another possible variant: 除了斯科特的答案,这是另一个可能的变体:
useWith(
flip(filter),
[
prop('getContents'),
pipe(
prop('section'),
equals,
assoc('sectionId', R.__, {
subsectionId: isNil,
sub2sectionId: isNil,
sub3sectionId: isNil
}),
where
)
])
And if you call it with the order of the arguments reversed you don't need the flip
on the filter
. 而且如果以倒序的参数顺序调用它,则不需要
filter
的flip
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.