Point-free for filter function

403 Views Asked by At

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

There are 3 best solutions below

3
On BEST ANSWER

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.

3
On

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

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

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))