[Haskell-cafe] Order of Map.fromListWith
mail at nh2.me
Thu Mar 3 17:23:10 UTC 2016
Does anybody know why for fromListWith, the arguments to the combining
function seem flipped?
> import Data.Map
> fromListWith (++) [('a',),('a',)]
I often use it to group things by some key, e.g.
postsByUserId :: Map Int [Int]
fromListWith (++) [ (userId, [postId]) | (userId, postId) <- posts ]
and regularly get tricked by the postIds being reversed in the result.
This is especially unintuitive to me since:
> foldl (++)  [,]
> foldr (++)  [,]
More information about the Haskell-Cafe