Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Point-free for filter function

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 = ??
like image 673
JoeChoi Avatar asked Dec 14 '22 13:12

JoeChoi


2 Answers

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

findMinimum = filter =<< (. fst) . (==) . minimum . map fst
like image 163
chi Avatar answered Dec 28 '22 15:12

chi


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.

like image 35
karakfa Avatar answered Dec 28 '22 15:12

karakfa