/ Published in: Java
Some really cool Haskell clustering function, the implementation uses Arrows (seen at http://debasishg.blogspot.com/2009/01/seeking-scala-equivalent-for-this.html).
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
-- Uasge *Main> let antwords = words "the tan ant gets some fat" *Main> clusterBy length antwords [["the","tan","ant","fat"],["gets","some"]] *Main> clusterBy head antwords [["ant"],["fat"],["gets"],["some"],["the","tan"]] *Main> clusterBy last antwords [["the","some"],["tan"],["gets"],["ant","fat"]] -- Source import Control.Arrow ((&&&)) clusterBy :: Ord b => (a -> b) -> [a] -> [[a]] clusterBy f = M.elems . M.map reverse . M.fromListWith (++) . map (f &&& return)