These are chat archives for ramda/ramda

19th
Oct 2017
arian‮
@arian-swydo
Oct 19 2017 09:51
hey, is there a way to avoid defaultTo's behaviour to default null to undefined:
defaultTo(undefined)(null) // -> undefined
Adam Szaraniec
@mimol91
Oct 19 2017 10:40
you can not use it at all (or use R.identity) , I am not sure what you want to achieve
Sean Lindo
@seanlindo
Oct 19 2017 19:37
Given a collection of objects A and a collection of objects B, there are 3 different operations I would like to perform based on the id property in the objects.
  • Get all objects in B that has a person_id that does not exist in A
  • Get all objects in A that has a person_id that does not exist in B
  • If an object exists with a person_id property in both B and A, then pull the objects out of B (as in, they take precedence).
I thought I got a close to a possible solution here, but R.differenceWith only returns one element , like so:
Sean Lindo
@seanlindo
Oct 19 2017 19:44
const A = [ ]

const B = [ 
    {  person_id: 'ABC', job_title: 'chef' },
    {  person_id: 'ABC', job_title: 'teacher' }
]

const cmp = (x, y) => R.path(['person_id'])(x) === R.path(['person_id'])(y) 

const finalCollection = R.differenceWith(cmp)(B, A)
The outcome of this function will only return [{ person_id: 'ABC', job_title: 'chef' }]
But in reality, I wanted it to return both elements in the array. R.differenceWith is not the function for me unfortunately :(
Any idea on what I might be able to use?
Brad Compton (he/him)
@Bradcomp
Oct 19 2017 19:49
I think the issue you have is that differenceWith works with sets, so it removes dupes. Do you have duplicate person_ids in your real data?
Sean Lindo
@seanlindo
Oct 19 2017 19:49
Absolutely, these are representative of resume line items in a database table, so it's natural to have several lines per person_id.
Brad Compton (he/him)
@Bradcomp
Oct 19 2017 19:49
And you want all of them?
Sean Lindo
@seanlindo
Oct 19 2017 19:52
Yes I do. We have a data vendor that delivers silo'd data on a month-by-month basis, so I can't just take the October dataset and say it's representative. I have to compare January with February and so forth from there.
But I can make a pretty good guess, which is why I can say "If this person exists in B but not in A, then give me all of B. If they exist in A but not in B, I'll carry over the old data and take A. If they exist in both A and B, I'll just take the latest, so give me all of B. "
Brad Compton (he/him)
@Bradcomp
Oct 19 2017 19:54
Awesome! Just trying to get the requirements right. Thank you!
Sean Lindo
@seanlindo
Oct 19 2017 19:54
No worries!
Scott Sauyet
@CrossEye
Oct 19 2017 19:54
One good start might be groupBy(prop('person_id'))
nm
Brad Compton (he/him)
@Bradcomp
Oct 19 2017 19:56
:point_up: I think that's a great option actually
Scott Sauyet
@CrossEye
Oct 19 2017 20:01
const byId = groupBy(prop('person_id'))
values(merge(byId(A), byId(B)))
Brad Compton (he/him)
@Bradcomp
Oct 19 2017 20:01
https://goo.gl/EqAm6Y <- One option is to separate the determination of which IDs you want from the picking of the records you need.
So you can do the groupBy call once for each list, and then use those dictionaries to pull what you need
Scott Sauyet
@CrossEye
Oct 19 2017 20:06
Or point-free:
compose(values, useWith(merge, [byId, byId]))
Sean Lindo
@seanlindo
Oct 19 2017 20:06
If groupBy is going to return an object with keys that are person_id, could I technically still use differenceWith ? At this point, I'm not concerned that it's removing duplicates, because my array is just a value. I can get to it later.
Thanks for the point-free example :)
Scott Sauyet
@CrossEye
Oct 19 2017 20:09
I think the pointed version is easier to read, though:
(A, B) => values(merge(byId(A), byId(B)))
Sean Lindo
@seanlindo
Oct 19 2017 20:22
I think I can build on that. Thank you!
Sean Lindo
@seanlindo
Oct 19 2017 20:33
I really like both of these recommendations. Something about the groupBy "clicks" with me.
Sean Lindo
@seanlindo
Oct 19 2017 21:02
OH
merge takes the right object by default
because it's a merge, it also satisfies my other requirement, which is if a key doesn't exist in A or B, I would like to have it
I wish you guys could be here to watch the lightbulb go off.
I will need to look closer into R.useWith, I don't understand it well enough to have come up with this solution without help.
Well, I understand it now.
Apply functions in the argument list to the object passed in, then apply fn last
Jonah
@jonahx
Oct 19 2017 22:09
OT, but because regs here share my taste for simplicity: Anyone use a static site generator they’re actually happy with? When I look at Jekyll and middleman, eg, my immeidate impression is of so much complexity I’d rather hand write plain html.