使用Ramda使函数pointfree

问题描述 投票:1回答:3

如何使以下功能无点(使用Ramda)?

const prefixAll = R.curry((prefix, list) => R.map(R.concat(prefix), list))
javascript functional-programming ramda.js pointfree
3个回答
7
投票

此外,这应该工作

R.useWith(R.map, [R.concat, R.identity])

R.identity有正确的功能,请参阅@ scott-sauyet的评论。)

see Ramda REPL

P.S:但我个人认为,compose更好 - 使用部分应用论证是更多的功能方法。例如R.compose(R.map, R.concat)('a')(['a','b'])


2
投票

我将代码输入pointfree.io(我首先将其转换为Haskell \prefix list -> map (prefix ++) list),然后返回:

map . (++)

所以JavaScript等效应该是:

const prefixAll = R.compose(R.map, R.concat);

当我在ramda's REPL测试时,我想我得到了理想的结果。


0
投票

IMO最简单的方法是使用在这种情况下通过R.lift提升R.concat函数实现的理解。

const prefixAll = R.lift(R.concat);
prefixAll(['hi-'], ['you', 'there']); // => ["hi-you", "hi-there"]

请注意,前缀必须作为单元素数组传递。 Ramda为您自动调整此功能。

© www.soinside.com 2019 - 2024. All rights reserved.