Skip to content

Instantly share code, notes, and snippets.

@mbbx6spp
Last active May 4, 2020 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbbx6spp/29dc4ce7e7feb2f60a629c3946a21584 to your computer and use it in GitHub Desktop.
Save mbbx6spp/29dc4ce7e7feb2f60a629c3946a21584 to your computer and use it in GitHub Desktop.
Fun with the state transformer with a coworker showing them functional programming constructs. Notice there are more lines of imports than there are lines of code.
module Main (main) where
import Control.Applicative (pure)
import Control.Bind (bind, discard)
import Control.Monad (void)
import Control.Monad.State (StateT, get, runStateT)
import Data.Array (range)
import Data.Eq ((==))
import Data.Function (($))
import Data.Functor (map)
import Data.Int (rem)
import Data.Show (show)
import Data.String (joinWith)
import Data.Unit (Unit, unit)
import Effect (Effect)
import Effect.Class (liftEffect)
import Effect.Console (log)
fizzBuzz :: StateT (Array Int) Effect Unit
fizzBuzz = do
xs <- get
liftEffect $ log $ joinWith "\n" (map toString xs)
pure unit
where toString :: Int -> String
toString n | n `rem` 15 == 0 = "FizzBuzz"
toString n | n `rem` 5 == 0 = "Buzz"
toString n | n `rem` 3 == 0 = "Fizz"
toString n = show n
main :: Effect Unit
main = void $ runStateT fizzBuzz (range 1 100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment