FSharp.Core


Result Module

Contains operations for working with values of type Result.

Functions and values

Function or value Description

bind binder result

Full Usage: bind binder result

Parameters:
    binder : 'T -> Result<'U, 'TError> - A function that takes the value of type T from a result and transforms it into a result containing a value of type U.
    result : Result<'T, 'TError> - The input result.

Returns: Result<'U, 'TError> A result of the output type of the binder.

bind f inp evaluates to match inp with Error e -> Error e | Ok x -> f x

binder : 'T -> Result<'U, 'TError>

A function that takes the value of type T from a result and transforms it into a result containing a value of type U.

result : Result<'T, 'TError>

The input result.

Returns: Result<'U, 'TError>

A result of the output type of the binder.

Example

 let tryParse (input: string) =
     match System.Int32.TryParse input with
     | true, v -> Ok v
     | false, _ -> Error "couldn't parse"

 Error "message" |> Result.bind tryParse // evaluates to Error "message"

 Ok "42" |> Result.bind tryParse // evaluates to Ok 42

 Ok "Forty-two" |> Result.bind tryParse // evaluates to Error "couldn't parse"

contains value result

Full Usage: contains value result

Parameters:
    value : 'T - The value to test for equality.
    result : Result<'T, 'Error> - The input result.

Returns: bool True if the result is Ok and contains a value equal to value, otherwise false.
Modifiers: inline
Type parameters: 'T, 'Error

Evaluates to true if result is Ok and its value is equal to value.

value : 'T

The value to test for equality.

result : Result<'T, 'Error>

The input result.

Returns: bool

True if the result is Ok and contains a value equal to value, otherwise false.

Example

 (99, Error 99) ||> Result.contains // evaluates to false
 (99, Ok 99) ||> Result.contains // evaluates to true
 (99, Ok 100) ||> Result.contains // evaluates to false

count result

Full Usage: count result

Parameters:
    result : Result<'T, 'Error> - The input result.

Returns: int A zero if the result is Error, a one otherwise.

count inp evaluates to match inp with Error _ -> 0 | Ok _ -> 1.

result : Result<'T, 'Error>

The input result.

Returns: int

A zero if the result is Error, a one otherwise.

Example

 Error 99 |> Result.count // evaluates to 0
 Ok 99 |> Result.count // evaluates to 1

defaultValue value result

Full Usage: defaultValue value result

Parameters:
    value : 'T - The specified default value.
    result : Result<'T, 'Error> - The input result.

Returns: 'T The result if the result is Ok, else the default value.

Gets the value of the result if the result is Ok, otherwise returns the specified default value.

value : 'T

The specified default value.

result : Result<'T, 'Error>

The input result.

Returns: 'T

The result if the result is Ok, else the default value.

Example

 Result.defaultValue 2 (Error 3) // evaluates to 2
 Result.defaultValue 2 (Ok 1) // evaluates to 1

defaultWith defThunk result

Full Usage: defaultWith defThunk result

Parameters:
    defThunk : 'Error -> 'T - A thunk that provides a default value when evaluated.
    result : Result<'T, 'Error> - The input result.

Returns: 'T The result if the result is Ok, else the result of evaluating defThunk.

Gets the value of the result if the result is Ok, otherwise evaluates defThunk and returns the result.

defThunk is not evaluated unless result is Error.

defThunk : 'Error -> 'T

A thunk that provides a default value when evaluated.

result : Result<'T, 'Error>

The input result.

Returns: 'T

The result if the result is Ok, else the result of evaluating defThunk.

Example

 Ok 1 |> Result.defaultWith (fun error -> 99) // evaluates to 1
 Error 2 |> Result.defaultWith (fun error -> 99) // evaluates to 99

exists predicate result

Full Usage: exists predicate result

Parameters:
    predicate : 'T -> bool - A function that evaluates to a boolean when given a value from the result type.
    result : Result<'T, 'Error> - The input result.

Returns: bool False if the result is Error, otherwise it returns the result of applying the predicate to the result value.

exists p inp evaluates to match inp with Error _ -> false | Ok x -> p x.

predicate : 'T -> bool

A function that evaluates to a boolean when given a value from the result type.

result : Result<'T, 'Error>

The input result.

Returns: bool

False if the result is Error, otherwise it returns the result of applying the predicate to the result value.

Example

 Error 6 |> Result.exists (fun x -> x >= 5) // evaluates to false
 Ok 42 |> Result.exists (fun x -> x >= 5) // evaluates to true
 Ok 4 |> Result.exists (fun x -> x >= 5) // evaluates to false

fold folder state result

Full Usage: fold folder state result

Parameters:
    folder : 'State -> 'T -> 'State - A function to update the state data when given a value from an result.
    state : 'State - The initial state.
    result : Result<'T, 'Error> - The input result.

Returns: 'State The original state if the result is Error, otherwise it returns the updated state with the folder and the result value.

fold f s inp evaluates to match inp with Error _ -> s | Ok x -> f s x.

folder : 'State -> 'T -> 'State

A function to update the state data when given a value from an result.

state : 'State

The initial state.

result : Result<'T, 'Error>

The input result.

Returns: 'State

The original state if the result is Error, otherwise it returns the updated state with the folder and the result value.

Example

 (0, Error 2) ||> Result.fold (fun accum x -> accum + x * 2) // evaluates to 0
 (0, Ok 1) ||> Result.fold (fun accum x -> accum + x * 2) // evaluates to 2
 (10, Ok 1) ||> Result.fold (fun accum x -> accum + x * 2) // evaluates to 12

foldBack folder result state

Full Usage: foldBack folder result state

Parameters:
    folder : 'T -> 'State -> 'State - A function to update the state data when given a value from an result.
    result : Result<'T, 'Error> - The input result.
    state : 'State - The initial state.

Returns: 'State The original state if the result is Error, otherwise it returns the updated state with the folder and the result value.

fold f inp s evaluates to match inp with Error _ -> s | Ok x -> f x s.

folder : 'T -> 'State -> 'State

A function to update the state data when given a value from an result.

result : Result<'T, 'Error>

The input result.

state : 'State

The initial state.

Returns: 'State

The original state if the result is Error, otherwise it returns the updated state with the folder and the result value.

Example

 (Error 2, 0) ||> Result.foldBack (fun x accum -> accum + x * 2) // evaluates to 0
 (Ok 1, 0) ||> Result.foldBack (fun x accum -> accum + x * 2) // evaluates to 2
 (Ok 1, 10) ||> Result.foldBack (fun x accum -> accum + x * 2) // evaluates to 12

forall predicate result

Full Usage: forall predicate result

Parameters:
    predicate : 'T -> bool - A function that evaluates to a boolean when given a value from the result type.
    result : Result<'T, 'Error> - The input result.

Returns: bool True if the result is Error, otherwise it returns the result of applying the predicate to the result value.

forall p inp evaluates to match inp with Error _ -> true | Ok x -> p x.

predicate : 'T -> bool

A function that evaluates to a boolean when given a value from the result type.

result : Result<'T, 'Error>

The input result.

Returns: bool

True if the result is Error, otherwise it returns the result of applying the predicate to the result value.

Example

 Error 1 |> Result.forall (fun x -> x >= 5) // evaluates to true
 Ok 42 |> Result.forall (fun x -> x >= 5) // evaluates to true
 Ok 4 |> Result.forall (fun x -> x >= 5) // evaluates to false

isError result

Full Usage: isError result

Parameters:
    result : Result<'T, 'Error> - The input result.

Returns: bool True if the result is Error.
Modifiers: inline
Type parameters: 'T, 'Error

Returns true if the result is Error.

result : Result<'T, 'Error>

The input result.

Returns: bool

True if the result is Error.

Example

 Ok 42 |> Result.isError // evaluates to false
 Error 42 |> Result.isError // evaluates to true

isOk result

Full Usage: isOk result

Parameters:
    result : Result<'T, 'Error> - The input result.

Returns: bool True if the result is OK.
Modifiers: inline
Type parameters: 'T, 'Error

Returns true if the result is Ok.

result : Result<'T, 'Error>

The input result.

Returns: bool

True if the result is OK.

Example

 Ok 42 |> Result.isOk // evaluates to true
 Error 42 |> Result.isOk // evaluates to false

iter action result

Full Usage: iter action result

Parameters:
    action : 'T -> unit - A function to apply to the result value.
    result : Result<'T, 'Error> - The input result.

iter f inp executes match inp with Error _ -> () | Ok x -> f x.

action : 'T -> unit

A function to apply to the result value.

result : Result<'T, 'Error>

The input result.

Example

 Error "Hello world" |> Result.iter (printfn "%s") // does nothing
 Ok "Hello world" |> Result.iter (printfn "%s") // prints "Hello world"

map mapping result

Full Usage: map mapping result

Parameters:
    mapping : 'T -> 'U - A function to apply to the OK result value.
    result : Result<'T, 'TError> - The input result.

Returns: Result<'U, 'TError> A result of the input value after applying the mapping function, or Error if the input is Error.

map f inp evaluates to match inp with Error e -> Error e | Ok x -> Ok (f x).

mapping : 'T -> 'U

A function to apply to the OK result value.

result : Result<'T, 'TError>

The input result.

Returns: Result<'U, 'TError>

A result of the input value after applying the mapping function, or Error if the input is Error.

Example

 Ok 1 |> Result.map (fun x -> "perfect") // evaluates to Ok "perfect"

 Error "message" |> Result.map (fun x -> "perfect") // evaluates to Error "message"

mapError mapping result

Full Usage: mapError mapping result

Parameters:
    mapping : 'TError -> 'U - A function to apply to the Error result value.
    result : Result<'T, 'TError> - The input result.

Returns: Result<'T, 'U> A result of the error value after applying the mapping function, or Ok if the input is Ok.

map f inp evaluates to match inp with Error x -> Error (f x) | Ok v -> Ok v.

mapping : 'TError -> 'U

A function to apply to the Error result value.

result : Result<'T, 'TError>

The input result.

Returns: Result<'T, 'U>

A result of the error value after applying the mapping function, or Ok if the input is Ok.

Example

 Ok 1 |> Result.mapError (fun x -> "bar") // evaluates to Ok 1

 Error "foo" |> Result.mapError (fun x -> "bar") // evaluates to Error "bar"

toArray result

Full Usage: toArray result

Parameters:
    result : Result<'T, 'Error> - The input result.

Returns: 'T[] The result array.

Convert the result to an array of length 0 or 1.

result : Result<'T, 'Error>

The input result.

Returns: 'T[]

The result array.

Example

 Error 42 |> Result.toArray // evaluates to [||]
 Ok 42 |> Result.toArray // evaluates to [| 42 |]

toList result

Full Usage: toList result

Parameters:
    result : Result<'T, 'Error> - The input result.

Returns: List<'T> The result list.

Convert the result to a list of length 0 or 1.

result : Result<'T, 'Error>

The input result.

Returns: List<'T>

The result list.

Example

 Error 42 |> Result.toList // evaluates to []
 Ok 42 |> Result.toList // evaluates to [ 42 ]

toOption result

Full Usage: toOption result

Parameters:
    result : Result<'T, 'Error> - The input result.

Returns: Option<'T> The option value.

Convert the result to an Option value.

result : Result<'T, 'Error>

The input result.

Returns: Option<'T>

The option value.

Example

 Error 42 |> Result.toOption // evaluates to None
 Ok 42 |> Result.toOption // evaluates to Some 42

toValueOption result

Full Usage: toValueOption result

Parameters:
    result : Result<'T, 'Error> - The input result.

Returns: ValueOption<'T> The result value.

Convert the result to an Option value.

result : Result<'T, 'Error>

The input result.

Returns: ValueOption<'T>

The result value.

Example

 Error 42 |> Result.toOption // evaluates to ValueNone
 Ok 42 |> Result.toOption // evaluates to ValueSome 42