These are chat archives for ramda/ramda

31st
Oct 2018
Sean Cannon
@SeanCannon
Oct 31 2018 06:16

Hey guys having a brain fart, need to turn this :

const foo = [
  { name : 'ASDF', value : 123, children : [
    { a : 'aaa' },
    { b : 'bbb' }
  ]},
  { name : 'ZXCV', value : 456, children : [
    { a : 'aaa' },
    { b : 'BBB' }
  ]},
  { name : 'ASDF', value : 123, children : [
    { c : 'ccc' },
    { d : 'ddd' }
  ]},
  { name : 'ZXCV', value : 456, children : [
    { a : 'aaa' },
    { g : 'ggg' }
  ]},
];

into this :

{
  ASDF : {
    { name : 'ASDF', value : 123, children : [
      { a : 'aaa' },
      { b : 'bbb' },
      { c : 'ccc' },
      { d : 'ddd' }
    ]}
  },
  ZXCV : {
    { name : 'ZXCV', value : 456, children : [
      { a : 'aaa' },
      { b : 'BBB' },
      { g : 'ggg' }
    ]}
  }
}

Ending a 14 hour code day, brain isn't working anymore, any help is appreciated. Thanks!

Stefano Vozza
@svozza
Oct 31 2018 10:07
have a look at groupBy
Martin Harvan
@kane77
Oct 31 2018 11:04
@SeanCannon will this do the job? https://goo.gl/YPJ2Au
let concatValues = (k, l, r) => k == 'children' ? R.concat(l, r) : l
let mergeConcat = mergeWithKey(concatValues)
let reduceChildren = reduce(mergeConcat, {})

const grouped = groupBy(prop('name'))(foo)
map(reduceChildren)(grouped)
Martin Harvan
@kane77
Oct 31 2018 11:19
sorry.. the previous version has duplicate objects for keys, if you just want unique you can always add uniq(concat(l, r)) or process it in some other way (e.g. mergeAll)
let concatValues = (k, l, r) => k == 'children' ? uniq(R.concat(l, r)) : l
let mergeConcat = mergeWithKey(concatValues)
let reduceChildren = reduce(mergeConcat, {})

const grouped = groupBy(prop('name'))(foo)
map(reduceChildren)(grouped)
Riku Tiira
@rikutiira
Oct 31 2018 11:29
Another way to do same:
R.pipe(
    R.groupBy(R.prop('name')),
    R.map((items) => R.merge(items[0], {
        children: R.uniq(R.unnest(R.pluck('children', items)))
    }))
)(foo)
Sean Cannon
@SeanCannon
Oct 31 2018 13:49
Perfect, thank you @kane77 and @rikutiira !
funfunction
@funfunction
Oct 31 2018 22:16
What would be the best way in Ramda to modify the value of one of the properties in an object, which is in an array of objects, when the index of the object is known.
Normally you could do this, as an example of toggling the value...
obj[i].isDone = !obj[i].isDone
Daniel Gray
@DanielFGray
Oct 31 2018 22:16
lenses
over(lensProp([i]), fn, collection)
Sean Cannon
@SeanCannon
Oct 31 2018 22:17
^ lensIndex(i) even
Daniel Gray
@DanielFGray
Oct 31 2018 22:17
yep, there's lensPath and lensIndex, but i personally just use lensPath which takes an array of each
Brad Compton (he/him)
@Bradcomp
Oct 31 2018 22:18
adjust and update are probably more specific to your use case
adjust really
Although lenses are the more general solution
Daniel Gray
@DanielFGray
Oct 31 2018 22:18
..yeah that's probably the simpler option you're right
Daniel Gray
@DanielFGray
Oct 31 2018 22:26
adjust(not, i, obj[i]) but i think that would return obj[i] rather than obj? over(lensPath([i, 'isDone'], not, obj) i think would be the lens solution
Brad Compton (he/him)
@Bradcomp
Oct 31 2018 22:28
const toggleProp = (prop) => evolve({[prop]: not});
adjust(toggleProp('isDone'), i, arr);
funfunction
@funfunction
Oct 31 2018 22:32
The problem I'm having with these solutions is that the object is being replaced with the boolean value in the new array, instead of the full object with changed boolean value.
Brad Compton (he/him)
@Bradcomp
Oct 31 2018 22:35
Did you try the last one
funfunction
@funfunction
Oct 31 2018 22:38
the last one is just returning a function for me, I haven't troubleshooted it yet
funfunction
@funfunction
Oct 31 2018 23:35
This is the best I've come up with so far....
const obj = R.nth(i, arr)
obj.isDone = !obj.isDone
R.update(i, obj, arr)