Skip to content

Instantly share code, notes, and snippets.

@blitzcode
Created July 31, 2014 13:19
Show Gist options
  • Save blitzcode/7c63533b247594706d11 to your computer and use it in GitHub Desktop.
Save blitzcode/7c63533b247594706d11 to your computer and use it in GitHub Desktop.
2D monadic loops
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