These are chat archives for ramda/ramda

12th
Jul 2018
catwen
@wenpengfei
Jul 12 2018 14:24
thanks!! @mimol91 @webduvet
functionalStoic
@functionalStoic
Jul 12 2018 17:13
 *      // Returns `Maybe.Nothing` if the given divisor is `0`
 *      const safeDiv = n => d => d === 0 ? Maybe.Nothing() : Maybe.Just(n / d)
 *
 *      R.traverse(Maybe.of, safeDiv(10), [2, 4, 5]); //=> Maybe.Just([5, 2.5, 2])
 *      R.traverse(Maybe.of, safeDiv(10), [2, 0, 5]); //=> Maybe.Nothing
In the example given for traverse. What rule exactly causes it to return only a Maybe.Nothing but nothing for the other 2 values?
functionalStoic
@functionalStoic
Jul 12 2018 17:18
@lax4mike Thanks!
Mike Lambert
@lax4mike
Jul 12 2018 17:19
what it's essentially doing is mapping over the array and applying the fn to each element in the array, which would results in [Maybe, Maybe, Maybe]. But the behavior of traverse is to flip-flop the types, so instead of an Array of Maybe, it's a Maybe of Array
in the 2nd case, it's [Just, Nothing, Just], so when it flip flips, the Nothing takes precedence
also, it's equivalent to
R.compose(
  R.sequence(Maybe.of),  //=> Just([5, 2.5, 2]))
  R.map(safeDiv(10)) //=> [Just(5), Just(2.5), Just(2)]
)([2, 4, 5]);
functionalStoic
@functionalStoic
Jul 12 2018 17:23
Ah. I see. Thanks for the explanation. I have a direction for digging ;-)
functionalStoic
@functionalStoic
Jul 12 2018 17:31
Where is Maybe.of coming from?
Mike Lambert
@lax4mike
Jul 12 2018 17:31
another library
functionalStoic
@functionalStoic
Jul 12 2018 17:33
Great! Maybe.js and Data.Maybe didn’t seem to have the of method
Mike Lambert
@lax4mike
Jul 12 2018 17:34
yeah, there are a bunch of implemtations
i'm not sure which is best..
functionalStoic
@functionalStoic
Jul 12 2018 17:34
Figured it was a loaded question
keithernet
@keithernet
Jul 12 2018 18:09

Hi everyone, my google fu could just be bad because I'm new to ramda, but I'm hoping you can help. I'm trying to grab a nested property of each property on an object and put them in a list. For example:
{
key1: {key3: {desiredProp: 1}, key4: {desiredProp: 2}},
key2: {key5: {desiredProp: 3}}
}

would return [{key1: [1,2]}, {key2: [3]}].

I know the data structure would be nicer if it was lists already, but I don't have control of that at the moment. Thanks!

Ben Briggs
@ben-eb
Jul 12 2018 18:19
@keithernet
map(
  compose(
    values,
    map(prop('desiredProp'))
  )
)({
key1: {key3: {desiredProp: 1}, key4: {desiredProp: 2}},
key2: {key5: {desiredProp: 3}}
})
keithernet
@keithernet
Jul 12 2018 18:20
that's awesome. thanks @ben-eb!
Ben Briggs
@ben-eb
Jul 12 2018 18:20
No problem :)
@keithernet You might want to clean it up for structures that don't have the desiredProp though
keithernet
@keithernet
Jul 12 2018 18:22
what is values there?
Ben Briggs
@ben-eb
Jul 12 2018 18:23
@keithernet You can paste that into the REPL and it will give you an output
Mike Lambert
@lax4mike
Jul 12 2018 18:23
https://ramdajs.com/docs/#values it transforms an object into an array
Ben Briggs
@ben-eb
Jul 12 2018 18:24
@lax4mike :+1:
keithernet
@keithernet
Jul 12 2018 18:24
Thanks both of you!
keithernet
@keithernet
Jul 12 2018 18:56
Style question. I'd like to get to tuples of the key and values. I know I can use toPairs to get there but is this the typical way that you'd call that:
compose(
  toPairs,
  map(
    compose(
      values,
      map(prop('desiredProp'))
    )
  )
)
Mike Lambert
@lax4mike
Jul 12 2018 18:57
you can also use mapObjIndexed which gives you access to the key and the value
then use R.values
functionalStoic
@functionalStoic
Jul 12 2018 19:16
Here is what I came up with in my own exploration. Not sure it is best https://goo.gl/Z2EQbE
Ben Briggs
@ben-eb
Jul 12 2018 19:59
map(
    compose(
        values,
        mapObjIndexed(
            unapply(compose(adjust(prop('desiredProp'), 0), take(2)))
        )
    )
)({
    key1: { key3: { desiredProp: 1 }, key4: { desiredProp: 2 } },
    key2: { key5: { desiredProp: 3 } },
});
Although I might prefer this mapObjIndexed over the point-free style:
mapObjIndexed((value, key) => pair(prop('desiredProp', value), key))