Skip to content

Instantly share code, notes, and snippets.

@yukitos
Last active November 27, 2015 03:20
Show Gist options
  • Save yukitos/06e4d825ccc33047ec8d to your computer and use it in GitHub Desktop.
Save yukitos/06e4d825ccc33047ec8d to your computer and use it in GitHub Desktop.
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