使用 Ramda 使函数 pointfree
Make function pointfree using Ramda
如何使以下函数成为 pointfree(使用 Ramda)?
const prefixAll = R.curry((prefix, list) => R.map(R.concat(prefix), list))
我把代码输入pointfree.io(好吧我先把它转换成Haskell \prefix list -> map (prefix ++) list
),结果返回:
map . (++)
所以 JavaScript 等价物应该是:
const prefixAll = R.compose(R.map, R.concat);
当我在 ramda's REPL 中测试时,我认为我获得了想要的结果。
此外,这应该有效
R.useWith(R.map, [R.concat, R.identity])
(R.identity
is there for proper arity of the function, see @scott-sauyet's comment.)
P.S:但我个人认为,compose
更好——使用参数的部分应用是更实用的方法。例如R.compose(R.map, R.concat)('a')(['a','b'])
IMO 最简单的方法是使用在这种情况下通过 R.lift 提升 R.concat 函数实现的理解。
const prefixAll = R.lift(R.concat);
prefixAll(['hi-'], ['you', 'there']); // => ["hi-you", "hi-there"]
请注意,前缀必须作为单元素数组传递。
此函数由 Ramda 自动为您柯里化。
如何使以下函数成为 pointfree(使用 Ramda)?
const prefixAll = R.curry((prefix, list) => R.map(R.concat(prefix), list))
我把代码输入pointfree.io(好吧我先把它转换成Haskell \prefix list -> map (prefix ++) list
),结果返回:
map . (++)
所以 JavaScript 等价物应该是:
const prefixAll = R.compose(R.map, R.concat);
当我在 ramda's REPL 中测试时,我认为我获得了想要的结果。
此外,这应该有效
R.useWith(R.map, [R.concat, R.identity])
(
R.identity
is there for proper arity of the function, see @scott-sauyet's comment.)
P.S:但我个人认为,compose
更好——使用参数的部分应用是更实用的方法。例如R.compose(R.map, R.concat)('a')(['a','b'])
IMO 最简单的方法是使用在这种情况下通过 R.lift 提升 R.concat 函数实现的理解。
const prefixAll = R.lift(R.concat);
prefixAll(['hi-'], ['you', 'there']); // => ["hi-you", "hi-there"]
请注意,前缀必须作为单元素数组传递。 此函数由 Ramda 自动为您柯里化。