Skip to content

Instantly share code, notes, and snippets.

@lgastako
Last active November 7, 2021 23:14
Show Gist options
  • Save lgastako/cba43b953a895847f9284b650d345329 to your computer and use it in GitHub Desktop.
Save lgastako/cba43b953a895847f9284b650d345329 to your computer and use it in GitHub Desktop.
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE OverloadedLabels #-}
module Example where
import GHC.Generics ( Generic )
import Control.Lens ( (+=)
, (*=)
, use
, view
)
import Control.Monad.State.Lazy ( State
, execState
, put
, replicateM
, when
)
import Data.Generics.Labels ()
newtype S = S { n :: Int }
deriving (Eq, Generic, Ord, Show)
foo :: Bool -> State S ()
foo flag = do
#n += 2
when flag $ #n *= 10
x <- use #n
put . S $ if | even x -> x + 1
| x > 100 -> x `rem` 100
| x < -100 -> x `rem` 100
| x `rem` 5 == 0 -> x + 11
| otherwise -> x - 25
main :: IO ()
main = print . n . flip execState initialVal . replicateM r $ foo flag
where
flag = True
initialVal = S 0
r = 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment