tc39 / proposal-upsert

ECMAScript Proposal, specs, and reference implementation for Map.prototype.upsert
https://tc39.es/proposal-upsert/
MIT License
202 stars 14 forks source link

Polymorhic upsert #15

Closed char0n closed 4 years ago

char0n commented 4 years ago

Hi,

I'm an maintainer of ramda-adjunct functional library and I'm thinking about porting this function into our library. I'm also an Elixir developer and this function makes even more sense comming from Map.update.

Do you think it would make sense to implement a polymorphic upsert function that works on multiple JavaScript types not just on Map? I've described the solution in this issue: https://github.com/char0n/ramda-adjunct/issues/1118

In my mind it makes sense to implement but I've seeking an opinion from original authors of this proposal.

Thank you very much!

ljharb commented 4 years ago

Are you asking if it would make sense in your library, or in the language itself?

While it might make some sense to conflate Map and Set, I don't think including generic iterables, or arrays, or arraylikes, makes sense to me (in the language). Note that if you include iterables or arraylikes, you have to handle strings too, and you can't mutate a string (what would upsert do to a string?).

char0n commented 4 years ago

Sorry for not being clear. For the library only, not generally for JavaScript. The library that I'm working on is functional library - auto-currying, immutability, data last principles applied. New version of input struct is produced in every function call, nothing is ever mutated.

Example function can be concat. It's a polymorphic function that can concat strings and arrays respectively. I'm looking current at polymorphic upsert that will be able to upsert Array, Map, POJO respectively.

ljharb commented 4 years ago

I suppose you could - at that point it's more of a philosophical argument about whether such polymorphic APIs are good :-) but if you believe they are, then what you suggest sounds reasonable to me!

char0n commented 4 years ago

Thank you and happy new year!