Skip to content

Instantly share code, notes, and snippets.

@Shamrock-Frost
Last active August 21, 2016 04:49
Show Gist options
  • Save Shamrock-Frost/c1a9ca5bfe972a341a7a584d9b5fc867 to your computer and use it in GitHub Desktop.
Save Shamrock-Frost/c1a9ca5bfe972a341a7a584d9b5fc867 to your computer and use it in GitHub Desktop.
Turn back all ye who enter here, for the black magic of {-# LANGUAGE RebindableSyntax #-} is afoot. This is the worst code I've ever written. The f# code is probably more idiomatic?
{-# LANGUAGE RebindableSyntax #-}
import Prelude hiding ((>>=), return)
main = mapM_ (putStrLn . fizzbuzz) [1..100]
fizzbuzz n = do
fizz <- (3, "Fizz")
buzz <- (5, "Buzz")
return $ fizz ++ buzz
where (key, val) >>= f | rem n key == 0 = f val
_ >>= f = f ""
return "" = show n
return s = s
type FizzBuzzBuilder(n) =
member this.Bind((k, v), f) =
if n % k = 0 then v else "" |> f
member this.Return(x) =
if x = "" then n.ToString() else x
let fizzbuzz n =
let b = new FizzBuzzBuilder(n)
b {
let! fizz = (3, "Fizz")
let! buzz = (5, "Buzz")
return (fizz + buzz)
}
let main = [1..100] |> List.map (fizzbuzz >> printfn "%s")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment