These are chat archives for ramda/ramda

12th
Apr 2019
Matt Bond
@zxaos
Apr 12 20:51

I'm trying to learn lenses, but am having trouble composing them.

For example, if I want to partition by a specific prop being over 0, I try:
const splitByNegative = R.partition(R.gte(R.view(R.lensProp('someValue'), 0)) it doesn't split like I expect. Where am I going wrong?

Ben Briggs
@ben-eb
Apr 12 21:24
@zxaos You're nearly there. The problem is that your input to gte is a function where it should be a number
There's two solutions I can see, one would be the simpler route of using propSatisfies, and the other would be using composition to sort out gte's arguments. But as I reversed the order of these I used lte instead:
const x = R.partition(
  R.propSatisfies(R.lte(0), 'someValue')
)

const y = R.partition(
  R.compose(R.lte(0), R.view(R.lensProp('someValue')))
)
(you could use the __ function instead but I don't like it as much.. ala R.gte(R.__, 0))
Matt Bond
@zxaos
Apr 12 21:32
Ok, I did try R.__ before posting, and ended up with fn is not a function
(const splitByNegative = R.partition(R.gte(R.view(someLens, R.__), 0)))
And I'm hoping to use a lens instead of a prop string so I can pass different lenses in
Ben Briggs
@ben-eb
Apr 12 21:34
@zxaos It's got the same problem :)
You could make it generic by const z = lens => partition(compose(lte(0), view(lens)))
Matt Bond
@zxaos
Apr 12 21:36
So the compose fixes it, because the data is evaluated for R.view first and then that resolves into a number so partition can work?
Ben Briggs
@ben-eb
Apr 12 21:37
It resolves into a number so gte can work
Matt Bond
@zxaos
Apr 12 21:37
ah, right
Ben Briggs
@ben-eb
Apr 12 21:37
In your original example you called gte with a function and then a number
Matt Bond
@zxaos
Apr 12 21:38
Thanks very much!
Ben Briggs
@ben-eb
Apr 12 21:38
No worries!