These are chat archives for ramda/ramda

14th
Jul 2018
Max
@maxfi
Jul 14 2018 11:06

Hi all! I'm trying to extract a prop from a collection based on props in another collection, and if they aren't found then provide a default. This is what I have so far https://goo.gl/eBrVMW

const col1 = [
  {id: 1},
  {id: 2},
  {id: 3},
]

const col2 = [
  {id: 1, val: 'x'}
]

const findOr = curry((defaultVal, idProp, valProp, xs, id) => pipe(
  find(propEq(idProp, id)),
  propOr(defaultVal, valProp)
)(xs))

const lookup = xs => map(
  pipe(
    prop('id'),
    findOr('n/a', 'id', 'val', xs)
  )
)

lookup(col2)(col1) // => ["x", "n/a", "n/a"]

I've got a feeling that I'm going about this the wrong way though or that a function like this already exists and I can't find it?! Any pointers would be greatly appreciated. Thanks.

trgwii
@trgwii
Jul 14 2018 11:16
Have you looked at zipWith?
Max
@maxfi
Jul 14 2018 11:18
Hmm. Would this work with arrays of objects? It looks like they would need to be of equal length and be sorted… right?
trgwii
@trgwii
Jul 14 2018 11:20
Yeah, you're right, sorry
Max
@maxfi
Jul 14 2018 11:20
Cool. Thanks anyways @trgwii !
trgwii
@trgwii
Jul 14 2018 11:22
```mapIndexed = addIndex(map);
Damn this mobile client!
Max
@maxfi
Jul 14 2018 11:22
:laughing:
Vesa Karvonen
@polytypic
Jul 14 2018 11:32
R.map(R.o(R.propOr('n/a', 'val'), R.o(R.flip(R.find)(col2), R.whereEq)), col1)
Max
@maxfi
Jul 14 2018 11:38
Thanks @polytypic ! Just trying to understand how that works now! đŸ¤ª
@polytypic I came up with the following before, which I think is similar to your suggestion?
const lookupOr = (defaultVal, idProp, valProp, searchArr) => map(
  pipe(
    prop(idProp),
    (id) => pipe(
      find(propEq(idProp, id)),
      propOr(defaultVal, valProp)
     )(searchArr)
  )
)
Bravi
@Bravilogy
Jul 14 2018 18:10
is there a function equivalent to
const toArray = (...args) => args;
?
basically placing everything in an array
I know of places 1 item. but perhaps there's another that does it for a bunch of items
Jacob Bogers
@Jacob_Bogers_twitter
Jul 14 2018 19:10
r.chain(r.append,r.head)([[1],[2],[3]])
>[ [ 1 ], [ 2 ], [ 3 ], [ 1 ] ]
I dont understand the results
Jacob Bogers
@Jacob_Bogers_twitter
Jul 14 2018 19:17
helloo
hi
perkee
@perkee
Jul 14 2018 21:26
@Bravilogy const toArray = unapply(identity) is the best I've come up with