如何使以下功能无点(使用Ramda)?
const prefixAll = R.curry((prefix, list) => R.map(R.concat(prefix), list))
此外,这应该工作
R.useWith(R.map, [R.concat, R.identity])
(
R.identity
有正确的功能,请参阅@ scott-sauyet的评论。)
P.S:但我个人认为,compose
更好 - 使用部分应用论证是更多的功能方法。例如R.compose(R.map, R.concat)('a')(['a','b'])
我将代码输入pointfree.io(我首先将其转换为Haskell \prefix list -> map (prefix ++) list
),然后返回:
map . (++)
所以JavaScript等效应该是:
const prefixAll = R.compose(R.map, R.concat);
当我在ramda's REPL测试时,我想我得到了理想的结果。
IMO最简单的方法是使用在这种情况下通过R.lift提升R.concat函数实现的理解。
const prefixAll = R.lift(R.concat);
prefixAll(['hi-'], ['you', 'there']); // => ["hi-you", "hi-there"]
请注意,前缀必须作为单元素数组传递。 Ramda为您自动调整此功能。