Skip to content

Instantly share code, notes, and snippets.

@chadaustin
Created January 29, 2016 02:12
Show Gist options
  • Select an option

  • Save chadaustin/465acf1f1c5680cfe4f4 to your computer and use it in GitHub Desktop.

Select an option

Save chadaustin/465acf1f1c5680cfe4f4 to your computer and use it in GitHub Desktop.
{-# LANGUAGE ExistentialQuantification, LambdaCase #-}
import Data.IORef
import Control.Monad
data Mover = forall a. Mover (IORef [a]) (IORef [a])
doMove :: Mover -> IO ()
doMove (Mover left right) = do
readIORef left >>= \case
[] -> return ()
(x:xs) -> do
writeIORef left xs
modifyIORef right (x:)
main = do
a1 <- newIORef ["foo", "bar"]
a2 <- newIORef []
b1 <- newIORef [1, 2, 6]
b2 <- newIORef []
let movers :: [Mover]
movers = [Mover a1 a2, Mover b1 b2]
forM_ movers doMove
readIORef a1 >>= putStrLn . show
readIORef a2 >>= putStrLn . show
readIORef b1 >>= putStrLn . show
readIORef b2 >>= putStrLn . show
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment