ResultModule
PackageReified.Result
Fail-fast helpers over the standard F#
Result type.Summary
| Name | Signature | Synopsis |
|---|---|---|
| Operations | ||
| ok | ok value | Creates an Ok result. |
| error | error failure | Creates an Error result. |
| map | map mapper result | Maps the success value of a result. |
| mapError | mapError mapper result | Maps the error value of a result. |
| bind | bind binder result | Binds a result to the next fail-fast operation. |
| orElseWith | orElseWith fallback result | Computes a fallback result from the source error when the result fails. |
| orElse | orElse fallback result | Falls back to another result when the source result fails. |
| okIf | okIf predicate input | Keeps the input value when the predicate holds, or returns the supplied error. |
| failIf | failIf predicate input | Keeps the input value when the predicate does not hold, or returns the supplied error. |
| require | require condition | Requires an already-computed condition where there is no subject value to preserve. |
| orError | orError failure result | Replaces whatever error a result carries with the supplied typed error. |
| fromTry | fromTry (tryResult, tryResult) | Converts a .NET Try* tuple into a unit-error result. |
| fromChoice | fromChoice choice | Converts an F# Choice into a result. |
| fromOption | fromOption value | Converts an option into a unit-error result. |
| fromValueOption | fromValueOption value | Converts a value option into a unit-error result. |
| toOption | toOption result | Drops the error channel and returns Some for success. |
| toValueOption | toValueOption result | Drops the error channel and returns ValueSome for success. |
| defaultValue | defaultValue fallback result | Returns the success value or the supplied fallback value. |
| tap | tap effect result | Runs a side effect on the successful value and returns the result unchanged. |
| tapError | tapError effect result | Runs a side effect on the error value and returns the result unchanged. |
| traverse | traverse mapping values | Maps each value with a result-returning function, stopping at the first error. |
| sequence | sequence values | Turns a sequence of results into one fail-fast result containing all successes. |
| traverseAll | traverseAll mapping values | Maps each value with a result-returning function, running every mapping and collecting every error. |
| sequenceAll | sequenceAll values | Turns a sequence of results into one result containing all successes, or every error. |
Operations
kind:member
kind:member
kind:member
kind:member
kind:member
kind:member
Computes a fallback result from the source error when the result fails.
Parameters
| Name | Type | Description |
|---|---|---|
| fallback | 'error -> Result<'value, 'error> | |
| result | Result<'value, 'error> |
Returns
Result<'value, 'error>
Verification Examples
let result = Error "boom"
result |> Result.orElseWith (fun error -> Ok (String.length error)) // Ok 4kind:member
kind:member
kind:member
kind:member
kind:member
kind:member
kind:member
kind:member
kind:member
kind:member
kind:member
kind:member
kind:member
Runs a side effect on the successful value and returns the result unchanged.
Parameters
| Name | Type | Description |
|---|---|---|
| effect | 'value -> unit | |
| result | Result<'value, 'error> |
Returns
Result<'value, 'error>
Verification Examples
Ok 10 |> Result.tap (printfn "loaded %d") // prints, then returns Ok 10kind:member
Runs a side effect on the error value and returns the result unchanged.
Parameters
| Name | Type | Description |
|---|---|---|
| effect | 'error -> unit | |
| result | Result<'value, 'error> |
Returns
Result<'value, 'error>
Verification Examples
Error "boom" |> Result.tapError (printfn "failed: %s") // prints, then returns Error "boom"kind:member
Maps each value with a result-returning function, stopping at the first error.
Parameters
| Name | Type | Description |
|---|---|---|
| mapping | 'input -> Result<'output, 'error> | |
| values | 'input seq |
Returns
Result<'output list, 'error>
Verification Examples
[ "1"; "2" ] |> Result.traverse parseInt // Ok [ 1; 2 ]kind:member
kind:member
Maps each value with a result-returning function, running every mapping and collecting every error.
Parameters
| Name | Type | Description |
|---|---|---|
| mapping | 'input -> Result<'output, 'error> | |
| values | 'input seq |
Returns
Result<'output list, 'error list>
Verification Examples
[ "1"; "x"; "y" ] |> Result.traverseAll parseInt // Error [ NotANumber "x"; NotANumber "y" ]kind:member
Turns a sequence of results into one result containing all successes, or every error.
Parameters
| Name | Type | Description |
|---|---|---|
| values | Result<'value, 'error> seq |
Returns
Result<'value list, 'error list>
Verification Examples
[ Ok 1; Error "missing"; Error "invalid" ] |> Result.sequenceAll // Error [ "missing"; "invalid" ]