These are chat archives for ramda/ramda

4th
Oct 2018
Pedro Ávila
@avilapedro
Oct 04 2018 02:30 UTC

@Bradcomp

That's a great point, but I think it's pretty common in JavaScript to have arrays of "unique" objects mainly as results of API calls, and also from application state. But definitely removeFirstOccurrenceOf would be more appropriate considering that it is intended to be used on arrays and arrays aren't unique, removeBy for arrays would be the same as reject.

indexBy followed by dissoc looks great, I liked the approach using lenses, very cool! However, I think most people when finding themselves in this case, would first think about a removeFirstOccurrenceOf function as a tool for solving their problem, I'd say that it's more intuitive.

Do you think there's space in Ramda for adding such a function?

Julien Gonzalez
@customcommander
Oct 04 2018 12:36 UTC

As we just saw, there are many ways to implement that function. How could Ramda come up with a one-size-fits-all solution without compromising too much on performance or without making too many assumptions?

Personally I like that Ramda has a limited but effective set of low-level building blocks. Developers have then the freedom to design more sophisticated functionalities in ways that suit them the best.

My £0.02 :-)

Jacob Bogers
@Jacob_Bogers_twitter
Oct 04 2018 16:48 UTC
muh
Pedro Ávila
@avilapedro
Oct 04 2018 17:30 UTC

@customcommander

there are many ways to implement that function

Just as any function on Ramda.

How could Ramda come up with a one-size-fits-all solution

Fits all? There's only one use case to be fitted: remove the first item that matches a predicate.

without compromising too much on performance

There's no performance concern if it is implemented by iterating only until the predicate is matched.

or without making too many assumptions?

What assumptions are there to make? It's a limited scope function.

Personally I like that Ramda has a limited but effective set of low-level building blocks.

I wouldn't say it is limited, but of course, that is subjective to one's perspective.

Developers have then the freedom to design more sophisticated functionalities in ways that suit them the best.

I agree! But I don't think removing the first item that matches a predicate is a "more sophisticated" functionality. In fact I think it's a common use case, but maybe I'm wrong on that.

Thank you for your £0.02, in fact your time is probably more valuable, so thank you for taking it!

Kurt Milam
@kurtmilam
Oct 04 2018 17:42 UTC

In the described case, I generally convert the array of objects to an object of objects keyed by the unique id, e.g.:

const xs = [{id:'a', x:1}, {id:'b', x:2}, {id:'c', x:3}, {id:'d', x:4}, {id:'e', x:5}]
const xsById = {a:{id:'a', x:1}, b:{id:'b', x:2}, c:{id:'c', x:3}, d:{id:'d', x:4}, e:{id:'e', x:5}}

This makes a whole lot of operations much easier to perform, for instance omit(['b'], xsById).

Matthew Willhite
@miwillhite
Oct 04 2018 19:33 UTC
I’ve been doing that a lot with arrays I have to deal with too…it can have a really big performance impact (positive).
Matthew Willhite
@miwillhite
Oct 04 2018 19:47 UTC
and it just so happens that ramda has indexBy :)
To borrow @kurtmilam’s examples…
// Earlier in the program
const xs = indexBy (prop (‘id’)) ([{ id: ‘a’, x: 1 }, /*… the rest */]);
// Later
const withoutB = omit ([‘b’]) (xs);
Julien Gonzalez
@customcommander
Oct 04 2018 20:02 UTC
@avilapedro Cool, thx.
Pedro Ávila
@avilapedro
Oct 04 2018 20:53 UTC
So what you're suggesting is rather than creating a tool for solving one use case, we should get rid of such use case?
Julien Gonzalez
@customcommander
Oct 04 2018 21:42 UTC
Not at all. I don’t think I have ever suggested that? My point was simply that since these functions are specialised and simple to build (YMMV), I’d let people build whatever they want however they want instead of cluttering the library.
I really didn’t mean to turn this into a debate or be interpreted as some sort of criticism. (Apologies if you felt that way.) Don’t let me be in your way of contributing to the project ;)
Pedro Ávila
@avilapedro
Oct 04 2018 21:57 UTC

@customcommander Oh I'm sorry I didn't mention who I was talking to, when I asked that, I was talking to @kurtmilam and @miwillhite, and to clarify, when I said "get rid of such use case" I was referring to storing elements as objects with ids inside an array.

And don't worry, I see this as an healthy and enlightening discussion, I'm really considering to just replace arrays to objects with keys as ids.

Matthew Willhite
@miwillhite
Oct 04 2018 22:56 UTC
It really depends on how you plan to access the elements in the array. If you find yourself looking to find a single element in the array often then I think that could imply a dictionary structure may be more suitable
Pedro Ávila
@avilapedro
Oct 04 2018 23:01 UTC
Makes sense