Created
April 19, 2022 14:21
-
-
Save fjod/de1c1da37768caf3f4be03b92028359a to your computer and use it in GitHub Desktop.
The Power of Composition - Scott Wlaschin - NDC Oslo 2020
This file contains hidden or 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
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 |
Author
fjod
commented
Apr 19, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment