如何用 Ramda.js 合并 2 个作品
How to merge 2 compositions with Ramda.js
我有一个要转换的字符串:
"replace-me-correctly"
=> "Replace me correctly"
我的代码使用 Ramda.js:
const _ = R; //converting Ramda.js to use _
const replaceTail = _.compose(_.replace(/-/g, ' '), _.tail);
const upperHead = _.compose(_.toUpper ,_.head);
const replaceMe = (str) => _.concat(upperHead(str) , replaceTail(str));
replaceMe("replace-me-correctly"); // "Replace me correctly"
我想知道是否有更简洁、更有效的方法将 replaceTail
与 upperHead
结合起来,这样我只遍历字符串一次?
不确定只遍历字符串一次。听起来很难。不过,我会提供一些不同的方法来获得乐趣和洞察力。
函数的幺半群实例将通过 运行 每个函数与给定的参数连接并连接它们的结果(它们必须全部 return 相同类型才能正确组合)。 replaceMe
正是这样做的,因此我们可以使用 mconcat
代替。
const { compose, head, tail, replace, toUpper } = require('ramda')
const { mconcat } = require('pointfree-fantasy')
// fun with monoids
const replaceTail = compose(replace(/-/g, ' '), tail)
const upperHead = compose(toUpper, head)
const replaceMe = mconcat([upperHead, replaceTail])
replaceMe("replace-me-correctly")
//=> "Replace me correctly"
这是一种组合函数的有趣方式。我不确定为什么要求在 replace
之前获取 tail
。似乎 replace
函数可以更新为仅通过正则表达式替换起始字符后的任何 -
。如果是这样的话,我们可以内联 replace
.
还有一件事。 Profunctor 的 dimap
函数实例非常简洁,镜头也是如此。将它们一起使用,我们可以将字符串转换为数组,然后 toUpper
只是第 0 个索引。
const { curry, compose, split, join, head, replace, toUpper } = require('ramda')
const { mconcat } = require('pointfree-fantasy')
const { makeLenses, over } = require('lenses')
const L = makeLenses([])
// fun with dimap
const dimap = curry((f, g, h) => compose(f,h,g))
const asChars = dimap(join(''), split(''))
const replaceMe = compose(replace(/-/g, ' '), asChars(over(L.num(0), toUpper)))
replaceMe("replace-me-correctly")
//=> "Replace me correctly"
Brian 的解决方案很棒。
请注意,您可以使用 lift
:
在纯 Ramda 中执行类似 mconcat
的操作
const replaceMe = _.lift(_.concat)(upperHead, replaceTail)
我有一个要转换的字符串:
"replace-me-correctly"
=> "Replace me correctly"
我的代码使用 Ramda.js:
const _ = R; //converting Ramda.js to use _
const replaceTail = _.compose(_.replace(/-/g, ' '), _.tail);
const upperHead = _.compose(_.toUpper ,_.head);
const replaceMe = (str) => _.concat(upperHead(str) , replaceTail(str));
replaceMe("replace-me-correctly"); // "Replace me correctly"
我想知道是否有更简洁、更有效的方法将 replaceTail
与 upperHead
结合起来,这样我只遍历字符串一次?
不确定只遍历字符串一次。听起来很难。不过,我会提供一些不同的方法来获得乐趣和洞察力。
函数的幺半群实例将通过 运行 每个函数与给定的参数连接并连接它们的结果(它们必须全部 return 相同类型才能正确组合)。 replaceMe
正是这样做的,因此我们可以使用 mconcat
代替。
const { compose, head, tail, replace, toUpper } = require('ramda')
const { mconcat } = require('pointfree-fantasy')
// fun with monoids
const replaceTail = compose(replace(/-/g, ' '), tail)
const upperHead = compose(toUpper, head)
const replaceMe = mconcat([upperHead, replaceTail])
replaceMe("replace-me-correctly")
//=> "Replace me correctly"
这是一种组合函数的有趣方式。我不确定为什么要求在 replace
之前获取 tail
。似乎 replace
函数可以更新为仅通过正则表达式替换起始字符后的任何 -
。如果是这样的话,我们可以内联 replace
.
还有一件事。 Profunctor 的 dimap
函数实例非常简洁,镜头也是如此。将它们一起使用,我们可以将字符串转换为数组,然后 toUpper
只是第 0 个索引。
const { curry, compose, split, join, head, replace, toUpper } = require('ramda')
const { mconcat } = require('pointfree-fantasy')
const { makeLenses, over } = require('lenses')
const L = makeLenses([])
// fun with dimap
const dimap = curry((f, g, h) => compose(f,h,g))
const asChars = dimap(join(''), split(''))
const replaceMe = compose(replace(/-/g, ' '), asChars(over(L.num(0), toUpper)))
replaceMe("replace-me-correctly")
//=> "Replace me correctly"
Brian 的解决方案很棒。
请注意,您可以使用 lift
:
mconcat
的操作
const replaceMe = _.lift(_.concat)(upperHead, replaceTail)