These are chat archives for ramda/ramda

24th
May 2015
Raine Virta
@raine
May 24 2015 18:27
how do you transpose with ramda?
Hardy Jones
@joneshf
May 24 2015 18:52
Something like this, (after cleanup):
var go = R.curry(function(xs, ys) {
  if (ys.length === 0) {
    return R.repeat([], R.max(R.map(R.length, xs)));
  } else {
    return R.zipWith(R.append, ys[0], go(R.tail(ys)));
  }
});
var transpose = function(xs) {
  return go(xs, xs);
};
eh, that max will probably die somehow though.
Raine Virta
@raine
May 24 2015 18:57
should ramda have that if it's so wieldy to implement?
(or some components of what you just wrote that make it easier to build)
Hardy Jones
@joneshf
May 24 2015 18:58
you could clean it up with case
at least visually
var go = R.curry(function(xs, ys) {
  return R.cond(
    [R.isEmpty, R.repeat([], R.max(R.map(R.length, xs)))],
    [R.T,       R.zipWith(R.append, ys[0], go(R.tail(ys)))]
  )(ys);
});
var transpose = function(xs) {
  return go(xs, xs);
};
or something
still pretty complex though
Hardy Jones
@joneshf
May 24 2015 19:05
i think there's a gem hidden in there though
Simon Friis Vindum
@paldepind
May 24 2015 19:13
@CrossEye I just read your comment in the Lodash/Underscore merger thread. I really loved it. And Daltons comment immediately after made me giggle.
Hardy Jones
@joneshf
May 24 2015 19:20
var ap = R.curry(function(f, x) {
  return f(x);
};
var zap = R.zipWith(ap);
var go = R.curry(function(xs, ys) {
  var times = R.max(R.map(R.length, xs));
  return R.cond(
    [R.isEmpty, R.repeat([], times)],
    [R.T,       zap(zap(R.repeat(R.append, times), ys[0]), go(xs, R.tail(ys)))]
  )(ys);
});
var transpose = function(xs) {
  return xs.length === 0 ? [] : go(xs, xs);
};
or so...
not quite as pretty as the haskell, since no infix stuff
could clean that up further with using an array with the other Apply/Applicative instance
then the true case becomes lift2(R.repeat(R.append, times), ys[0], go(xs, R.tail(ys)))
or even further if you have some tail lens.
and a head lens i guess also
Hardy Jones
@joneshf
May 24 2015 19:25
tailLens.map(go(xs), headLens.map(R.append)(ys))
i think
which then becomes:
R.pipe(tailLens.map(go(xs)), headLens.map(R.append))(ys)
and then maybe you change the cond to ifElse and don't even have to mention the ys, so you can move it back into transpose.
i dunno, you'd have to play with it a bit.
Raine Virta
@raine
May 24 2015 21:14