module Control.Concurrent.NanoErl.Examples.Dominos where
import Control.Concurrent.NanoErl
data Fall = Fall
deriving (Show)
numDominos :: Int
numDominos = 5*10^5
main :: IO ()
main = runNanoErl $ do
putStrLn $ "Creating " ++ show numDominos ++ " dominos..."
_ <- spawn $ makeDominos numDominos Nothing
return ()
makeDominos :: Int -> Maybe (Pid Fall) -> Process Fall
makeDominos 0 (Just firstDomino) = \self -> do
putStrLn "Knocking down the first domino..."
firstDomino ! Fall
receive self $ \case
Fall -> putStrLn "You knocked 'em all down!"
makeDominos 0 Nothing =
error "that shouldn't happen"
makeDominos n Nothing = \self -> do
nextDomino <- spawn $ makeDominos (n1) (Just self)
receive self $ \case
Fall -> nextDomino ! Fall
makeDominos n justFirst = \self -> do
nextDomino <- spawn $ makeDominos (n1) justFirst
receive self $ \case
Fall -> nextDomino ! Fall