5 people like it.

Result type

 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: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
type Result<'Err, 'Value> = 
    | Error of 'Err
    | Success of 'Value

[<AutoOpen>]
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module Prelude = 
    let inline always value = fun () -> value
    let inline always' value = fun _ -> value

[<AutoOpen>]
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module ResultPrelude = 
    let guard f formatError = 
        try 
            Success(f())
        with e -> Error(formatError e)

module Result = 
    let inline substitute (forError : 'Err -> 'Outcome) (forSuccess : 'Value -> 'Outcome) (result : Result<'Err, 'Value>) = 
        match result with
        | Error err -> forError err
        | Success value -> forSuccess value
    
    let andThen f (result : Result<'Err, 'ValueA>) : Result<'Err, 'ValueB> = result |> substitute Error f
    
    let andGuard f formatError (result : Result<'Err, 'ValueA>) : Result<'Err, 'ValueB> = 
        result |> andThen (fun value -> 
                      try 
                          Success(f value)
                      with e -> Error(formatError e))
    
    let andTry f formatError finally' (result : Result<'Err, 'ValueA>) : Result<'Err, 'ValueB> = 
        result |> andThen (fun value -> 
                      try 
                          try 
                              Success(f value)
                          with e -> Error(formatError e)
                      finally
                          finally' value)
    
    let andError f (result : Result<'ErrA, 'Value>) : Result<'ErrB, 'Value> = result |> substitute f Success
    let map f (result : Result<'Err, 'A>) : Result<'Err, 'B> = result |> andThen (f >> Success)
    let formatError f (result : Result<'ErrA, 'Value>) : Result<'ErrB, 'Value> = result |> andError (f >> Error)
    let isError result = result |> substitute (always' true) (always' false)
    let isSuccess result = result |> substitute (always' false) (always' true)
    let withDefault def (result : Result<'Err, 'Value>) = result |> substitute (always' def) id
    let asError (result : Result<'Err, 'Value>) = result |> substitute Some (always' None)
    let asSuccess (result : Result<'Err, 'Value>) = result |> substitute (always' None) Some
    let toOption (result : Result<'Err, 'Value>) = asSuccess result
    let iter f (result : Result<'Err, 'Value>) = result |> substitute (always'()) f
    let sinkError f (result : Result<'Err, 'Value>) = result |> substitute f (always'()) 
union case Result.Error: 'Err -> Result<'Err,'Value>
union case Result.Success: 'Value -> Result<'Err,'Value>
Multiple items
type AutoOpenAttribute =
  inherit Attribute
  new : unit -> AutoOpenAttribute
  new : path:string -> AutoOpenAttribute
  member Path : string

Full name: Microsoft.FSharp.Core.AutoOpenAttribute

--------------------
new : unit -> AutoOpenAttribute
new : path:string -> AutoOpenAttribute
Multiple items
type CompilationRepresentationAttribute =
  inherit Attribute
  new : flags:CompilationRepresentationFlags -> CompilationRepresentationAttribute
  member Flags : CompilationRepresentationFlags

Full name: Microsoft.FSharp.Core.CompilationRepresentationAttribute

--------------------
new : flags:CompilationRepresentationFlags -> CompilationRepresentationAttribute
type CompilationRepresentationFlags =
  | None = 0
  | Static = 1
  | Instance = 2
  | ModuleSuffix = 4
  | UseNullAsTrueValue = 8
  | Event = 16

Full name: Microsoft.FSharp.Core.CompilationRepresentationFlags
CompilationRepresentationFlags.ModuleSuffix: CompilationRepresentationFlags = 4
val always : value:'a -> unit -> 'a

Full name: Script.PreludeModule.always
val value : 'a
val always' : value:'a -> 'b -> 'a

Full name: Script.PreludeModule.always'
val guard : f:(unit -> 'a) -> formatError:(exn -> 'b) -> Result<'b,'a>

Full name: Script.ResultPreludeModule.guard
val f : (unit -> 'a)
val formatError : (exn -> 'b)
val e : exn
Multiple items
module Result

from Script

--------------------
type Result<'Err,'Value> =
  | Error of 'Err
  | Success of 'Value

Full name: Script.Result<_,_>
val substitute : forError:('Err -> 'Outcome) -> forSuccess:('Value -> 'Outcome) -> result:Result<'Err,'Value> -> 'Outcome

Full name: Script.Result.substitute
val forError : ('Err -> 'Outcome)
val forSuccess : ('Value -> 'Outcome)
val result : Result<'Err,'Value>
type Result<'Err,'Value> =
  | Error of 'Err
  | Success of 'Value

Full name: Script.Result<_,_>
val err : 'Err
val value : 'Value
val andThen : f:('ValueA -> Result<'Err,'ValueB>) -> result:Result<'Err,'ValueA> -> Result<'Err,'ValueB>

Full name: Script.Result.andThen
val f : ('ValueA -> Result<'Err,'ValueB>)
val result : Result<'Err,'ValueA>
val andGuard : f:('ValueA -> 'ValueB) -> formatError:(exn -> 'Err) -> result:Result<'Err,'ValueA> -> Result<'Err,'ValueB>

Full name: Script.Result.andGuard
val f : ('ValueA -> 'ValueB)
val formatError : (exn -> 'Err)
val value : 'ValueA
val andTry : f:('ValueA -> 'ValueB) -> formatError:(exn -> 'Err) -> finally':('ValueA -> unit) -> result:Result<'Err,'ValueA> -> Result<'Err,'ValueB>

Full name: Script.Result.andTry
val finally' : ('ValueA -> unit)
val andError : f:('ErrA -> Result<'ErrB,'Value>) -> result:Result<'ErrA,'Value> -> Result<'ErrB,'Value>

Full name: Script.Result.andError
val f : ('ErrA -> Result<'ErrB,'Value>)
val result : Result<'ErrA,'Value>
val map : f:('A -> 'B) -> result:Result<'Err,'A> -> Result<'Err,'B>

Full name: Script.Result.map
val f : ('A -> 'B)
val result : Result<'Err,'A>
val formatError : f:('ErrA -> 'ErrB) -> result:Result<'ErrA,'Value> -> Result<'ErrB,'Value>

Full name: Script.Result.formatError
val f : ('ErrA -> 'ErrB)
val isError : result:Result<'a,'b> -> bool

Full name: Script.Result.isError
val result : Result<'a,'b>
val isSuccess : result:Result<'a,'b> -> bool

Full name: Script.Result.isSuccess
val withDefault : def:'Value -> result:Result<'Err,'Value> -> 'Value

Full name: Script.Result.withDefault
val def : 'Value
val id : x:'T -> 'T

Full name: Microsoft.FSharp.Core.Operators.id
val asError : result:Result<'Err,'Value> -> 'Err option

Full name: Script.Result.asError
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
val asSuccess : result:Result<'Err,'Value> -> 'Value option

Full name: Script.Result.asSuccess
val toOption : result:Result<'Err,'Value> -> 'Value option

Full name: Script.Result.toOption
val iter : f:('Value -> unit) -> result:Result<'Err,'Value> -> unit

Full name: Script.Result.iter
val f : ('Value -> unit)
val sinkError : f:('Err -> unit) -> result:Result<'Err,'Value> -> unit

Full name: Script.Result.sinkError
val f : ('Err -> unit)
Raw view Test code New version

More information

Link:http://fssnip.net/7OD
Posted:8 years ago
Author:
Tags: