Scala has a function groupBy on lists that accepts a function for extracting keys from list items, and returns another list where the items are tuples consisting of
groupBy
Specifically, the following should work:
scalaGroupBy f = groupBy ((==) `on` f) . sortBy (comparing f)
modulo that this doesn't get you the result of f in each group, but if you really need it you can always post-process with
f
map (\xs -> (f (head xs), xs)) . scalaGroupBy f