emmanueltouzery / prelude-ts

Functional programming, immutable collections and FP constructs for typescript and javascript
ISC License
376 stars 21 forks source link

Allow equality comparer in the Set/Map constructor #50

Open nmunday opened 3 years ago

nmunday commented 3 years ago

There maybe a way already to allow this. I would like to be able to use interfaces for the elements. At the moment I believe that would mean having equals and hashCode on the interface which would then mean creating an instance of the interface would require setting those each time.

If the collection implementations could take an EqualityComparer which has those 2 methods then I believe I could use interfaces.

Another advantage is that I could then have different Equality for the same interface. My main Interface is:

interface Fact { subject: string, property: string , value: string }

Depending on the circumstance any of the properties could be the key or any combination also.

Thanks.

emmanueltouzery commented 3 years ago

It's true that prelude-ts is currently more geared towards objects. Maybe we could add such a feature, depending on how much change would have to be done to the implementation. I'm guessing it wouldn't be too bad, but I don't plan on investing time on this soon.

Maybe you could leverage javascript's prototype nature.. https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_prototypes I didn't think about it too much though.

Sorry for the delay to answer...

ornamentist commented 1 year ago

I think this means that Sets that require custom equality or hash code generation can only be used with classes and methods and not simple objects (for example number[])?

emmanueltouzery commented 1 year ago

Yes, you shouldn't put things like plain arrays. This is covered in the docs: https://github.com/emmanueltouzery/prelude-ts/wiki/Prelude%E2%88%92ts-user-guide#equality

It wouldn't compile in typescript and should throw in JavaScript.