Last active
July 3, 2020 15:16
-
-
Save tokdaniel/a909058cf463e82daf47c20c4bfbfd9a to your computer and use it in GitHub Desktop.
FizzBuzz in Purescript
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 where | |
import Prelude (bind, mod, pure, (<$>), (<<<), (==)) | |
import Data.List (List, (..)) | |
import Data.Either (Either(..)) | |
isDivisibleBy :: Int -> String -> Either String Int -> Either String Int | |
isDivisibleBy divider replacer value = do | |
v <- value | |
if mod v divider == 0 | |
then Left replacer | |
else Right v | |
fizz :: Either String Int -> Either String Int | |
fizz = isDivisibleBy 5 "Fizz" | |
buzz :: Either String Int -> Either String Int | |
buzz = isDivisibleBy 3 "Buzz" | |
fizzBuzz :: Either String Int -> Either String Int | |
fizzBuzz = fizz <<< buzz <<< (isDivisibleBy 15 "FizzBuzz") | |
result :: List (Either String Int) | |
result = fizzBuzz <<< pure <$> (1 .. 100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment