These are chat archives for ramda/ramda

17th
Sep 2018
Baqer Mamouri
@bmamouri
Sep 17 2018 07:45

I am wondering how can I go through a list of objects and change only only object that met a predicate?

stuff = [
  {id: 1, name: 'A'},
  {id: 2, name: 'B'},
]

My first attempt was to do this:

R.pipe(
  R.find(R.propEq('id', 2)),
  R.assoc('realtime', null)
)
Alex Deas
@alex-deas_gitlab
Sep 17 2018 07:46
@bmamouri You can just map over the list and based on some logic either return the value or change it in some way
R.map(x => x.id === 1 ? { …x, is1: true } : x)
Philipp Wille
@Yord
Sep 17 2018 07:50
Same process as @alex-deas_gitlab using when:
const pred = propEq('id', 1)
const action = assoc('n', 42)

map(when(pred, action), stuff)
Baqer Mamouri
@bmamouri
Sep 17 2018 07:50

Thank you @Alex,

I tried to use map with Ramda functions:

R.pipe(
  R.map(
    R.ifElse(
      R.equals(1, R.prop('id')),
      R.assoc('realtime', null),
      R.identity
    )
  )
)

But it does not work because I think I am providing two values to R.equals. But I am not sure how to give only one.

Alex Deas
@alex-deas_gitlab
Sep 17 2018 07:52
you’d need to use propEq or where instead to check the properties of an object
Philipp Wille
@Yord
Sep 17 2018 07:52
@bmamouri I took the freedom to answer seconds before you asked the question ;)
Baqer Mamouri
@bmamouri
Sep 17 2018 07:53
Amazing @Yord. Thank you very much for the snippet. That was really pretty! <3
Alex Deas
@alex-deas_gitlab
Sep 17 2018 07:53
or rewrite the check to R.both(R.always(1), R.prop(“id”))
Baqer Mamouri
@bmamouri
Sep 17 2018 07:54
Ahhh now I understand what always is used for! I already learned a few things. Thanks again Alex
Julien Goux
@jgoux
Sep 17 2018 14:04
Hello all
Is there a function to go from : [{ a: 1, b: 2}, { a: 2, b: 3}] to {a: [1, 2], b:[2, 3]} ?
A kind of accumulation of values by keys, given an array of homogenous objects
I can reduce it but I was curious if there is a premade function :D
matrixbot
@matrixbot
Sep 17 2018 14:06
Sgus Julien Goux (Gitter): checkout mergeWith if it helps you
https://ramdajs.com/docs/#mergeWith
Brad Compton (he/him)
@Bradcomp
Sep 17 2018 14:09
groupBy will almost get you there
Julien Goux
@jgoux
Sep 17 2018 14:13
@Bradcomp I don't see how, as the groupBy receive the whole object, and I need to accumulate all the values for each keys
Honestly if there isn't a premade function, the reduce way will be the most simple in my case
Thanks anyway Brad!
Philipp Wille
@Yord
Sep 17 2018 14:18
This depends on the properties of the problem
The following assumes both objects always have the same keys and no key is an array:
const list = [{ a: 1, b: 2}, { a: 2, b: 3}]
mergeWith(Array.of, ...list)
(which uses @matrixbot proposed solution)
If you assume that some keys of one object are not present in the other or if you wish to concatenate lists you have to adjust accordingly
this is what I did
oh
your solution is neat :D
Galileo Sanchez
@galileopy
Sep 17 2018 14:22
It would be nice to have a debugging tool that can show you how your data is being transformed over time
Philipp Wille
@Yord
Sep 17 2018 14:24
"problem" with mergeWith is that it only triggers on keys that are present in both objects
(which is intended behaviour of course)
Julien Goux
@jgoux
Sep 17 2018 14:25
@Yord They are, it's for bunch loading a directory exporting always the same things
Philipp Wille
@Yord
Sep 17 2018 14:25
@jgoux Oh neat, that simplifies things ;)
Julien Goux
@jgoux
Sep 17 2018 14:25
Thanks for the help Brad and Philipp !
I'm doing a custom require for a graphql folder :)
Philipp Wille
@Yord
Sep 17 2018 14:26
You're very welcome!