Skip to content

Commit 97b6ad2

Browse files
gustywallymathieu
authored andcommitted
+ (Value)Option.either
1 parent a9770e5 commit 97b6ad2

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

src/FSharpPlus/Extensions/Option.fs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,16 @@ module Option =
7070
match pair with
7171
| (true, x) -> Some x
7272
| (false, _) -> None
73+
74+
/// <summary>
75+
/// Extracts a value from either side of an Option.
76+
/// </summary>
77+
/// <param name="fSome">The function to apply if the option is Some.</param>
78+
/// <param name="fNone">The function to apply if the option is None.</param>
79+
/// <param name="source">The option to extract the value from.</param>
80+
#if !NET45
81+
let inline either ([<InlineIfLambda>]fSome: 'T -> 'U) ([<InlineIfLambda>]fNone: unit -> 'U) (source: option<'T>) : 'U =
82+
#else
83+
let inline either (fSome: 'T -> 'U) (fNone: unit -> 'U) (source: option<'T>) : 'U =
84+
#endif
85+
match source with Some v -> fSome v | None -> fNone ()

src/FSharpPlus/Extensions/ValueOption.fs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,17 @@ module ValueOption =
8181
let ofOption (source: option<'T>) =
8282
match source with
8383
| Some x -> ValueSome x
84-
| None -> ValueNone
84+
| None -> ValueNone
85+
86+
/// <summary>
87+
/// Extracts a value from either side of a ValueOption.
88+
/// </summary>
89+
/// <param name="fSome">The function to apply if the option is ValueSome.</param>
90+
/// <param name="fNone">The function to apply if the option is ValueNone.</param>
91+
/// <param name="source">The option to extract the value from.</param>
92+
#if !NET45
93+
let inline either ([<InlineIfLambda>]fSome: 'T -> 'U) ([<InlineIfLambda>]fNone: unit -> 'U) (source: ValueOption<'T>) : 'U =
94+
#else
95+
let inline either (fSome: 'T -> 'U) (fNone: unit -> 'U) (source: ValueOption<'T>) : 'U =
96+
#endif
97+
match source with ValueSome v -> fSome v | ValueNone -> fNone ()

0 commit comments

Comments
 (0)