Created
December 24, 2024 16:59
-
-
Save TheAngryByrd/b1f404cc8dfe843aab7d5afdfc99c111 to your computer and use it in GitHub Desktop.
Choice Combining Result CE
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
| type ResultBuilder () = | |
| member inline _.Return (x) = Ok x | |
| member inline _.Bind(x,f) = | |
| match x with | |
| | Ok x -> f x | |
| | Error e -> Error <| e | |
| member inline _.Bind(x: Result<_,_>,f: 'a -> Result<_,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of7 e) -> Choice1Of7 e |> Error | |
| | Error (Choice2Of7 e) -> Choice2Of7 e |> Error | |
| | Error (Choice3Of7 e) -> Choice3Of7 e |> Error | |
| | Error (Choice4Of7 e) -> Choice4Of7 e |> Error | |
| | Error (Choice5Of7 e) -> Choice5Of7 e |> Error | |
| | Error (Choice6Of7 e) -> Choice6Of7 e |> Error | |
| | Error (Choice7Of7 e) -> Choice7Of7 e |> Error | |
| | Error (e) -> Error <| Choice7Of7 e | |
| member inline _.Bind(x: Result<_,_>,f: 'a -> Result<_,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of7 e) -> Choice1Of7 e |> Error | |
| | Error (Choice2Of7 e) -> Choice2Of7 e |> Error | |
| | Error (Choice3Of7 e) -> Choice3Of7 e |> Error | |
| | Error (Choice4Of7 e) -> Choice4Of7 e |> Error | |
| | Error (Choice5Of7 e) -> Choice5Of7 e |> Error | |
| | Error (Choice6Of7 e) -> Choice6Of7 e |> Error | |
| | Error (Choice7Of7 e) -> Choice7Of7 e |> Error | |
| | Error (e) -> Error <| Choice6Of7 e | |
| member inline _.Bind(x: Result<_,_>,f: 'a -> Result<_,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of7 e) -> Choice1Of7 e |> Error | |
| | Error (Choice2Of7 e) -> Choice2Of7 e |> Error | |
| | Error (Choice3Of7 e) -> Choice3Of7 e |> Error | |
| | Error (Choice4Of7 e) -> Choice4Of7 e |> Error | |
| | Error (Choice5Of7 e) -> Choice5Of7 e |> Error | |
| | Error (Choice6Of7 e) -> Choice6Of7 e |> Error | |
| | Error (Choice7Of7 e) -> Choice7Of7 e |> Error | |
| | Error (e) -> Error <| Choice5Of7 e | |
| member inline _.Bind(x: Result<_,_>,f: 'a -> Result<_,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of7 e) -> Choice1Of7 e |> Error | |
| | Error (Choice2Of7 e) -> Choice2Of7 e |> Error | |
| | Error (Choice3Of7 e) -> Choice3Of7 e |> Error | |
| | Error (Choice4Of7 e) -> Choice4Of7 e |> Error | |
| | Error (Choice5Of7 e) -> Choice5Of7 e |> Error | |
| | Error (Choice6Of7 e) -> Choice6Of7 e |> Error | |
| | Error (Choice7Of7 e) -> Choice7Of7 e |> Error | |
| | Error (e) -> Error <| Choice4Of7 e | |
| member inline _.Bind(x: Result<_,_>,f: 'a -> Result<_,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of7 e) -> Choice1Of7 e |> Error | |
| | Error (Choice2Of7 e) -> Choice2Of7 e |> Error | |
| | Error (Choice3Of7 e) -> Choice3Of7 e |> Error | |
| | Error (Choice4Of7 e) -> Choice4Of7 e |> Error | |
| | Error (Choice5Of7 e) -> Choice5Of7 e |> Error | |
| | Error (Choice6Of7 e) -> Choice6Of7 e |> Error | |
| | Error (Choice7Of7 e) -> Choice7Of7 e |> Error | |
| | Error (e) -> Error <| Choice3Of7 e | |
| member inline _.Bind(x: Result<_,_>,f: 'a -> Result<_,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of7 e) -> Choice1Of7 e |> Error | |
| | Error (Choice2Of7 e) -> Choice2Of7 e |> Error | |
| | Error (Choice3Of7 e) -> Choice3Of7 e |> Error | |
| | Error (Choice4Of7 e) -> Choice4Of7 e |> Error | |
| | Error (Choice5Of7 e) -> Choice5Of7 e |> Error | |
| | Error (Choice6Of7 e) -> Choice6Of7 e |> Error | |
| | Error (Choice7Of7 e) -> Choice7Of7 e |> Error | |
| | Error (e) -> Error <| Choice2Of7 e | |
| member inline _.Bind(x: Result<_,_>,f: 'a -> Result<_,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of7 e) -> Choice1Of7 e |> Error | |
| | Error (Choice2Of7 e) -> Choice2Of7 e |> Error | |
| | Error (Choice3Of7 e) -> Choice3Of7 e |> Error | |
| | Error (Choice4Of7 e) -> Choice4Of7 e |> Error | |
| | Error (Choice5Of7 e) -> Choice5Of7 e |> Error | |
| | Error (Choice6Of7 e) -> Choice6Of7 e |> Error | |
| | Error (Choice7Of7 e) -> Choice7Of7 e |> Error | |
| | Error (e) -> Error <| Choice1Of7 e | |
| member inline _.Bind(x: Result<'a,_>,f: 'a -> Result<'d,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of6 e) -> Error <| Choice1Of6 e | |
| | Error (Choice2Of6 e) -> Error <| Choice2Of6 e | |
| | Error (Choice3Of6 e) -> Error <| Choice3Of6 e | |
| | Error (Choice4Of6 e) -> Error <| Choice4Of6 e | |
| | Error (Choice5Of6 e) -> Error <| Choice5Of6 e | |
| | Error (Choice6Of6 e) -> Error <| Choice6Of6 e | |
| | Error (e) -> Error <| Choice6Of6 e | |
| member inline _.Bind(x: Result<'a,_>,f: 'a -> Result<'d,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of6 e) -> Error <| Choice1Of6 e | |
| | Error (Choice2Of6 e) -> Error <| Choice2Of6 e | |
| | Error (Choice3Of6 e) -> Error <| Choice3Of6 e | |
| | Error (Choice4Of6 e) -> Error <| Choice4Of6 e | |
| | Error (Choice5Of6 e) -> Error <| Choice5Of6 e | |
| | Error (Choice6Of6 e) -> Error <| Choice6Of6 e | |
| | Error (e) -> Error <| Choice5Of6 e | |
| member inline _.Bind(x: Result<'a,_>,f: 'a -> Result<'d,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of6 e) -> Error <| Choice1Of6 e | |
| | Error (Choice2Of6 e) -> Error <| Choice2Of6 e | |
| | Error (Choice3Of6 e) -> Error <| Choice3Of6 e | |
| | Error (Choice4Of6 e) -> Error <| Choice4Of6 e | |
| | Error (Choice5Of6 e) -> Error <| Choice5Of6 e | |
| | Error (Choice6Of6 e) -> Error <| Choice6Of6 e | |
| | Error (e) -> Error <| Choice4Of6 e | |
| member inline _.Bind(x: Result<'a,_>,f: 'a -> Result<'d,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of6 e) -> Error <| Choice1Of6 e | |
| | Error (Choice2Of6 e) -> Error <| Choice2Of6 e | |
| | Error (Choice3Of6 e) -> Error <| Choice3Of6 e | |
| | Error (Choice4Of6 e) -> Error <| Choice4Of6 e | |
| | Error (Choice5Of6 e) -> Error <| Choice5Of6 e | |
| | Error (Choice6Of6 e) -> Error <| Choice6Of6 e | |
| | Error (e) -> Error <| Choice3Of6 e | |
| member inline _.Bind(x: Result<'a,_>,f: 'a -> Result<'d,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of6 e) -> Error <| Choice1Of6 e | |
| | Error (Choice2Of6 e) -> Error <| Choice2Of6 e | |
| | Error (Choice3Of6 e) -> Error <| Choice3Of6 e | |
| | Error (Choice4Of6 e) -> Error <| Choice4Of6 e | |
| | Error (Choice5Of6 e) -> Error <| Choice5Of6 e | |
| | Error (Choice6Of6 e) -> Error <| Choice6Of6 e | |
| | Error (e) -> Error <| Choice2Of6 e | |
| member inline _.Bind(x: Result<'a,_>,f: 'a -> Result<'d,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of6 e) -> Error <| Choice1Of6 e | |
| | Error (Choice2Of6 e) -> Error <| Choice2Of6 e | |
| | Error (Choice3Of6 e) -> Error <| Choice3Of6 e | |
| | Error (Choice4Of6 e) -> Error <| Choice4Of6 e | |
| | Error (Choice5Of6 e) -> Error <| Choice5Of6 e | |
| | Error (Choice6Of6 e) -> Error <| Choice6Of6 e | |
| | Error (e) -> Error <| Choice1Of6 e | |
| member inline _.Bind(x: Result<'a,_>,f: 'a -> Result<'d,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of5 e) -> Error <| Choice1Of5 e | |
| | Error (Choice2Of5 e) -> Error <| Choice2Of5 e | |
| | Error (Choice3Of5 e) -> Error <| Choice3Of5 e | |
| | Error (Choice4Of5 e) -> Error <| Choice4Of5 e | |
| | Error (Choice5Of5 e) -> Error <| Choice5Of5 e | |
| | Error (e) -> Error <| Choice5Of5 e | |
| member inline _.Bind(x: Result<'a,_>,f: 'a -> Result<'d,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of5 e) -> Error <| Choice1Of5 e | |
| | Error (Choice2Of5 e) -> Error <| Choice2Of5 e | |
| | Error (Choice3Of5 e) -> Error <| Choice3Of5 e | |
| | Error (Choice4Of5 e) -> Error <| Choice4Of5 e | |
| | Error (Choice5Of5 e) -> Error <| Choice5Of5 e | |
| | Error (e) -> Error <| Choice4Of5 e | |
| member inline _.Bind(x: Result<'a,_>,f: 'a -> Result<'d,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of5 e) -> Error <| Choice1Of5 e | |
| | Error (Choice2Of5 e) -> Error <| Choice2Of5 e | |
| | Error (Choice3Of5 e) -> Error <| Choice3Of5 e | |
| | Error (Choice4Of5 e) -> Error <| Choice4Of5 e | |
| | Error (Choice5Of5 e) -> Error <| Choice5Of5 e | |
| | Error (e) -> Error <| Choice3Of5 e | |
| member inline _.Bind(x: Result<'a,_>,f: 'a -> Result<'d,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of5 e) -> Error <| Choice1Of5 e | |
| | Error (Choice2Of5 e) -> Error <| Choice2Of5 e | |
| | Error (Choice3Of5 e) -> Error <| Choice3Of5 e | |
| | Error (Choice4Of5 e) -> Error <| Choice4Of5 e | |
| | Error (Choice5Of5 e) -> Error <| Choice5Of5 e | |
| | Error (e) -> Error <| Choice2Of5 e | |
| member inline _.Bind(x: Result<'a,_>,f: 'a -> Result<'d,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of5 e) -> Error <| Choice1Of5 e | |
| | Error (Choice2Of5 e) -> Error <| Choice2Of5 e | |
| | Error (Choice3Of5 e) -> Error <| Choice3Of5 e | |
| | Error (Choice4Of5 e) -> Error <| Choice4Of5 e | |
| | Error (Choice5Of5 e) -> Error <| Choice5Of5 e | |
| | Error (e) -> Error <| Choice1Of5 e | |
| member inline _.Bind(x: Result<'a,_>,f: 'a -> Result<'d,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of4 e) -> Error <| Choice1Of4 e | |
| | Error (Choice2Of4 e) -> Error <| Choice2Of4 e | |
| | Error (Choice3Of4 e) -> Error <| Choice3Of4 e | |
| | Error (Choice4Of4 e) -> Error <| Choice4Of4 e | |
| | Error (e) -> Error <| Choice4Of4 e | |
| member inline _.Bind(x: Result<'a,_>,f: 'a -> Result<'d,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of4 e) -> Error <| Choice1Of4 e | |
| | Error (Choice2Of4 e) -> Error <| Choice2Of4 e | |
| | Error (Choice3Of4 e) -> Error <| Choice3Of4 e | |
| | Error (Choice4Of4 e) -> Error <| Choice4Of4 e | |
| | Error (e) -> Error <| Choice3Of4 e | |
| member inline _.Bind(x: Result<'a,_>,f: 'a -> Result<'d,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of4 e) -> Error <| Choice1Of4 e | |
| | Error (Choice2Of4 e) -> Error <| Choice2Of4 e | |
| | Error (Choice3Of4 e) -> Error <| Choice3Of4 e | |
| | Error (Choice4Of4 e) -> Error <| Choice4Of4 e | |
| | Error (e) -> Error <| Choice2Of4 e | |
| member inline _.Bind(x: Result<'a,_>,f: 'a -> Result<'d,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of4 e) -> Error <| Choice1Of4 e | |
| | Error (Choice2Of4 e) -> Error <| Choice2Of4 e | |
| | Error (Choice3Of4 e) -> Error <| Choice3Of4 e | |
| | Error (Choice4Of4 e) -> Error <| Choice4Of4 e | |
| | Error (e) -> Error <| Choice1Of4 e | |
| member inline _.Bind(x: Result<'a,_>,f: 'a -> Result<'d,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of3 e) -> Error <| Choice1Of3 e | |
| | Error (Choice2Of3 e) -> Error <| Choice2Of3 e | |
| | Error (Choice3Of3 e) -> Error <| Choice3Of3 e | |
| | Error (e) -> Error <| Choice1Of3 e | |
| member inline _.Bind(x: Result<'a,_>,f: 'a -> Result<'d,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of3 e) -> Error <| Choice1Of3 e | |
| | Error (Choice2Of3 e) -> Error <| Choice2Of3 e | |
| | Error (Choice3Of3 e) -> Error <| Choice3Of3 e | |
| | Error (e) -> Error <| Choice2Of3 e | |
| member inline _.Bind(x: Result<'a,_>,f: 'a -> Result<'d,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of3 e) -> Error <| Choice1Of3 e | |
| | Error (Choice2Of3 e) -> Error <| Choice2Of3 e | |
| | Error (Choice3Of3 e) -> Error <| Choice3Of3 e | |
| | Error (e) -> Error <| Choice3Of3 e | |
| member inline _.Bind(x: Result<'a,_>,f: 'a -> Result<'d,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of2 e) -> Error <| Choice1Of2 e | |
| | Error (Choice2Of2 e) -> Error <| Choice2Of2 e | |
| | Error (e) -> Error <| Choice1Of2 e | |
| member inline _.Bind(x: Result<'a,_>,f: 'a -> Result<'d,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of2 e) -> Error <| Choice1Of2 e | |
| | Error (Choice2Of2 e) -> Error <| Choice2Of2 e | |
| | Error (e) -> Error <| Choice2Of2 e | |
| member _.BindReturn(x, f) = | |
| match x with | |
| | Ok x -> Ok (f x) | |
| | Error e -> Error e | |
| let result = ResultBuilder() | |
| [<AutoOpen>] | |
| module ResultBuilderExtensions = | |
| type ResultBuilder with | |
| member inline _.Bind(x: Result<'a,'b>,f: 'a -> Result<'d,'e>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error e -> Error <| Choice2Of2 e | |
| | Error (e) -> Error <| Choice1Of2 e | |
| [<AutoOpen>] | |
| module ResultBuilderExtensions2 = | |
| type ResultBuilder with | |
| member inline _.Bind(x: Result<'a,_>,f: 'a -> Result<'d,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of2 e) -> Error <| Choice2Of3 e | |
| | Error (Choice2Of2 e) -> Error <| Choice3Of3 e | |
| | Error (e) -> Error <| Choice1Of3 e | |
| member inline _.Bind(x: Result<'a,_>,f: 'a -> Result<'d,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of3 e) -> Error <| Choice2Of4 e | |
| | Error (Choice2Of3 e) -> Error <| Choice3Of4 e | |
| | Error (Choice3Of3 e) -> Error <| Choice4Of4 e | |
| | Error (e) -> Error <| Choice1Of4 e | |
| member inline _.Bind(x: Result<'a,_>,f: 'a -> Result<'d,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of4 e) -> Error <| Choice2Of5 e | |
| | Error (Choice2Of4 e) -> Error <| Choice3Of5 e | |
| | Error (Choice3Of4 e) -> Error <| Choice4Of5 e | |
| | Error (Choice4Of4 e) -> Error <| Choice5Of5 e | |
| | Error (e) -> Error <| Choice1Of5 e | |
| member inline _.Bind(x: Result<'a,_>,f: 'a -> Result<'d,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of5 e) -> Error <| Choice2Of6 e | |
| | Error (Choice2Of5 e) -> Error <| Choice3Of6 e | |
| | Error (Choice3Of5 e) -> Error <| Choice4Of6 e | |
| | Error (Choice4Of5 e) -> Error <| Choice5Of6 e | |
| | Error (Choice5Of5 e) -> Error <| Choice6Of6 e | |
| | Error (e) -> Error <| Choice1Of6 e | |
| member inline _.Bind(x: Result<'a,_>,f: 'a -> Result<'d,_>) = | |
| match x with | |
| | Ok x -> | |
| match f x with | |
| | Ok x -> Ok x | |
| | Error (Choice1Of6 e) -> Error <| Choice2Of7 e | |
| | Error (Choice2Of6 e) -> Error <| Choice3Of7 e | |
| | Error (Choice3Of6 e) -> Error <| Choice4Of7 e | |
| | Error (Choice4Of6 e) -> Error <| Choice5Of7 e | |
| | Error (Choice5Of6 e) -> Error <| Choice6Of7 e | |
| | Error (Choice6Of6 e) -> Error <| Choice7Of7 e | |
| | Error (e) -> Error <| Choice1Of7 e | |
| type FoodErrors = | |
| | BadFood | |
| | SpilledMilk | |
| let getFood () = result { | |
| let! _ = Error BadFood | |
| return 42 | |
| } | |
| type CatErrors = | |
| | CatHungry | |
| | CatHappy | |
| let petCat () = result { | |
| let! _ = Error CatHappy | |
| return 0 | |
| } | |
| type WalkAroundErrors = | |
| | SteppedOnLego | |
| | DirtyFloor | |
| let walkAround () = result { | |
| let! _ = Error SteppedOnLego | |
| return 1701 | |
| } | |
| type CoffeeErrors = | |
| | HasSugar | |
| | HasCream | |
| let coffee () = result { | |
| let! _ = Error HasSugar | |
| return 1 | |
| } | |
| type Error1 = | |
| | ErrorA | |
| | ErrorB | |
| type Error2 = | |
| | ErrorC | |
| | ErrorD | |
| type Error3 = | |
| | ErrorE | |
| | ErrorF | |
| type Error4 = | |
| | ErrorG | |
| | ErrorH | |
| let kitchenActivity () = result { | |
| let! x = getFood () | |
| printfn "Side effect!" | |
| let! y = petCat () | |
| let! z = walkAround () | |
| let! a = coffee () | |
| let! _ = Error ErrorA | |
| let! _ = Error ErrorB | |
| let! _ = Error ErrorC | |
| let! _ = Error ErrorD | |
| let! _ = Error ErrorE | |
| let! _ = Error ErrorG | |
| let! _ = Error ErrorG | |
| return x + y + z + a | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment