Last active
May 10, 2017 22:28
-
-
Save matthewleon/77951008507fb6d0904c3c9bf29eade8 to your computer and use it in GitHub Desktop.
purescript fizzbuzz DSL
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
-- based on "FizzBuzz in Haskell by Embedding a Domain-Specific Language" | |
-- by Maciej Piróg | |
-- https://themonadreader.files.wordpress.com/2014/04/fizzbuzz.pdf | |
-- and "skiphaltprint" by Mathias Verraes | |
-- https://github.com/mathiasverraes/skiphaltprint | |
module Main where | |
import Prelude | |
import Control.Monad.Eff (Eff, forE) | |
import Control.Monad.Eff.Console (CONSOLE, log) | |
main :: forall e. Eff (console :: CONSOLE | e) Unit | |
main = forE 1 101 \i -> log $ fizzbuzz i | |
fizzbuzz :: Int -> String | |
fizzbuzz n = rules id $ show n | |
where | |
test :: Int -> String -> (String -> String) -> (String -> String) | |
test d s f | n `mod` d == 0 = const $ s <> f "" | |
| otherwise = f | |
rules = test 3 "fizz" <<< test 5 "buzz" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment