Last active
December 16, 2015 23:59
-
-
Save vasily-kirichenko/5517668 to your computer and use it in GitHub Desktop.
Applicative validation
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 FSharpx.Choice | |
open FSharpx.Validation | |
open FSharpx.Collections | |
open System | |
// generic validator which returns Success if given predicate succeeds, otherwise Failure of NonEmptyList of errors | |
let validator pred error value = | |
if pred value then Choice1Of2 value | |
else Choice2Of2 (NonEmptyList.singleton error) | |
// some simple validators | |
let notEqual x = validator ((<>) x) <| sprintf "should not equal to %d" x | |
let evenNumber = validator (fun x -> x % 2 = 0) "should be even" | |
// a more complex validator made by combining a couple of simpler ones | |
let validateNumber n = | |
returnM n | |
<* evenNumber n | |
<* notEqual 3 n | |
let printRes res = | |
match res with | |
| Success n -> printfn "%A" n | |
| Failure errors -> errors |> String.concat ", " |> printfn "%s" | |
// test | |
[-5..5] |> List.iter (fun x -> (x, validateNumber x) ||> print) | |
results: | |
-5 | should be even | |
-4 | -4 | |
-3 | should be even | |
-2 | -2 | |
-1 | should be even | |
0 | 0 | |
1 | should be even | |
2 | 2 | |
3 | should be even, should not equal to 3 | |
4 | 4 | |
5 | should be even |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment