-
Notifications
You must be signed in to change notification settings - Fork 0
/
fizzbuzz.hs
27 lines (22 loc) · 798 Bytes
/
fizzbuzz.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{-# LANGUAGE OverloadedStrings #-}
-- Largely just http://www.parsonsmatt.org/2016/02/27/an_elegant_fizzbuzz.html
import Data.Foldable (fold, traverse_)
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import qualified Data.Text.IO as T
import Data.Text.Lazy (toStrict)
import Data.Text.Lazy.Builder (toLazyText)
import Data.Text.Lazy.Builder.Int (decimal)
rule :: (Integral a) => a -> Text -> a -> Maybe Text
rule n t i
| i `mod` n == 0 = Just t
| otherwise = Nothing
showIntegral :: (Integral a) => a -> Text
showIntegral = toStrict . toLazyText . decimal
fizzBuzz :: (Integral a) => a -> Text
fizzBuzz = fromMaybe <$> showIntegral <*> fold [fizz, buzz]
where
fizz = rule 3 "Fizz"
buzz = rule 5 "Buzz"
main :: IO ()
main = traverse_ (T.putStrLn . fizzBuzz) [1 .. 100]