RosettaCodeData/Task/Hash-join/Haskell/hash-join-2.hs

19 lines
564 B
Haskell
Raw Permalink Normal View History

2015-02-20 09:02:09 -05:00
{-# LANGUAGE TupleSections #-}
import qualified Data.Map as M
import Data.List
import Data.Maybe
import Control.Applicative
mapJoin xs fx ys fy = joined
where yMap = foldl' f M.empty ys
f m y = M.insertWith (++) (fy y) [y] m
2016-12-05 22:15:40 +01:00
joined = concat .
mapMaybe (\x -> map (x,) <$> M.lookup (fx x) yMap) $ xs
2015-02-20 09:02:09 -05:00
2016-12-05 22:15:40 +01:00
main = mapM_ print $ mapJoin
2015-02-20 09:02:09 -05:00
[(1, "Jonah"), (2, "Alan"), (3, "Glory"), (4, "Popeye")]
snd
[("Jonah", "Whales"), ("Jonah", "Spiders"),
("Alan", "Ghosts"), ("Alan", "Zombies"), ("Glory", "Buffy")]
fst