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"

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"