Skip to content

Commit 197b697

Browse files
authored
+ functions as Alternatives (conditionally) (#535)
1 parent 6c3cfdf commit 197b697

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

docsrc/content/abstraction-alternative.fsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ From .Net/F#
5959
- ``option<'T>``
6060
- ``array<'T>``
6161
- ``seq<'T>``
62+
- ``'T -> 'Alternative``
6263
6364
From F#+
6465
@@ -157,4 +158,4 @@ let r6 = map string (Just 6)
157158
// But not always the Monoidal behaviour is the same
158159

159160
let r3 = Some 2 ++ Some 1 // addition => Some 3
160-
let r2 = Some 2 <|> Some 1 // first success => Some 2
161+
let r2 = Some 2 <|> Some 1 // first success => Some 2

src/FSharpPlus/Control/Alternative.fs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ type Empty =
2424
static member Empty ([<Optional>]_output: option<'T> , [<Optional>]_mthd: Empty ) = None : option<'T>
2525
static member Empty ([<Optional>]_output: voption<'T> , [<Optional>]_mthd: Empty ) = ValueNone : voption<'T>
2626
static member Empty ([<Optional>]_output: list<'T> , [<Optional>]_mthd: Empty ) = [ ] : list<'T>
27-
static member Empty ([<Optional>]_output: 'T [] , [<Optional>]_mthd: Empty ) = [||] : 'T []
27+
static member Empty ([<Optional>]_output: 'T [] , [<Optional>]_mthd: Empty ) = [||] : 'T []
2828

2929
static member inline Invoke () : '``Alternative<'T>`` =
3030
let inline call (mthd: ^M, output: ^R) = ((^M or ^R) : (static member Empty : _*_ -> _) output, mthd)
3131
call (Unchecked.defaultof<Empty>, Unchecked.defaultof<'``Alternative<'T>``> )
3232

3333
static member inline InvokeOnInstance () : '``Alternative<'T>`` = (^``Alternative<'T>`` : (static member Empty : ^``Alternative<'T>``) ()) : '``Alternative<'T>``
3434

35+
type Empty with
36+
static member inline Empty ([<Optional>]_output: 'R -> '``Alternative<'T>``, [<Optional>]_mthd: Empty) = (fun _ -> Empty.Invoke ()) : 'R -> '``Alternative<'T>``
3537

3638
type Append =
3739
inherit Default1
@@ -53,6 +55,8 @@ type Append =
5355
let inline call (mthd: ^M, input1: ^I, input2: ^I) = ((^M or ^I) : (static member ``<|>`` : _*_*_ -> _) input1, input2, mthd)
5456
call (Unchecked.defaultof<Append>, x, y)
5557

58+
type Append with
59+
static member inline ``<|>`` (x: 'R -> '``Alt<'T>`` , y , [<Optional>]_mthd: Append ) = fun r -> Append.Invoke (x r) (y r)
5660

5761

5862
type IsAltLeftZero =

0 commit comments

Comments
 (0)