These are chat archives for ramda/ramda

7th
Mar 2016
David Chambers
@davidchambers
Mar 07 2016 07:24
I just wrote this:
//    controlWrapping :: String -> String
const controlWrapping =
R.pipe(R.split(' :: '),
       R.map(R.split(' => ')),
       R.map(R.map(R.split(/([(][^()]+[)])/))),
       R.map(R.map(R.append(''))),
       R.map(R.map(R.splitEvery(2))),
       R.map(R.map(R.map(R.over(R.lensIndex(1), nbsp)))),
       R.map(R.map(R.unnest)),
       R.map(R.map(R.map(R.split(' -> ')))),
       R.map(R.map(R.map(R.map(nbsp)))),
       R.map(R.map(R.map(R.join(' -> ')))),
       R.map(R.map(R.join(''))),
       R.map(R.join(' => ')),
       R.join(' :: '));
> controlWrapping('map :: Functor f => (a -> b) -> f a -> f b')
'map :: Functor f => (a -> b) -> f a -> f b'
Scott Christopher
@scott-christopher
Mar 07 2016 08:20
@davidchambers: That's a whopper :D
You could make use of pipe(map(f), map(g)) === map(pipe(f, g)) here.
Should look something like:
const controlWrapping_ = R.pipe(
  R.split(' :: '),
  R.map(R.pipe(
    R.split(' => '),
    R.map(R.pipe(
      R.split(/([(][^()]+[)])/),
      R.append(''),
      R.splitEvery(2),
      R.map(R.over(R.lensIndex(1), nbsp)),
      R.unnest,
      R.map(R.pipe(
        R.split(' -> '),
        R.map(nbsp),
        R.join(' -> ')
      )),
      R.join('')
    ),
    R.join(' => ')
  ))),
  R.join(' :: ')
);
David Chambers
@davidchambers
Mar 07 2016 08:21
That's true, though I prefer nested maps to nested pipes. ;)
We could probably write this as a :mag: composition, even.
Have a look at plaid/transcribe#10 if you're interested in the context.
Scott Christopher
@scott-christopher
Mar 07 2016 08:22
Yeah, I was thinking it could be written using isos and traversals.
David Chambers
@davidchambers
Mar 07 2016 08:23
Interesting!
Scott Christopher
@scott-christopher
Mar 07 2016 08:23
Sure. I'll take a look once I get home.
David Chambers
@davidchambers
Mar 07 2016 08:24
It's good to have an example ready for the day we decide to add isos and traversals to Ramda or ramda-fantasy. ;)
Scott Christopher
@scott-christopher
Mar 07 2016 11:46
Seems like it's somewhat possible:
const splitStrI    = delim => Iso.iso(R.split(delim), R.join(delim));
const splitEveryI  = n => Iso.iso(R.splitEvery(n), R.unnest);
const splitParensI = Iso.iso(R.split(/([(][^()]+[)])/), R.join(''));

const opticStart = R.compose(
  splitStrI(' :: '), Traversal.traversed, splitStrI(' => '), Traversal.traversed, splitParensI
);

const opticA = R.compose(
  opticStart, Traversal.traversed, splitStrI(' -> '), Traversal.traversed
);
const opticB = R.compose(
  opticStart, splitEveryI(2), Traversal.traversed, Traversal.ixArray(1)
);

const sig = 'map :: Functor f => (a -> b) -> f a -> f b';
const nbsp = R.replace(/\s/g, ' ');
console.log(
  Setter.over(opticB, nbsp, Setter.over(opticA, nbsp, sig))
);
// map :: Functor f => (a -> b) -> f a -> f b
It would be nice if opticA and opticB could be joined together in some way to make a traversal of both. That way it wouldn't be necessary to call over for each of them.
But I don't know if that's possible...
David Chambers
@davidchambers
Mar 07 2016 16:35
Interesting!