These are chat archives for ramda/ramda

31st
Jan 2019
Edouard Hienrichs
@edouardhienrichs
Jan 31 06:51
I have the feeling there is a better "ramda way" to write that line but I was not able to figure that out:
const isNonNegativeInt = R.and(R.gte(0), (nbr) => R.identical(Math.floor(nbr), nbr))
pravinsakthivel
@pravinsakthivel
Jan 31 07:21
Hi
can i integrate Rambda JS with Oracle JET framework ?
Julien BONNIN
@Powerplex
Jan 31 15:59
Hello everyone :) I wanted to unify the way I update an object by using only lenses to reach for props/paths. Is it possible to "dissoc" with a lens ?
Maxime ROBIN
@Waxo
Jan 31 16:06
non, it doesn't make sense I think since an Lens is here for setting up external "getters/setters" and the purpose of dissoc is to erase something not modifying it
Julien BONNIN
@Powerplex
Jan 31 16:06
@edouardhienrichs A good way would be to decouple your functions to keep things clear:
const isNonNegative = gte(__, 0) // be careful with the order here !
const isInteger = n => Math.floor(n) ===  n
const isNonNegativeInt = both(isNonNegative, isInteger)

isNonNegativeInt(-1) // false
isNonNegativeInt(0) // true
OK @Waxo but then if my code looks like this :
const categoryLens = lensProp('category')
const setCategory = set(categoryLens)
const removeCategory = dissoc('category') // I don't want tu re-use the same string here :(

// Do I also need to put props/paths into constants ? :(

const defaultSearch = {
    filter: true,
    category: 78
}

const newSearch = removeCategory(defaultSearch)
newSearch
Maxime ROBIN
@Waxo
Jan 31 16:12
maybe use assoc
const catProp = 'category';

R.pipe(
  R.assoc(catProp, 'your data');
  R.dissoc(catProp);
)(defaultSearch);
Julien BONNIN
@Powerplex
Jan 31 16:17
In my case I would just need to remove the key, this is an object I retrieve from a legacy codebase and I just want to clean it before using it. In my case I have dozens of getter/setters based on Lenses (so there const for my props/paths because I compose lenses together to avoid repetition). But then when I need to remove a prop/path, this is the only case not covered with Lenses so I need to start putting all my props/paths into constants, but then I lose the advantage of lenses composition. :( But thanks for the help :D
Riku Tiira
@rikutiira
Jan 31 16:19
Ramda's lenses are pretty simple, what you could do is R.set(lens, undefined, object) and treat undefined as non-existing values, or explicitly doing R.reject(R.isNil, o) afterwards
If you want a more robust lens library for JS, I recommend taking a look at partial.lenses: https://github.com/calmm-js/partial.lenses
Brad Compton (he/him)
@Bradcomp
Jan 31 16:19
:point_up_2: I was about to suggest Partial.Lens
It has L.remove which does what you want it to
Riku Tiira
@rikutiira
Jan 31 16:20
It treats undefined as non-existing value so with that you can do L.set(lens, undefined, o) and it removes the property altogether
and L.remove is shorthand for that operation
Julien BONNIN
@Powerplex
Jan 31 16:21
I'll have a look at this ! Thanks. I thought about the reject isNil approach but I would need to make it recursive for deep objects and it could be bad for performance. This library might be the solution :D I just had a hard time so that my team accept replacing Lodash with Ramda :D I am not sure they will understand that we need yet another library ahah
Julien BONNIN
@Powerplex
Jan 31 16:26
But then if lenses libraries offer this feature, why not Ramda ?
Brad Compton (he/him)
@Bradcomp
Jan 31 16:28
Ramda's support for lenses is fairly rudimentary.
Riku Tiira
@rikutiira
Jan 31 16:28
@Powerplex you could also do something like this if you have deep objects:
const o = { category: 'test', keep: true, path: { to: { foo: 'foo', bar: 'bar' } } }
const l = ['path', 'to', 'foo']

R.over(R.lensPath(R.init(l)), R.omit([R.last(l)]), o)
Julien BONNIN
@Powerplex
Jan 31 16:28
We have set, over and view, why not adding delete ? (I see remove already exists but not to be used with lenses)
Riku Tiira
@rikutiira
Jan 31 16:28
it's a bit roundabout but does what you want
Julien BONNIN
@Powerplex
Jan 31 16:45
Thanks. I discussed with my team and we decided to remove lenses entirely, as we don't want another library for now. we are going to store props/paths in constants, and directly use methods with them (assoc/dissoc, etc)
Riku Tiira
@rikutiira
Jan 31 16:46
Yes, dissoc is definitely cleaner solution than that with ramda
although I don't think you should rule out lenses entirely, for example modifying a deeply nested object with R.over can be cleaner than using the alternative of R.evolve
Julien BONNIN
@Powerplex
Jan 31 17:06
Or :
Julien BONNIN
@Powerplex
Jan 31 21:05
(and you are right, I will NEVER use evolve because it requires to imitate/know the structure of the object, and if the structure of the object changes then you need to rewrite all your use of 'evolve' too)
Still ramda is my favourite library <3
Ben Briggs
@ben-eb
Jan 31 21:58

I've found lenses to be really good at mapping data from one format to another, but also some lenses can be reversed. e.g. using partial lenses:

const lens = L.pick({ email: 'from' });

L.get(lens, { from: 'x' }) //=> { email: 'x' }
L.getInverse(lens, { email: 'x' }) // => { from: 'x' }

This for me is much more compelling as the lens in this example acts as a single source of truth for converting data e.g. in a database to an api and vice versa