gigobyte / purify

Functional programming library for TypeScript - https://gigobyte.github.io/purify/
ISC License
1.5k stars 58 forks source link

EQ Combinator #628

Closed TheWix closed 1 year ago

TheWix commented 1 year ago

Heya,

I was wondering if there has been any thought about adding something like the Eq Combinator seen in fp-ts. The idea of building up a complex equality function from smaller ones is really appealing to me due to the amount of inline equality logic I see in enterprise software. It would look something like this:

const eqUser = Eq.struct<User>({
  id: eqString,
  name: eqName,
  address: eqAddress
});

eqUser.equals(user1, user2);

You could also fit it into future list operations:

declare const myList: List<User>;
declare const myList2: List<User>;

myList.difference(eqUser, myList2)
myList.union(eqUser, myList2);
myList.intersect(eqUser, myList2);
myList.unique(eqUser);
//...etc

EDIT: I'd also be happy at taking a stab at a PR for this, if you like the idea

gigobyte commented 1 year ago

The Records and Tuples proposal is at stage 2 at the moment, considering that and also the fact that learning a completely new API (the Eq Combinator) just to compare DTOs is not worth the effort in my opinion, I'm not convinced yet.

Leaving this issue open in case anyone else wants to chime in

TheWix commented 1 year ago

Never knew about that new proposal. Happy to see it.

also the fact that learning a completely new API (the Eq Combinator) just to compare DTOs is not worth the effort in my opinion, I'm not convinced yet.

These aren't just DTOs I am talking about. The issues around equality, especially entity types are many. Equality can mean different things in different contexts. We have cases in our codebase where we want to compare all the properties in an object, some cases where we want to compare a single property like an id, and some cases where we want to compare just a few properties (type and id, for example). This has led to many different practices for dealing with equality. In a microservice, this isn't too big of a problem, but in monoliths and mini-monoliths this problem gets out of hand quickly.

When there is a common pattern and functions that use that pattern (such as array functions accepting an Eq) it incentivizes people to use a single pattern.

That being said, I understand wanting to keep the library small.