|
|
|
|
|
by wz1000
4069 days ago
|
|
Here's a version in Haskell that has the same functionality: fizzbuzz i triggers = map (\x -> fromMaybe (show x) $
liftM mconcat (sequence triggers) x)
[i..]
fizzbuzz 1 $ [\i -> ["Fizz" | i `rem` 3 == 0]
,\i -> ["Buzz" | i `rem` 5 == 0]
]
Its just me playing around and trying to be clever.The way FizzBuzz can be expressed as a Monoid is cool. The rest is just plumbing. mconcat [Just "Fizz", Just "Buzz"] = Just "FizzBuzz"
mconcat [Just "Fizz", Nothing ] = Just "Fizz"
mconcat [Nothing , Nothing ] = Nothing
Note that this requires {-# LANGUAGE MonadComprehensions #-} to compile. Thats for the sugar used when constructing the triggers. |
|