Proposal: Adding on

Bulat Ziganshin bulat.ziganshin at
Fri Nov 3 11:58:28 EST 2006

Hello Duncan,

Friday, November 3, 2006, 4:42:27 PM, you wrote:
>    groupBy ((==) `on` fst)
> vs groupBy (equating fst)

groupOn fst
sortOn snd

-- |Sort list by function result (use Schwarznegian transform)
sortOn  f  =  map snd . sortOn' fst . map (keyval f)

-- |Sort list by function result (don't use Schwarznegian transform!)
sortOn' f  =  sortBy (map2cmp f)

-- |Group list by function result
groupOn f  =  groupBy (map2eq f)

-- |Sort and Group list by function result
sort_and_groupOn  f  =  groupOn f . sortOn  f
sort_and_groupOn' f  =  groupOn f . sortOn' f

-- |Check that all adjacent values in the list meets given criteria
isAll f []       = True
isAll f [x]      = True
isAll f (x:y:ys) = f x y  &&  isAll f (y:ys)

-- |Check that list is sorted by given field/critery
isSortedOn f  =  isAll (<=) . map f

-- |Check that all elements in list are equal by given field/critery
isEqOn f      =  isAll (==) . map f

-- Utility functions for list operations
keyval  f x    =  (f x, x)                -- |Return pair containing computed key and original value
map2cmp f x y  =  (f x) `compare` (f y)   -- |Converts "key_func" to "compare_func"
map2eq  f x y  =  (f x) == (f y)          -- |Converts "key_func" to "eq_func"

while we are here, i also propose to export from Data.List 'merge'
function that is useful on its own

Best regards,
 Bulat                            mailto:Bulat.Ziganshin at

More information about the Libraries mailing list