4 people like it.

Result Type Module Extensions

Extensions to the Result module which may be useful for processing Result types.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
/// Extends the Result module with additional functions which can be useful for processing Result types.
module Result =
    [<CompiledName("IsOk")>]
    let isOk result = match result with Error _ -> false | Ok _ -> true
  
    [<CompiledName("IsError")>]
    let isError result = match result with Error _ -> true | Ok _ -> false
    
    [<CompiledName("ChooseOk")>]
    let chooseOk result = match result with Error _ -> None | Ok x -> Some x

    [<CompiledName("ChooseError")>]
    let chooseError result = match result with Error err -> Some err | Ok _ -> None

    [<CompiledName("Get")>]
    let getOk result = match result with Error err -> failwithf "Could not get Ok item from Result as it contained an error: %A" err | Ok x -> x

    [<CompiledName("GetError")>]
    let getError result = match result with Error err -> err | Ok x -> failwithf "Could not get Error from Result: %A" x
    
    [<CompiledName("Throw")>]
    let throwOnError result = match result with Error err -> failwith err | Ok _ -> ()

// A common scenario is processing a list of items and handling the results
let mixedBag =
    [|
        Ok 50.0
        Error "Could not retrieve statistics for item: Reason 1"
        Ok 16.5
        Ok 10.1
        Error "Could not retrieve statistics for item: Reason 2"
        Ok 5.0
    |]

// You may want to route the successes to a different workflow
let successes = 
    mixedBag 
    |> Array.choose Result.chooseOk
    
// You may also want to route the failures to a different workflow
let failures =
    mixedBag
    |> Array.choose Result.chooseError
    
printfn "There were %d successes and %d failures for this operation." successes.Length failures.Length
Multiple items
type CompiledNameAttribute =
  inherit Attribute
  new : compiledName:string -> CompiledNameAttribute
  member CompiledName : string

Full name: Microsoft.FSharp.Core.CompiledNameAttribute

--------------------
new : compiledName:string -> CompiledNameAttribute
val isOk : result:'a -> 'b

Full name: Script.Result.isOk
val result : 'a
val isError : result:'a -> 'b

Full name: Script.Result.isError
val chooseOk : result:'a -> 'b

Full name: Script.Result.chooseOk
union case Option.None: Option<'T>
union case Option.Some: Value: 'T -> Option<'T>
val chooseError : result:'a -> 'b

Full name: Script.Result.chooseError
val getOk : result:'a -> 'b

Full name: Script.Result.getOk
val failwithf : format:Printf.StringFormat<'T,'Result> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.failwithf
val getError : result:'a -> 'b

Full name: Script.Result.getError
val throwOnError : result:'a -> 'b

Full name: Script.Result.throwOnError
val failwith : message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.failwith
val mixedBag : obj []

Full name: Script.mixedBag
val successes : obj []

Full name: Script.successes
module Array

from Microsoft.FSharp.Collections
val choose : chooser:('T -> 'U option) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.choose
module Result

from Script


 Extends the Result module with additional functions which can be useful for processing Result types.
val failures : obj []

Full name: Script.failures
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
property System.Array.Length: int
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/7WU
Posted:5 years ago
Author:Travis Rosenbaum
Tags: applicative , extensions , result type