Last active
November 7, 2021 23:14
-
-
Save lgastako/cba43b953a895847f9284b650d345329 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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