admin管理员组文章数量:1023758
I have a string that I'm converting:
"replace-me-correctly"
=> "Replace me correctly"
my code using Ramda.js:
const _ = R; //converting Ramda.js to use _
const replaceTail = _pose(_.replace(/-/g, ' '), _.tail);
const upperHead = _pose(_.toUpper ,_.head);
const replaceMe = (str) => _.concat(upperHead(str) , replaceTail(str));
replaceMe("replace-me-correctly"); // "Replace me correctly"
What I'd like to know is if there is a cleaner more efficient way to bine replaceTail
with upperHead
so I only traverse the string once?
JSBin example
I have a string that I'm converting:
"replace-me-correctly"
=> "Replace me correctly"
my code using Ramda.js:
const _ = R; //converting Ramda.js to use _
const replaceTail = _.pose(_.replace(/-/g, ' '), _.tail);
const upperHead = _.pose(_.toUpper ,_.head);
const replaceMe = (str) => _.concat(upperHead(str) , replaceTail(str));
replaceMe("replace-me-correctly"); // "Replace me correctly"
What I'd like to know is if there is a cleaner more efficient way to bine replaceTail
with upperHead
so I only traverse the string once?
JSBin example
Share Improve this question edited Nov 27, 2015 at 0:11 cmdv asked Nov 26, 2015 at 23:39 cmdvcmdv 1,7433 gold badges16 silver badges23 bronze badges2 Answers
Reset to default 6Not sure about traversing the string just once. Sounds difficult. I'll offer some different approaches for fun and insight though.
The monoid instance for function will concat each function by running them all with the given argument and concatting their results (they have to all return the same type to bine correctly). replaceMe
is doing exactly that so we can use mconcat
instead.
const { pose, head, tail, replace, toUpper } = require('ramda')
const { mconcat } = require('pointfree-fantasy')
// fun with monoids
const replaceTail = pose(replace(/-/g, ' '), tail)
const upperHead = pose(toUpper, head)
const replaceMe = mconcat([upperHead, replaceTail])
replaceMe("replace-me-correctly")
//=> "Replace me correctly"
That's a fun way to bine functions. I wasn't sure why the requirement was to grab the tail
before the replace
. Seems like the replace
function could be updated to just replace any -
past the starting char via regex. If that's the case we could inline the replace
.
One more thing. The function instance of dimap
from Profunctor is pretty neat and so are lenses. Using them together, we can convert the string to an array, then toUpper
just the 0th index.
const { curry, pose, 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) => pose(f,h,g))
const asChars = dimap(join(''), split(''))
const replaceMe = pose(replace(/-/g, ' '), asChars(over(L.num(0), toUpper)))
replaceMe("replace-me-correctly")
//=> "Replace me correctly"
Brian's solutions are great.
Note that you can do something like mconcat
in plain Ramda using lift
:
const replaceMe = _.lift(_.concat)(upperHead, replaceTail)
I have a string that I'm converting:
"replace-me-correctly"
=> "Replace me correctly"
my code using Ramda.js:
const _ = R; //converting Ramda.js to use _
const replaceTail = _pose(_.replace(/-/g, ' '), _.tail);
const upperHead = _pose(_.toUpper ,_.head);
const replaceMe = (str) => _.concat(upperHead(str) , replaceTail(str));
replaceMe("replace-me-correctly"); // "Replace me correctly"
What I'd like to know is if there is a cleaner more efficient way to bine replaceTail
with upperHead
so I only traverse the string once?
JSBin example
I have a string that I'm converting:
"replace-me-correctly"
=> "Replace me correctly"
my code using Ramda.js:
const _ = R; //converting Ramda.js to use _
const replaceTail = _.pose(_.replace(/-/g, ' '), _.tail);
const upperHead = _.pose(_.toUpper ,_.head);
const replaceMe = (str) => _.concat(upperHead(str) , replaceTail(str));
replaceMe("replace-me-correctly"); // "Replace me correctly"
What I'd like to know is if there is a cleaner more efficient way to bine replaceTail
with upperHead
so I only traverse the string once?
JSBin example
Share Improve this question edited Nov 27, 2015 at 0:11 cmdv asked Nov 26, 2015 at 23:39 cmdvcmdv 1,7433 gold badges16 silver badges23 bronze badges2 Answers
Reset to default 6Not sure about traversing the string just once. Sounds difficult. I'll offer some different approaches for fun and insight though.
The monoid instance for function will concat each function by running them all with the given argument and concatting their results (they have to all return the same type to bine correctly). replaceMe
is doing exactly that so we can use mconcat
instead.
const { pose, head, tail, replace, toUpper } = require('ramda')
const { mconcat } = require('pointfree-fantasy')
// fun with monoids
const replaceTail = pose(replace(/-/g, ' '), tail)
const upperHead = pose(toUpper, head)
const replaceMe = mconcat([upperHead, replaceTail])
replaceMe("replace-me-correctly")
//=> "Replace me correctly"
That's a fun way to bine functions. I wasn't sure why the requirement was to grab the tail
before the replace
. Seems like the replace
function could be updated to just replace any -
past the starting char via regex. If that's the case we could inline the replace
.
One more thing. The function instance of dimap
from Profunctor is pretty neat and so are lenses. Using them together, we can convert the string to an array, then toUpper
just the 0th index.
const { curry, pose, 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) => pose(f,h,g))
const asChars = dimap(join(''), split(''))
const replaceMe = pose(replace(/-/g, ' '), asChars(over(L.num(0), toUpper)))
replaceMe("replace-me-correctly")
//=> "Replace me correctly"
Brian's solutions are great.
Note that you can do something like mconcat
in plain Ramda using lift
:
const replaceMe = _.lift(_.concat)(upperHead, replaceTail)
本文标签: javascriptHow to merge 2 compositions with RamdajsStack Overflow
版权声明:本文标题:javascript - How to merge 2 compositions with Ramda.js - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745595528a2158144.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论