Last active
May 13, 2016 13:57
-
-
Save bsima/d14f996bd4700a17c7d4d8abfe135606 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
-- http://www.parsonsmatt.org/2016/02/27/an_elegant_fizzbuzz.html | |
import Data.Foldable | |
import Data.Maybe | |
type FizzRule = Integer -> Maybe String | |
rule :: Integer -> String -> FizzRule | |
rule n m i = | |
case i `mod` n of | |
0 -> Just m | |
_ -> Nothing | |
fizz = rule 3 "Fizz" | |
buzz = rule 5 "Buzz" | |
fizzBuzz :: (Functor f, Foldable t) | |
=> t (Integer -> Maybe String) | |
-> f Integer | |
-> f String | |
fizzBuzz rules = fmap (fromMaybe <$> show <*> ruleSet) | |
where | |
ruleSet = fold rules | |
{- example repl session | |
λ> :l fizzbuzz.hs | |
[1 of 1] Compiling Main ( fizzbuzz.hs, interpreted ) | |
Ok, modules loaded: Main. | |
λ> fizzBuzz [fizz, buzz] | |
fizzBuzz [fizz, buzz] :: Functor f => f Integer -> f String | |
?? now what do ?? | |
-} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You have to wrap the
Integer
argument in aFunctor
, specifically a list.To get the output you might expect, you can map over the output strings and print them.