Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
28
Task/Hash-join/Haskell/hash-join-1.hs
Normal file
28
Task/Hash-join/Haskell/hash-join-1.hs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
{-# LANGUAGE LambdaCase, TupleSections #-}
|
||||
import qualified Data.HashTable.ST.Basic as H
|
||||
import Data.Hashable
|
||||
import Control.Monad.ST
|
||||
import Control.Monad
|
||||
import Data.STRef
|
||||
|
||||
hashJoin :: (Eq k, Hashable k) =>
|
||||
[t] -> (t -> k) -> [a] -> (a -> k) -> [(t, a)]
|
||||
hashJoin xs fx ys fy = runST $ do
|
||||
l <- newSTRef []
|
||||
ht <- H.new
|
||||
forM_ ys $ \y -> H.insert ht (fy y) =<<
|
||||
(H.lookup ht (fy y) >>= \case
|
||||
Nothing -> return [y]
|
||||
Just v -> return (y:v))
|
||||
forM_ xs $ \x -> do
|
||||
H.lookup ht (fx x) >>= \case
|
||||
Nothing -> return ()
|
||||
Just v -> modifySTRef' l ((map (x,) v) ++)
|
||||
readSTRef l
|
||||
|
||||
test = mapM_ print $ hashJoin
|
||||
[(1, "Jonah"), (2, "Alan"), (3, "Glory"), (4, "Popeye")]
|
||||
snd
|
||||
[("Jonah", "Whales"), ("Jonah", "Spiders"),
|
||||
("Alan", "Ghosts"), ("Alan", "Zombies"), ("Glory", "Buffy")]
|
||||
fst
|
||||
18
Task/Hash-join/Haskell/hash-join-2.hs
Normal file
18
Task/Hash-join/Haskell/hash-join-2.hs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
{-# 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
|
||||
joined = concat . catMaybes .
|
||||
map (\x -> map (x,) <$> M.lookup (fx x) yMap) $ xs
|
||||
|
||||
test = mapM_ print $ mapJoin
|
||||
[(1, "Jonah"), (2, "Alan"), (3, "Glory"), (4, "Popeye")]
|
||||
snd
|
||||
[("Jonah", "Whales"), ("Jonah", "Spiders"),
|
||||
("Alan", "Ghosts"), ("Alan", "Zombies"), ("Glory", "Buffy")]
|
||||
fst
|
||||
Loading…
Add table
Add a link
Reference in a new issue