These are chat archives for ramda/ramda

13th
Jan 2018
Francisco
@franciscotln
Jan 13 2018 21:59 UTC

@isidorosp if you want a pointfree function then:

const parseData = R.pipe(
  R.transpose,
  R.converge(R.map, [ R.pipe(R.head, R.zipObj), R.tail ]),
);

If you prefer a pointer function in the middle:

const parseData = R.pipe(
  R.transpose,
  ([headers, ...data]) => R.map(R.zipObj(headers), data),
);

If you want it in the inverse order, just add a R.reverse at the end of R.pipe.

isidorosp
@isidorosp
Jan 13 2018 22:34 UTC
oh neat
thanks @franciscotln !
how's converge working in that first one?
Francisco
@franciscotln
Jan 13 2018 22:54 UTC

After transposing you again get an array of arrays: the head is an array with the name of columns, the tail is an array of arrays with the data from each column. Then enters R.converge:

You see, map accepts two arguments: a function as first argument and the data to map over (array of arrays).
Converge has some branching functions (those inside the array as you can see, R.pipe(R.head, R.zipObj) and R.tail) << these are the branching functions. When the function returned by converge is called, it applies each branching function to the entry data. After that, the results of these calls are passed to the function you are converging (in our case, R.map). The order is important when you defined the branching functions because they will be passed to R.map in that same order.

Francisco
@franciscotln
Jan 13 2018 23:00 UTC

So it will work like this R.converge(R.map, [ R.pipe(R.head, R.zipObj), R.tail ]):
R.pipe(R.head, R.zipObj) is called with the resulting data from the transposition, so you take the head element, pass it to R.zipObj. But R.zipObj is a curried function that takes two arguments (two arrays to zip) but since we called it only with one argument, we get back a function that is expecting one array. This will be the mapping function.

then the second function inside the branching array is called with the same entry data: R.tail. This will return the same array of arrays but without the first element (that one with the headers). So calling R.tail with our entry data gives back an array and this will be the "new" data that we will map over. This result is then passed to the R.map function as second argument. And that's it :D

It is much simpler in practice than what I wrote, but I wanted to give you a step-by-step response
isidorosp
@isidorosp
Jan 13 2018 23:08 UTC
appreciate the detail :) big help as i'm still getting my head around the whole FP paradigm approach
Francisco
@franciscotln
Jan 13 2018 23:08 UTC
the most important: did you understand it? :D
isidorosp
@isidorosp
Jan 13 2018 23:09 UTC
yeah especially the second comment made it clear
Francisco
@franciscotln
Jan 13 2018 23:10 UTC
ok, I'm glad it helped ;-)
isidorosp
@isidorosp
Jan 13 2018 23:10 UTC
:thumbsup:
spent most of the day tr ying to figure out why Ramda stopped working after i upgraded webpack :sweat:
was convinced I'd screwed something up in my code since I couldn't easily find a similar bug report/issue via googling, but it turns out you need to use import * as R from 'ramda' as opposed to import R from 'ramda'
Francisco
@franciscotln
Jan 13 2018 23:13 UTC
yeap, there's no default export
you have to import everything with an alias
isidorosp
@isidorosp
Jan 13 2018 23:13 UTC
so this was a nice thing to think about after dicking about for two hours
Francisco
@franciscotln
Jan 13 2018 23:14 UTC
ramda/ramda#2322
isidorosp
@isidorosp
Jan 13 2018 23:14 UTC
yeah. initially i wasn't looking at ramda because I thought what was failing was actually the object that I was "ramdaing"
and that it was due to an async/await hiccup between webpack and babel after my upgrades
yeah the reason why it doesn't work makes sense, it's just odd since it used to
Francisco
@franciscotln
Jan 13 2018 23:18 UTC
¯(ツ)/¯