Home
Insert
Update snippet 'FizzBuzz with Rule Engine'
Title
Passcode
Description
Inspired by http://dave.fayr.am/posts/2012-10-4-finding-fizzbuzz.html Rules are in a list of lambdas that can be easily modified. A pattern-matching recursive function applies them in the correct order.
Source code
let fizzRules = [ (fun i -> if i % 3 = 0 then "Fizz" else "") (fun i -> if i % 5 = 0 then "Buzz" else "") (fun i -> if i % 7 = 0 then "Bazz" else "") (fun i -> if i % 11 = 0 then "Bop" else "") ] let fizzBuzz rules i = let rec ruleRunner s rl = match s, rl with | "", [] -> i.ToString() | _, [] -> s | _, h::t -> ruleRunner (s + h i) t ruleRunner "" rules [ 1 .. 105 ] |> Seq.map (fizzBuzz fizzRules) |> Seq.iter (printfn "%s")
Tags
fizzbuzz
kata
rules
recursion
pattern matching
lambdas
fizzbuzz
kata
rules
recursion
pattern matching
lambdas
Author
Link
Reference NuGet packages
If your snippet has external dependencies, enter the names of NuGet packages to reference, separated by a comma (
#r
directives are not required).
Update