samhh / fp-ts-std

The missing pseudo-standard library for fp-ts.
https://samhh.github.io/fp-ts-std/
MIT License
207 stars 27 forks source link

Feature Request: `minimumBy` and `maximumBy` #161

Closed imcotton closed 1 year ago

imcotton commented 1 year ago

Addition features next to minimum and maximum, something like:

:: (a -> b) -> Ord b -> ReadonlyNonEmptyArray a -> a
samhh commented 1 year ago

Looks solvable with Ord.contramap?

As an aside I believe the Haskell convention of a "by" suffix is where an Ord constraint is omitted and a predicate or comparison or what have you is supplied directly instead.

imcotton commented 1 year ago

Yes I was thinking about contramap, however to follow Haskell's convention, do you think it's also necessary to provide

comparing :: (a -> b) -> Ord b -> (a -> a -> Ordering)

as packed deal to compose with

maximumBy :: (a -> a -> Ordering) -> ReadonlyNonEmptyArray a -> a

In this way it aligns to Haskell, but somehow skipped contramap? Or it's actually just be OK?

samhh commented 1 year ago

If I'm understanding correctly:

maximum(Ord.contramap(f)(o)) = maximumBy(comparing(f)(o))

I personally never interact with Ordering except in Ord definitions. contramap is also more general as it covers all functions taking Ord, whereas a "By" convention via comparing requires a new "By" function for each use case (e.g. maximumBy instead of reusing maximum).

imcotton commented 1 year ago

You are right, on the second thought by having maximum alongside with contramap is sufficient enough to cover the usage, I was getting a blind angle between two systems, thanks for help to clarify.