Skip to content

Instantly share code, notes, and snippets.

@fjod
Created April 19, 2022 14:21
Show Gist options
  • Save fjod/de1c1da37768caf3f4be03b92028359a to your computer and use it in GitHub Desktop.
Save fjod/de1c1da37768caf3f4be03b92028359a to your computer and use it in GitHub Desktop.
The Power of Composition - Scott Wlaschin - NDC Oslo 2020
open Microsoft.FSharp.Core
type FizzBuzzResult =
| Unhadnled of int
| Handled of string
let isDivisiblyBy n divisor =
n % divisor = 0
let handle divisor label n =
let ok = isDivisiblyBy n divisor
match ok with
| true -> Handled label
| _ -> Unhadnled n
let printFizzBuzz fbr =
match fbr with
| Handled h -> printfn "Handled %s" h
| Unhadnled h -> printfn "Unhandled %i" h
let ifUnhandledDo f prevFizzBuzz =
match prevFizzBuzz with
| Handled s -> Handled s
| Unhadnled i -> f i
let fizBuzz n =
n
|> handle 15 "FizzBuzz"
|> ifUnhandledDo (handle 3 "fizz")
|> ifUnhandledDo (handle 5 "buzz")
|> printFizzBuzz
[0..100] |> List.iter fizBuzz
@fjod
Copy link
Author

fjod commented Apr 19, 2022

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment