Created
July 31, 2014 13:19
-
-
Save blitzcode/7c63533b247594706d11 to your computer and use it in GitHub Desktop.
2D monadic loops
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main (main) where | |
import qualified Data.Vector.Unboxed.Mutable as VUM | |
import Control.Monad.State | |
import Control.Monad.Reader | |
import Control.Loop | |
main :: IO () | |
main = do | |
let w = 1024 | |
h = 1024 | |
v <- VUM.new $ w * h :: IO (VUM.IOVector Int) | |
forM_ [0..1000] $ \_ -> do | |
{- | |
forM_ [0..h - 1] $ \py -> forM_ [0..w - 1] $ \px -> | |
VUM.unsafeWrite v (px + py * w) (px + py) | |
-} | |
{- | |
forLoop 0 (< h) (+1) $ \py -> forLoop 0 (< w) (+1) $ \px -> | |
VUM.unsafeWrite v (px + py * w) (px + py) | |
-} | |
{- | |
numLoop 0 (h - 1) $ \py -> numLoop 0 (w - 1) $ \px -> | |
VUM.unsafeWrite v (px + py * w) (px + py) | |
-} | |
return () | |
void $ flip runReaderT 0 . flip runStateT 0 . forM_ [0..100] $ \_ -> do | |
{- | |
forM_ [0..h - 1] $ \py -> forM_ [0..w - 1] $ \px -> | |
liftIO $ VUM.unsafeWrite v (px + py * w) (px + py) | |
-} | |
{- | |
forLoop 0 (< h) (+1) $ \py -> forLoop 0 (< w) (+1) $ \px -> | |
liftIO $ VUM.unsafeWrite v (px + py * w) (px + py) | |
-} | |
{- | |
numLoop 0 (h - 1) $ \py -> numLoop 0 (w - 1) $ \px -> | |
liftIO $ VUM.unsafeWrite v (px + py * w) (px + py) | |
-} | |
return () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment