Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/FSharpPlus/Extensions/Result.fs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,24 @@ module Result =
/// </remarks>
let defaultWith (defThunk: 'Error->'T) (result: Result<'T,'Error>) : 'T = match result with Ok v -> v | Error e -> defThunk e

/// <summary>Returns the first result if it is <c>Ok</c>, otherwise returns the second result.</summary>
/// <param name="alternative">The alternative result.</param>
/// <param name="source">The source result.</param>
/// <returns>The source result if it is <c>Ok</c>, otherwise the alternative result.</returns>
let orElse (alternative: Result<'T, 'Error>) (source: Result<'T, 'Error>) : Result<'T, 'Error> =
match source with
| Ok v -> Ok v
| Error _ -> alternative

/// <summary>Returns the first result if it is <c>Ok</c>, otherwise invokes the alternative thunk to obtain an alternative result.</summary>
/// <param name="alternativeThunk">A thunk that provides an alternative result when invoked.</param>
/// <param name="source">The source result.</param>
/// <returns>The source result if it is <c>Ok</c>, otherwise the result of invoking the alternative thunk.</returns>
let orElseWith (alternativeThunk: 'Error -> Result<'T, 'Error>) (source: Result<'T, 'Error>) : Result<'T, 'Error> =
match source with
| Ok v -> Ok v
| Error e -> alternativeThunk e

/// Converts a Result<'T,'Error> to a Choice<'T,'Error>.
let toChoice (source: Result<'T,'U>) = match source with Ok x-> Choice1Of2 x | Error x -> Choice2Of2 x

Expand Down
Loading