Created
December 5, 2015 20:46
-
-
Save caindy/9bf281ecada30038510e to your computer and use it in GitHub Desktop.
an example of using an active pattern as a function argument
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
open System | |
let xmas = DateTime.Parse("December 25") | |
let (|ChristmasEve|ChristmasDay|Twelvetide|NearlyThere|TisTheSeason|NotChristmas|) (d : DateTime) = | |
match (d - xmas).Days with | |
| 0 -> ChristmasDay d.Year | |
| -1 -> ChristmasEve d.Year | |
| t when t > 0 && t < 12 -> Twelvetide t | |
| t when t > -7 && t < 0 -> NearlyThere | |
| t when t > -25 && t < 0 -> TisTheSeason | |
| _ -> NotChristmas | |
let (|Twelvetide|) (Twelvetide n) = | |
match n with | |
| 1 -> "Partridge in a pear tree" | |
| 2 -> "Turtle doves" | |
| 3 -> "French hens" | |
| 4 -> "Calling Birds" | |
| 5 -> "Gold Rings" | |
| 6 -> "Geese a-Laying" | |
| 7 -> "Swans a-Swimming" | |
| 8 -> "Maids a-Milking" | |
| 9 -> "Ladies Dancing" | |
| 10 -> "Lords a-Leaping" | |
| 11 -> "Pipers Pipin" | |
| 12 -> "Drummers Drumming" | |
let greetings = function | |
| ChristmasEve y | ChristmasDay y -> "Merry Christmas!" | |
| NearlyThere -> "Avoid the malls" | |
| TisTheSeason -> "Be Jolly!" | |
| NotChristmas -> "Bahumbug" | |
| Twelvetide s -> s | |
let rec from (d : DateTime) = seq { | |
yield d; yield! from <| d.AddDays(1.) | |
} | |
from (DateTime.Today) | |
|> Seq.takeWhile (function NotChristmas -> false | _ -> true) | |
|> Seq.iter (greetings >> printfn "%s") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment