These are chat archives for ramda/ramda

20th
Dec 2015
@CrossEye yeah, she's one of my idols.
@scott-christopher isn't that what om does?
also I haven't looked into it well enough but this as well: https://github.com/zrho/purescript-optic-ui
Scott Christopher
@scott-christopher
Dec 20 2015 00:05
Perhaps I already had seen this and dreamed it up as an invention of my own... :D
Hardy Jones
@joneshf
Dec 20 2015 00:05
heh
Scott Christopher
@scott-christopher
Dec 20 2015 00:06
purescript-optic-ui is pretty much on par with what I was thinking.
Tamas
@bling5630
Dec 20 2015 11:10

Hey Rams,
I would like refactor this code, can you help me how can I cut the doubled maps right here and write a short code,
Thanks,
Tanas

var matrix = function (number) {
  return _.map(function (row) {
    return _.map(function (column) {
      return column[row];

    }, BASE_DATA);

  },_.keys(BASE_DATA[0]));
};

var BASE_DATA = [
  [1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16],
];

console.log(matrix(BASE_DATA));

outcome is:

[ [ 1, 5, 9, 13 ],
[ 2, 6, 10, 14 ],
[ 3, 7, 11, 15 ],
[ 4, 8, 12, 16 ] ]

Raine Virta
@raine
Dec 20 2015 11:25
@bling5630, #1526 will introduce transpose
Tamas
@bling5630
Dec 20 2015 11:41
thanks @raine I will check it
Hardy Jones
@joneshf
Dec 20 2015 15:45
@bling5630 as a note, if you find yourself using the indices of an array as an integral part of the logic, there's usually a better way to do what you want. You almost never need to use the actual indices of an array, and your code becomes simpler when you move away from that.
Not ramda, but the idea should be transferable:
λ: let transpose' = foldr (zipWith (:)) (repeat [])
λ: transpose' [[1,2,3,4], [5,6,7,8]]
[[1,5],[2,6],[3,7],[4,8]]
λ: transpose' [[1,2,3,4], [5,6,7,8], [9,10,11,12]]
[[1,5,9],[2,6,10],[3,7,11],[4,8,12]]
λ: transpose' [[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16]]
[[1,5,9,13],[2,6,10,14],[3,7,11,15],[4,8,12,16]]
Hardy Jones
@joneshf
Dec 20 2015 15:55
function transpose(xs) {
  const shortest = Math.min.apply(null, R.map(R.length, xs));
  return R.reduce(R.zipWith(R.flip(R.append)), R.repeat([], shortest), xs);
}
Scott Sauyet
@CrossEye
Dec 20 2015 16:27

Saw the Haskell, went to try it in JS. Found I'm beat to it. Mine was similar, though:

var transpose = R.lift(R.reduce(R.zipWith(R.flip(R.append))))(
   R.pipe(R.map(R.length), R.apply(Math.min), R.repeat([])), R.identity
);

Several variants (including old ones from #1526 and earlier discussions) are at http://bit.ly/1YpsALP

Using shortest:
var transpose = R.lift(R.reduce(R.zipWith(R.flip(R.append))))(
  R.compose(R.repeat([]), shortest), 
  R.identity
);
Tamas
@bling5630
Dec 20 2015 16:43
hello Guys, thanks again, this is a really good help for my start because I would like to solve the 'Print out a 2-D array in spiral order' , Tamas
Kurtis Rainbolt-Greene
@krainboltgreene
Dec 20 2015 21:23
I need to find with default, but find doesn't take a default option. Is there another way to achieve this without memoizing & if condition?
Rather, is there a "ramda way"?
Asaf
@asaf-romano
Dec 20 2015 21:24
pipe(find(...), defaultTo(...)) ?
Kurtis Rainbolt-Greene
@krainboltgreene
Dec 20 2015 21:25
Ah-ha!
Thanks @asaf-romano :)
Asaf
@asaf-romano
Dec 20 2015 21:25
np
either(find(...), always(...)) works too, but you're not supposed to know that :)
Kurtis Rainbolt-Greene
@krainboltgreene
Dec 20 2015 21:31
Haha, which is preferred?
Asaf
@asaf-romano
Dec 20 2015 21:36
matter of taste, i guess (in particular, whether or not you like the behavior of || in js). find(..., defaultTo(...)) is very symmetric to your question phrasing though.
pipe(find, defaultTo), that is.
Kurtis Rainbolt-Greene
@krainboltgreene
Dec 20 2015 21:37
Gotcha. Should it be pipe(find(fn)(list), defaultTo(item))?
Yes.
Asaf
@asaf-romano
Dec 20 2015 21:41
pipe(find(fn), defaultTo(item))(list)