Skip to content

Instantly share code, notes, and snippets.

@tokiwoousaka
tokiwoousaka / gist:ace99e3a5dcb81c6b67e
Last active August 29, 2015 14:09
Stateにたいして色んな処理が書けるようになったよー https://github.com/tokiwoousaka/reasonable-lens
{-# LANGUAGE TemplateHaskell #-}
module Main where
import Control.Monad.State
import Control.Lens
type StateIO s a = StateT s IO a
data Hoge a = Hoge
{ _foo :: a
, _bar :: String
f :: Maybe Int
f = do
x <- return 5
y <- return 3
return $ x + y
f' :: Maybe Int
f' = do
x <- return 5
import Control.Monad.State
main :: IO ()
main = print $ runState f 1000 --状態の初期値を1000にしてfを実行
f :: State Int ()
f = do
x <- sub1
sub2 x
@tokiwoousaka
tokiwoousaka / gist:1a06887f58e7465ef6e9
Last active August 29, 2015 14:10
SeasonalFruitsを「SeasonとFruitのタプル」のリストと定義、即ち、季節と果物の直積の部分集合を表す。SeasonalFruits -> [Season] という型から、query関数はSeasonalFruitsから季節の部分集合への写像であると読める。
module Main where
import Control.Monad
type Season = String
type Fruit = String
type SeasonalFruits = [(Season, Fruit)]
dt :: SeasonalFruits
dt =
[ ("春", "いちご")
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
module Main where
----
-- Coyoneda
data CoYoneda f x = forall b. CoYoneda (b -> x) (f b)
instance Functor (CoYoneda f) where
{-# LANGUAGE TemplateHaskell #-}
module Main where
import Control.Monad.State
import Control.Lens
data Point = Point
{ _pointX :: Int
, _pointY :: Int
} deriving Show
{-# LANGUAGE RankNTypes #-}
module Main where
type Bool' = forall a. a -> a -> a
true :: Bool'
true = const
false :: Bool'
false = const id
{-# LANGUAGE TemplateHaskell, RankNTypes #-}
module Main where
import Control.Lens
import Control.Monad.State
import Data.List
sample :: [String]
sample =
[ "hoge"
, "piyo"
モナド則: (m >>= g) >>= h == m >>= (\x -> g x >>= h)
定義: f >=> g == \x -> f x >>= g
命題:(f >=> g) >=> h == f >=> (g >=> h)
(f >=> g) >=> h
== (\x -> f x >>= g) >=> h -- (>=>)の定義
== \y -> (\x -> f x >>= g) y >>= h -- (>=>)の定義
== \y -> (f y >>= g) >>= h
== \y -> f y >>= (\z -> g z >>= h) -- モナド則
== \y -> f y >>= (g >=> h) -- (>=>)の定義
var move_rand_pos = function (obj) {
obj.x = rand(SCREEN_WIDTH - CHARACTER_WIDTH);
obj.y = rand(SCREEN_HEIGHT - CHARACTER_HEIGHT);
}