Created
July 28, 2022 09:43
-
-
Save edgarfgp/66a73f3d990b54d1588af3f75b5dff23 to your computer and use it in GitHub Desktop.
Active Patterns. with Pattern matching
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
let (|IsOneOf|_|) x y = if x = y then Some() else None | |
type SomeRecord = | |
{ ExternalEnum: int | |
SomeStringData: string } | |
let toProcess = | |
[ { ExternalEnum = 1 | |
SomeStringData = "I contain xml" } | |
{ ExternalEnum = 2 | |
SomeStringData = "I contain json" } | |
{ ExternalEnum = 3 | |
SomeStringData = "I contain csv" } | |
{ ExternalEnum = 4 | |
SomeStringData = "I also contain json" } ] | |
let processJsonFiles (record: SomeRecord) = | |
match record with | |
| { ExternalEnum = IsOneOf 2 | IsOneOf 4 | |
SomeStringData = _ } -> Some "parsed json" | |
| _ -> None | |
toProcess | |
|> List.map processJsonFiles | |
|> List.iter (printfn "%A") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Credit to @TheAngryByrd. Saving as a gist so I do not forget about this