Last active
May 4, 2020 18:09
-
-
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.
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
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