Last active
August 29, 2015 14:12
-
-
Save Sintrastes/fc6568ac6f0db3a515c2 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
-- Saw a (joke?) stack exchange post about an extensible fizzbuzz implementation. | |
-- I was bored, so I thought I'd give my take on something similar in Haskell: | |
import qualified Data.Map.Strict as M | |
import Control.Monad (join) | |
fizzBuzz :: M.Map Int String -> Int -> String -> [String] | |
fizzBuzz dict n default_value = map (toWords . divisors) [1..n] | |
where keys = M.keys dict | |
divisors n = filter (\x -> (n `mod` x) == 0) keys | |
toWords x | (length x == 0) = default_value | |
| otherwise = join $ map (\k -> dict M.! k) x | |
-- Examples! -- | |
normal_fizzbuzz = M.fromList [(3,"Fizz"),(5,"Buzz")] | |
extended_fizzbuzz = M.fromList [(3,"Fizz"),(5,"Buzz"),(7,"Bar"),(11,"Baz"),(13,"Jazz")] | |
main = do | |
mapM_ putStrLn $ fizzBuzz normal_fizzbuzz 25 "None" | |
mapM_ putStrLn $ fizzBuzz extended_fizzbuzz 25 "None" | |
-- Much extend, very concise, wow. -- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment