5

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 = _.compose(_.replace(/-/g, ' '), _.tail);
const upperHead = _.compose(_.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 combine replaceTail with upperHead so I only traverse the string once?

JSBin example

cmdv
  • 1,693
  • 3
  • 15
  • 23

2 Answers2

6

Not 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 combine correctly). replaceMe is doing exactly that so we can use mconcat instead.

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"

That's a fun way to combine 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, 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
  • 692
  • 3
  • 14
  • Massive +1 the `mconcat` from your library is what I was looking for. Dimap looks fun will have to look further into that :) – cmdv Nov 27 '15 at 09:02
4

Brian's solutions are great.

Note that you can do something like mconcat in plain Ramda using lift:

const replaceMe = _.lift(_.concat)(upperHead, replaceTail)
Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
  • ah cool I was searching through the docs for a while and couldn't find which operator to use thank you – cmdv Nov 27 '15 at 18:48