sanctuary-js / sanctuary

:see_no_evil: Refuge from unsafe JavaScript
https://sanctuary.js.org
MIT License
3.03k stars 94 forks source link

logic: remove S.allPass and S.anyPass #633

Closed davidchambers closed 5 years ago

davidchambers commented 5 years ago

I've never been happy with these functions, but until now I've been unable to explain my discomfort.

Consider the following expressions:

S.allPass (preds) (x)
S.anyPass (preds) (x)

My first idea was to fold the predicates into a single predicate:

S.reduce (p => q => x => p (x) && q (x)) (S.K (true)) (preds) (x)
S.reduce (p => q => x => p (x) || q (x)) (S.K (false)) (preds) (x)

If one is willing to unconditionally apply every predicate, one can replace the lambdas:

S.reduce (S.lift2 (S.and)) (S.K (true)) (preds) (x)
S.reduce (S.lift2 (S.or)) (S.K (false)) (preds) (x)

Only advanced Sanctuary users would think of the expressions above; I don't consider them suitable substitutes for S.allPass (preds) (x) and S.anyPass (preds) (x).

My second idea was simpler:

S.all (S.T (x)) (preds)
S.any (S.T (x)) (preds)

The expressions above read well to me. When applied to x, do all of the predicates return true? When applied to x, do any of the predicates return true? This is exactly how I read the expressions featuring S.allPass and S.anyPass. :)