Return to Snippet

Revision: 11116
at January 25, 2009 16:31 by narkisr


Initial Code
-- 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 ((&&&))
import qualified Data.Map as M

clusterBy :: Ord b => (a -> b) -> [a] -> [[a]]
clusterBy f = M.elems . M.map reverse . M.fromListWith (++) . map (f &&& return)

Initial URL


Initial Description
Some really cool Haskell clustering function, the implementation uses Arrows (seen at http://debasishg.blogspot.com/2009/01/seeking-scala-equivalent-for-this.html).

Initial Title
Cool Haskell functional magic (arrows)

Initial Tags


Initial Language
Java