Last active
August 29, 2015 14:02
-
-
Save JonCanning/2f5c22c48bfbd24367ac to your computer and use it in GitHub Desktop.
FizzBuzzLucky
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
type Result = | |
| Text of string | |
| Number of int | |
override self.ToString() = match self with | Text s -> s | Number i -> i.ToString() | |
let (>>=) f1 f2 m = match f1 m with | Text _ as r -> r | Number i -> f2 i | |
let rule f s = function | i when f i -> Text s | i -> Number i | |
let fizz = rule (fun i -> i % 3 = 0) "fizz" | |
let buzz = rule (fun i -> i % 5 = 0) "buzz" | |
let fizzbuzz = rule (fun i -> i % 3 = 0 && i % 5 = 0) "fizzbuzz" | |
let lucky = rule (fun i -> i.ToString().Contains "3") "lucky" | |
let calculate = lucky >>= fizzbuzz >>= fizz >>= buzz >> stdout.WriteLine | |
seq {1..20} |> Seq.iter calculate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment