不确定遍历字符串一次。听起来很困难。尽管我会提供一些不同的方法来获得乐趣和洞察力。
函数的monoid实例会通过用给定的参数运行它们并将它们的结果进行连接(它们必须全部返回相同类型才能正确组合)来对每个函数进行连接。 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"
大规模+1从资料库中的'mconcat'就是我一直在寻找。 Dimap看起来有趣,将不得不进一步深入:) – cmdv