These are chat archives for ramda/ramda

25th
Mar 2019
Zephilim
@Zephilim
Mar 25 18:21
Hi. I'm having a few problems using R.reduce/R.assoc/R.prop to turn an array of objects (each with key & value pairs) into another single object, with all those key value pairs populated on it. Can someone take a look? Ta:
https://goo.gl/oPqPDy
Brad Compton (he/him)
@Bradcomp
Mar 25 18:30

const attributeReducer = R.assoc(R.prop('name'), R.prop('value'));

This won't do what you are expecting it to do

assoc is expecting two strings as its first two arguments, but you are providing it with functions
Zephilim
@Zephilim
Mar 25 18:32
Arg ok, let me look at that again then
Brad Compton (he/him)
@Bradcomp
Mar 25 18:32
Check out converge
Zephilim
@Zephilim
Mar 25 18:33
Arg, but thats what I thought it was supposed to do. When you invoke R.prop('name'), that returns a string right?
You will see that in my intermediate values!
Brad Compton (he/him)
@Bradcomp
Mar 25 18:33
R.prop('name') returns a function that can be invoked with an argument
but assoc won't execute that function on the input, it will simply take the function and convert it to a string
Zephilim
@Zephilim
Mar 25 18:34
Ok, so how do i get it it to be invoked with the underlying object from the collection?
Brad Compton (he/him)
@Bradcomp
Mar 25 18:36

check out converge

converge(objOf, [prop('name'), prop('value')])({name: 'a', value: 1}); // {a: 1}

Zephilim
@Zephilim
Mar 25 18:37
Ok thanks, I'll take a look
Brad Compton (he/him)
@Bradcomp
Mar 25 18:44
Is it necessary to use reduce? I think the problem is a lot easier with a few other functions if you're interested in alternative solutions
Zephilim
@Zephilim
Mar 25 18:46
You earlier solution doesnt work, because the input is not a an array of objects - ({name: 'a', value: 1})
Ok, I'm willing to find an alterative solution, but 'reduce' seemed like the right function
I was using R.assoc, because thats the way to push an attribute onto an object
Brad Compton (he/him)
@Bradcomp
Mar 25 18:49
My earlier suggestion was just an example, not a solution
That was my first thought for a solution
Zephilim
@Zephilim
Mar 25 18:56
Ok, thats just what I needed. Just need to figure out where I went wrong. Many thanks
Brad Compton (he/him)
@Bradcomp
Mar 25 18:56
Sure
:bowtie:
Zephilim
@Zephilim
Mar 25 18:57
I guess it helps when you know every function in the ramda api and have detailed knowledge of them all, so you pick the right solution. I'm getting there but slowly!
Brad Compton (he/him)
@Bradcomp
Mar 25 18:58
Yeah, there are a lot of functions there for sure!