Point-free for filter function

Viewed 299

Does there exist a point-free function for filter function to find minimum of first element of pair in a list? for example:

findMinimum xs =  filter ((== minimum (map fst xs)) . fst ) xs

-- example:
findMinimum [(0, 0), (0, 1), (2, 2), (3, 2), (1, 4)] = [(0, 0), (0, 1)]

How to convert findMinimum function to point-free:

findMinimum = ??
3 Answers

pointfree.io outputs this, which is not too bad. I still prefer the original code, though.

findMinimum = filter =<< (. fst) . (==) . minimum . map fst

a different implementation

head . groupBy ((==) `on` fst) . sortOn fst

sort and group by first, pick the first sub list. Perhaps you may want to handle empty list explicitly.

Putting the pair in Arg, you get ordering on the first element, that you can exploit as follows:

import Data.Semigroup (Arg(..))
import Data.Ord (comparing)

findMinimum :: Ord a => [(a, b)] -> (a, b)
findMinimum = minimumBy (comparing (uncurry Arg))
Related