Skip to content

Instantly share code, notes, and snippets.

@HybridEidolon
Created May 15, 2015 07:35
Show Gist options
  • Save HybridEidolon/80e259d90a23f509d4c9 to your computer and use it in GitHub Desktop.
Save HybridEidolon/80e259d90a23f509d4c9 to your computer and use it in GitHub Desktop.
state monads are magic
import Control.Monad.State.Lazy
data Vec2 = Vec2 Float Float deriving (Show)
data Entity = Entity Vec2 [Char] deriving (Show)
vec2Add :: Vec2 -> Vec2 -> Vec2
vec2Add (Vec2 x1 y1) (Vec2 x2 y2) = Vec2 (x1+x2) (y1+y2)
incrementOne :: Num s => State s ()
incrementOne = do
x <- get
put (x + 1)
updateEntity :: State Entity ()
updateEntity = do
e <- get
put (changeIt e)
where changeIt (Entity f s) = Entity (vec2Add f $ Vec2 4 8) (s ++ " yeah")
main :: IO ()
main = do
putStrLn "Hello world, here's some state manipulation."
a <- return $ execState updateEntity $ Entity (Vec2 1 5) "fuck yeah"
a <- return $ execState updateEntity a
a <- return $ execState updateEntity a
putStrLn (show a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment