These are chat archives for ramda/ramda

27th
Feb 2019
Robert Mennell
@skatcat31
Feb 27 00:13
reduce into Set of entries keyed by the id, Array.from, map converge zip into objects??
Ben Briggs
@ben-eb
Feb 27 00:57
const combine = R.curry((xs, ys) => {
  const cache = R.reduce((acc, x) => acc.set(x.id, x), new Map(), xs);

  return R.map(y => R.mergeDeepRight(cache.get(y.id), y), ys);
})
Improvements could be made (maybe you'd want a function instead of hardcoded id lookup there)
Lewis
@6ewis
Feb 27 09:53
@Bradcomp is crocks like ramda?
@ben-eb thanks - seems overcomplicated
Ben Briggs
@ben-eb
Feb 27 09:55
@6ewis Please, if you have a better solution go ahead :wink:
Lewis
@6ewis
Feb 27 09:56
@ben-eb i think i do but it's not ramda, pure js
Ben Briggs
@ben-eb
Feb 27 09:57
That's OK too!
Lewis
@6ewis
Feb 27 09:58
what's the equivalent of that myArray.sort((d1, d2) => new Date(d1.date).getTime() - new Date(d2.date).getTime()); in ramda?
Riku Tiira
@rikutiira
Feb 27 10:00

how about this:

const fn = R.pipe(
  R.groupBy(R.prop('id')),
  R.map(R.mergeAll),
  R.values
)

fn([...a1, ...a2])

That would work with arbitrary number of arrays and doesn’t require both arrays to have all ids so it’s a bit more generic

Ben Briggs
@ben-eb
Feb 27 10:01
@rikutiira Ahhh, I forgot about groupBy :)
Riku Tiira
@rikutiira
Feb 27 10:01
or alternative call it with fn([a1, a2]) and add R.unnest to pipe
Lewis
@6ewis
Feb 27 10:04
@rikutiira better than my solution
from an aesthetic stand point
const fn = (firstArray, secondArray) => 
  Object.values(firstArray.concat(secondArray).reduce((acc, next) => {
    acc[next.id] = Object.assign(acc[next.id] || {}, next); 
    return acc
  }, {}));
Riku Tiira
@rikutiira
Feb 27 10:08
to me both are just as fine
Lewis
@6ewis
Feb 27 10:08
sortBy expect one argument, how does that work for comparing differences like dates in ascending order
@rikutiira yours is more readable
i suspect mine is more performant
Riku Tiira
@rikutiira
Feb 27 10:09
yes, I’m sure it is
Ben Briggs
@ben-eb
Feb 27 10:27
@6ewis You could try:
const dateAsc = compose(d => new Date(d).getTime(), prop('date'))

sortBy(dateAsc, xs)