Created
February 14, 2018 20:48
-
-
Save danyx23/63482bb677982c7fd72d81751a0734e2 to your computer and use it in GitHub Desktop.
F# Result applicative accumulating errors in a list
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
type Test = | |
{ A : string | |
B : int | |
C : float | |
} | |
let buildTest a b c = | |
{ A = a | |
B = b | |
C = c | |
} | |
let a = Error ["No a"] | |
let b = Error ["No b"] | |
let c = Error ["No c"] | |
let (<*>) (res1 : Result<('a -> 'c), 'b list>) (res2 : Result<'a, 'b list>) : Result<'c, 'b list> = | |
match res1, res2 with | |
| Error err1, Error err2 -> Error (List.append err1 err2) | |
| Ok f, Error err2 -> Error err2 | |
| Error err1, Ok a -> Error err1 | |
| Ok f, Ok a -> Ok <| f a | |
let f = Ok buildTest | |
let x = f <*> a <*> b <*> c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment