Last active
November 27, 2015 03:20
-
-
Save yukitos/06e4d825ccc33047ec8d to your computer and use it in GitHub Desktop.
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 IFizzBuzz = | |
abstract member check : int -> bool | |
abstract member getVal : unit -> string | |
type Fizz() = | |
interface IFizzBuzz with | |
member x.check n = (n % 3 = 0) | |
member x.getVal() = "Fizz" | |
type Buzz() = | |
interface IFizzBuzz with | |
member x.check n = (n % 5 = 0) | |
member x.getVal() = "Buzz" | |
type FizzBuzz(handlers : IFizzBuzz list) = | |
member x.eval n = | |
let result = | |
("", handlers) | |
||> Seq.fold (fun acc h -> | |
if h.check(n) then acc + h.getVal() | |
else acc) | |
if result = "" then string n | |
else result | |
let fb = | |
new FizzBuzz( | |
[ | |
new Fizz() | |
new Buzz() | |
]) | |
[1..100] |> List.map(fb.eval) |> printfn "%A" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment