Skip to content

Instantly share code, notes, and snippets.

@ariady-putra
Last active May 10, 2023 17:33
Show Gist options
  • Select an option

  • Save ariady-putra/e2184a14d050212e4a8caeb3a7f8560a to your computer and use it in GitHub Desktop.

Select an option

Save ariady-putra/e2184a14d050212e4a8caeb3a7f8560a to your computer and use it in GitHub Desktop.
-- MonadReader
env <- ask
let val = view (fieldOfEnv . subField) env
-- MonadState
val <- use $ fieldOfState . subField -- get
fieldOfState . subField .= val'      -- put
fieldOfState . subField %= func      -- modify

https://wiki.haskell.org/LensBeginnersCheatsheet

processMove :: (MonadState Game m) => Move -> m Move
processMove playerMove = do
    gameState <- get -- lens getters:
    let fav = view (gHistory . hFavorite) gameState -- get field
    let fav = gameState ^. gHistory . hFavorite     -- infix variant
    favorite <- use $ gHistory . hFavorite          -- alternative getter
    
    -- lens setters:
    let g' = set gStrategy Cheat gameState  -- put field
    let g' = gStrategy .~ Cheat $ gameState -- infix variant
    let g' = gameState & gStrategy .~ Cheat -- Data.Function (&)
    
    -- lens modifiers:
    let g' = over gStrategy succ gameState -- modify field
    let g' = gStrategy %~ succ $ gameState -- infix variant
    let g' = gameState & gStrategy %~ succ -- Data.Function (&)
    
    -- sample application / use-case:
    let computerMove =
            case favorite of
                Rock     -> Paper
                Paper    -> Scissors
                Scissors -> Rock
    
    case playerMove of
        Rock     -> gHistory . hRock     %= succ -- increase Rock counter
        Paper    -> gHistory . hPaper    %= succ -- increase Paper counter
        Scissors -> gHistory . hScissors %= succ -- increase Scissors counter
    
    r <- use $ gHistory . hRock     -- get Rock count
    p <- use $ gHistory . hPaper    -- get Paper count
    s <- use $ gHistory . hScissors -- get Sciccosrs count
    let  favorite = snd . maximum $
            [ (r, Rock)
            , (p, Paper)
            , (s, Scissors)
            ]
    gHistory . hFavorite .= favorite -- put favorite
    return computerMove

where

...
import Control.Lens  (makeLenses)
import System.Random (..., StdGen, ...)
...

type Score     = Int
type Frequency = Int

data Result   = Win | Lose | Draw       deriving Show
data Strategy = Cheat | Dumb | Random   deriving (...)
data Move     = Rock | Paper | Scissors deriving (...)
instance Ord Move where
  compare :: Move -> Move -> Ordering
  ...

data History = History
    { _hMove     :: Move
    , _hResult   :: Result
    , _hRock     :: Frequency
    , _hPaper    :: Frequency
    , _hScissors :: Frequency
    , _hFavorite :: Move
    }
    deriving (Show)
makeLenses ''History

data Game = Game
    { _gPlayerScore   :: Score
    , _gComputerScore :: Score
    , _gStrategy      :: Strategy
    , _gHistory       :: History
    , _gGenerator     :: StdGen
    }
    deriving (Show)
makeLenses ''Game
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment