Skip to content

Commit

Permalink
Initial draft
Browse files Browse the repository at this point in the history
  • Loading branch information
gusty committed Sep 16, 2023
1 parent 1283d35 commit f3dba96
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 80 deletions.
1 change: 0 additions & 1 deletion src/FSharpPlus/Builders.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ namespace FSharpPlus
module GenericBuilders =

open FSharpPlus.Operators
open FSharpPlus.Data

// Idiom brackets
type Ii = Ii
Expand Down
11 changes: 0 additions & 11 deletions src/FSharpPlus/Control/Alternative.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace FSharpPlus.Control

open System.Runtime.InteropServices
open FSharpPlus
open FSharpPlus.Data
open FSharpPlus.Internals


Expand Down Expand Up @@ -38,8 +37,6 @@ type Empty with
type Append =
inherit Default1
static member ``<|>`` (x: 'T seq , y , [<Optional>]_mthd: Default2) = Seq.append x y
static member ``<|>`` (x: 'T NonEmptySeq , y , [<Optional>]_mthd: Default2) = NonEmptySeq.append x y

static member inline ``<|>`` (x: '``Alt<'T>`` , y: '``Alt<'T>``, [<Optional>]_mthd: Default1) = (^``Alt<'T>`` : (static member (<|>) : _*_ -> _) x, y) : '``Alt<'T>``
static member inline ``<|>`` (_: ^t when ^t: null and ^t: struct , _, _mthd: Default1) = ()

Expand Down Expand Up @@ -114,14 +111,6 @@ type Choice =
res <- Append.Invoke res e.Current
res

static member inline Choice (x: ref<NonEmptySeq<'``Alternative<'T>``>>, _mthd: Choice) =
use e = x.Value.GetEnumerator ()
e.MoveNext() |> ignore
let mutable res = e.Current
while e.MoveNext() && not (IsAltLeftZero.Invoke res) do
res <- Append.Invoke res e.Current
res

static member inline Choice (x: ref<list<'``Alternative<'T>``>>, _mthd: Choice) =
use e = (List.toSeq x.Value ).GetEnumerator ()
let mutable res = Empty.Invoke ()
Expand Down
13 changes: 4 additions & 9 deletions src/FSharpPlus/Control/Applicative.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ open Microsoft.FSharp.Quotations

open FSharpPlus.Internals
open FSharpPlus
open FSharpPlus.Data


type Apply =
Expand All @@ -20,8 +19,7 @@ type Apply =
static member inline ``<*>`` (f: '``Applicative<'T->'U>``, x: '``Applicative<'T>``, [<Optional>]_output: '``Applicative<'U>``, [<Optional>]_mthd:Default1) : '``Applicative<'U>`` = ((^``Applicative<'T->'U>`` or ^``Applicative<'T>`` or ^``Applicative<'U>``) : (static member (<*>) : _*_ -> _) f, x)

static member ``<*>`` (f: Lazy<'T->'U> , x: Lazy<'T> , [<Optional>]_output: Lazy<'U> , [<Optional>]_mthd: Apply) = Lazy.apply f x : Lazy<'U>
static member ``<*>`` (f: seq<_> , x: seq<'T> , [<Optional>]_output: seq<'U> , [<Optional>]_mthd: Apply) = Seq.apply f x : seq<'U>
static member ``<*>`` (f: NonEmptySeq<_> , x: NonEmptySeq<'T> , [<Optional>]_output: NonEmptySeq<'U> , [<Optional>]_mthd: Apply) = NonEmptySeq.apply f x : NonEmptySeq<'U>
static member ``<*>`` (f: seq<_> , x: seq<'T> , [<Optional>]_output: seq<'U> , [<Optional>]_mthd: Apply) = Seq.apply f x : seq<'U>
static member ``<*>`` (f: IEnumerator<_> , x: IEnumerator<'T> , [<Optional>]_output: IEnumerator<'U> , [<Optional>]_mthd: Apply) = Enumerator.map2 id f x : IEnumerator<'U>
static member ``<*>`` (f: list<_> , x: list<'T> , [<Optional>]_output: list<'U> , [<Optional>]_mthd: Apply) = List.apply f x : list<'U>
static member ``<*>`` (f: _ [] , x: 'T [] , [<Optional>]_output: 'U [] , [<Optional>]_mthd: Apply) = Array.apply f x : 'U []
Expand Down Expand Up @@ -93,8 +91,7 @@ type Lift2 =
inherit Default1

static member Lift2 (f, (x: Lazy<_> , y: Lazy<_> ), _mthd: Lift2) = Lazy.map2 f x y
static member Lift2 (f, (x: seq<_> , y: seq<_> ), _mthd: Lift2) = Seq.lift2 f x y
static member Lift2 (f, (x: NonEmptySeq<_> , y: NonEmptySeq<_> ), _mthd: Lift2) = NonEmptySeq.lift2 f x y
static member Lift2 (f, (x: seq<_> , y: seq<_> ), _mthd: Lift2) = Seq.lift2 f x y
static member Lift2 (f, (x: IEnumerator<_> , y: IEnumerator<_> ), _mthd: Lift2) = Enumerator.map2 f x y
static member Lift2 (f, (x , y ), _mthd: Lift2) = List.lift2 f x y
static member Lift2 (f, (x , y ), _mthd: Lift2) = Array.lift2 f x y
Expand Down Expand Up @@ -140,8 +137,7 @@ type Lift3 =
inherit Default1

static member Lift3 (f, (x: Lazy<_> , y: Lazy<_> , z: Lazy<_> ), _mthd: Lift3) = Lazy.map3 f x y z
static member Lift3 (f, (x: seq<_> , y: seq<_> , z: seq<_> ), _mthd: Lift3) = Seq.lift3 f x y z
static member Lift3 (f, (x: NonEmptySeq<_> , y: NonEmptySeq<_> , z: NonEmptySeq<_> ), _mthd: Lift3) = NonEmptySeq.lift3 f x y z
static member Lift3 (f, (x: seq<_> , y: seq<_> , z: seq<_> ), _mthd: Lift3) = Seq.lift3 f x y z
static member Lift3 (f, (x: IEnumerator<_> , y: IEnumerator<_> , z: IEnumerator<_> ), _mthd: Lift3) = Enumerator.map3 f x y z
static member Lift3 (f, (x , y , z ), _mthd: Lift3) = List.lift3 f x y z
static member Lift3 (f, (x , y , z ), _mthd: Lift3) = Array.lift3 f x y z
Expand Down Expand Up @@ -185,8 +181,7 @@ type Lift3 with
type IsLeftZero =
inherit Default1

static member IsLeftZero (t: ref<seq<_>> , _mthd: IsLeftZero) = Seq.isEmpty t.Value
static member IsLeftZero (_: ref<NonEmptySeq<_>>, _mthd: IsLeftZero) = false
static member IsLeftZero (t: ref<seq<_>> , _mthd: IsLeftZero) = Seq.isEmpty t.Value
static member IsLeftZero (t: ref<list<_>> , _mthd: IsLeftZero) = List.isEmpty t.Value
static member IsLeftZero (t: ref<array<_>> , _mthd: IsLeftZero) = Array.isEmpty t.Value
static member IsLeftZero (t: ref<option<_>> , _mthd: IsLeftZero) = Option.isNone t.Value
Expand Down
10 changes: 3 additions & 7 deletions src/FSharpPlus/Control/Foldable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ open System.Runtime.InteropServices
open System.Text
open System.Collections.Generic
open FSharpPlus
open FSharpPlus.Data
open FSharpPlus.Internals
open FSharpPlus.Internals.Prelude

Expand Down Expand Up @@ -231,8 +230,7 @@ type Head =
static member inline Head (x: '``Foldable<'T>``, [<Optional>]_impl: Default2) = Seq.head (ToSeq.Invoke x) : 'T
static member inline Head (x: '``Foldable<'T>``, [<Optional>]_impl: Default1) = (^``Foldable<'T>`` : (member Head : 'T) x)
static member Head (x: 'T option , [<Optional>]_impl: Head ) = x.Value
static member Head (x: 'T [] , [<Optional>]_impl: Head ) = x.[0]
static member Head (x: NonEmptySeq<'T> , [<Optional>]_impl: Head ) = x.First
static member Head (x: 'T [] , [<Optional>]_impl: Head ) = x.[0]
static member Head (x: Id<'T> , [<Optional>]_impl: Head ) = x.getValue
static member Head (x: ResizeArray<'T> , [<Optional>]_impl: Head ) = x.[0]
static member Head (x: string , [<Optional>]_impl: Head ) = x.[0]
Expand All @@ -247,8 +245,7 @@ type TryHead =
inherit Default1
static member inline TryHead (x , [<Optional>]_impl: Default1) = Seq.tryHead <| ToSeq.Invoke x
static member TryHead (x: 't list , [<Optional>]_impl: TryHead ) = List.tryHead x
static member TryHead (x: 't [] , [<Optional>]_impl: TryHead ) = Array.tryHead x
static member TryHead (x: NonEmptySeq<'T>,[<Optional>]_impl: TryHead) = Some x.First
static member TryHead (x: 't [] , [<Optional>]_impl: TryHead ) = Array.tryHead x
static member TryHead (x: Id<'T> , [<Optional>]_impl: TryHead ) = Some x.getValue
static member TryHead (x: string , [<Optional>]_impl: TryHead ) = String.tryHead x
static member TryHead (x: StringBuilder, [<Optional>]_impl: TryHead ) = if x.Length = 0 then None else Some (x.ToString().[0])
Expand All @@ -263,8 +260,7 @@ type TryLast =
inherit Default1
static member inline TryLast (x , [<Optional>]_impl: Default1) = Seq.tryLast <| ToSeq.Invoke x
static member TryLast (x: 't list , [<Optional>]_impl: TryLast) = List.tryLast x
static member TryLast (x: 't [] , [<Optional>]_impl: TryLast) = Array.tryLast x
static member TryLast (x: NonEmptySeq<'T>, [<Optional>]_impl: TryLast) = Some <| Seq.last x
static member TryLast (x: 't [] , [<Optional>]_impl: TryLast) = Array.tryLast x
static member TryLast (x: Id<'T> , [<Optional>]_impl: TryLast ) = Some x.getValue
static member TryLast (x: string , [<Optional>]_impl: TryLast ) = String.tryLast x
static member TryLast (x: StringBuilder , [<Optional>]_impl: TryLast ) = if x.Length = 0 then None else Some (x.ToString().[x.Length - 1])
Expand Down
10 changes: 3 additions & 7 deletions src/FSharpPlus/Control/Functor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ open Microsoft.FSharp.Quotations
open FSharpPlus.Internals
open FSharpPlus.Internals.Prelude
open FSharpPlus
open FSharpPlus.Data

#if (!FABLE_COMPILER || FABLE_COMPILER_3) && ! FABLE_COMPILER_4

Expand Down Expand Up @@ -125,8 +124,7 @@ type Map with
and '``Applicative<'T->'U>`` : (static member Return : ('T -> 'U) -> '``Applicative<'T->'U>``)
, f: 'T->'U), [<Optional>]_mthd: Default3) = Apply.InvokeOnInstance (Return.InvokeOnInstance f: '``Applicative<'T->'U>``) x : '``Applicative<'U>``

static member Map ((x: seq<_> , f: 'T->'U), _mthd: Default2) = Seq.map f x : seq<'U>
static member Map ((x: NonEmptySeq<_> , f: 'T->'U), _mthd: Default2) = NonEmptySeq.map f x : NonEmptySeq<'U>
static member Map ((x: seq<_> , f: 'T->'U), _mthd: Default2) = Seq.map f x : seq<'U>
static member Map ((x: IEnumerator<_> , f: 'T->'U), _mthd: Default2) = Enumerator.map f x : IEnumerator<'U>
static member Map ((x: IDictionary<_,_> , f: 'T->'U), _mthd: Default2) = Dict.map f x : IDictionary<'Key,'U>
static member Map ((x: IReadOnlyDictionary<_,_>, f: 'T->'U), _mthd: Default2) = IReadOnlyDictionary.mapValues f x : IReadOnlyDictionary<'Key,_>
Expand Down Expand Up @@ -183,8 +181,7 @@ type Unzip =

static member Unzip ((source: ResizeArray<'T * 'U> , _output: ResizeArray<'T> * ResizeArray<'U> ) , _mthd: Unzip ) = Map.Invoke fst source, Map.Invoke snd source

static member Unzip ((source: seq<'T * 'U> , _output: seq<'T> * seq<'U> ) , _mthd: Unzip ) = Map.Invoke fst source, Map.Invoke snd source
static member Unzip ((source: NonEmptySeq<'T * 'U> , _output: NonEmptySeq<'T> * NonEmptySeq<'U> ) , _mthd: Unzip ) = Map.Invoke fst source, Map.Invoke snd source
static member Unzip ((source: seq<'T * 'U> , _output: seq<'T> * seq<'U> ) , _mthd: Unzip ) = Map.Invoke fst source, Map.Invoke snd source

static member Unzip ((source: IEnumerator<'T * 'U> , _output: IEnumerator<'T> * ResizeArray<'U> ) , _mthd: Unzip ) = Map.Invoke fst source, Map.Invoke snd source
static member Unzip ((source: IDictionary<'Key, 'T * 'U> , _output: IDictionary<_,'T> * IDictionary<_,'U> ) , _mthd: Unzip ) = Dict.unzip source
Expand All @@ -204,8 +201,7 @@ type Unzip =
type Zip =
inherit Default1
static member Zip ((x: IEnumerator<'T> , y: IEnumerator<'U> , _output: IEnumerator<'T*'U> ), _mthd: Zip) = Enumerator.zip x y
static member Zip ((x: seq<'T> , y: seq<'U> , _output: seq<'T*'U> ), _mthd: Zip) = Seq.zip x y
static member Zip ((x: NonEmptySeq<'T> , y: NonEmptySeq<'U> , _output: NonEmptySeq<'T*'U> ), _mthd: Zip) = NonEmptySeq.zip x y
static member Zip ((x: seq<'T> , y: seq<'U> , _output: seq<'T*'U> ), _mthd: Zip) = Seq.zip x y
static member Zip ((x: IDictionary<'K, 'T> , y: IDictionary<'K,'U> , _output: IDictionary<'K,'T*'U> ), _mthd: Zip) = Dict.zip x y
static member Zip ((x: IReadOnlyDictionary<'K, 'T>, y: IReadOnlyDictionary<'K,'U>, _output: IReadOnlyDictionary<'K,'T*'U>), _mthd: Zip) = IReadOnlyDictionary.zip x y
static member Zip ((x: Dictionary<'K, 'T> , y: Dictionary<'K,'U> , _output: Dictionary<'K,'T*'U> ), _mthd: Zip) = Dict.zip x y :?> Dictionary<'K,'T*'U>
Expand Down
10 changes: 0 additions & 10 deletions src/FSharpPlus/Control/Monad.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ open System.Threading.Tasks
open Microsoft.FSharp.Quotations

open FSharpPlus
open FSharpPlus.Data
open FSharpPlus.Internals
open FSharpPlus.Internals.Prelude

Expand Down Expand Up @@ -60,8 +59,6 @@ type Bind =

static member (>>=) (source: ResizeArray<'T>, f: 'T -> ResizeArray<'U>) = ResizeArray (Seq.bind (f >> seq<_>) source) : ResizeArray<'U>

static member (>>=) (source: NonEmptySeq<'T>, f: 'T -> NonEmptySeq<'U>) = NonEmptySeq.collect f source : NonEmptySeq<'U>

#if !FABLE_COMPILER || FABLE_COMPILER_3
static member inline Invoke (source: '``Monad<'T>``) (binder: 'T -> '``Monad<'U>``) : '``Monad<'U>`` =
let inline call (_mthd: 'M, input: 'I, _output: 'R, f) = ((^M or ^I or ^R) : (static member (>>=) : _*_ -> _) input, f)
Expand Down Expand Up @@ -115,8 +112,6 @@ type Join =
dct

static member Join (x: ResizeArray<ResizeArray<'T>> , [<Optional>]_output: ResizeArray<'T> , [<Optional>]_mthd: Join) = ResizeArray (Seq.bind seq<_> x) : ResizeArray<'T>

static member Join (x: NonEmptySeq<NonEmptySeq<'T>> , [<Optional>]_output: NonEmptySeq<'T> , [<Optional>]_mthd: Join) = NonEmptySeq.concat x : NonEmptySeq<'T>

static member inline Invoke (source: '``Monad<Monad<'T>>``) : '``Monad<'T>`` =
let inline call (mthd: 'M, input: 'I, output: 'R) = ((^M or ^I or ^R) : (static member Join : _*_*_ -> _) input, output, mthd)
Expand All @@ -137,7 +132,6 @@ type Return =


static member Return (_: seq<'a> , _: Default2) = fun x -> Seq.singleton x : seq<'a>
static member Return (_: NonEmptySeq<'a>, _: Default2) = fun x -> NonEmptySeq.singleton x : NonEmptySeq<'a>
static member Return (_: IEnumerator<'a>, _: Default2) = fun x -> Enumerator.upto None (fun _ -> x) : IEnumerator<'a>
static member inline Return (_: 'R , _: Default1) = fun (x: 'T) -> Return.InvokeOnInstance x : 'R
static member Return (_: Lazy<'a> , _: Return ) = fun x -> Lazy<_>.CreateFromValue x : Lazy<'a>
Expand Down Expand Up @@ -179,7 +173,6 @@ type Delay =
static member inline Delay (_mthd: Default1, _: unit-> ^t when ^t : null and ^t : struct , _ ) = ()

static member Delay (_mthd: Default2, x: unit-> _ , _ ) = Seq.delay x : seq<'T>
static member Delay (_mthd: Default2, x: unit-> _ , _ ) = NonEmptySeq.delay x : NonEmptySeq<'T>
static member Delay (_mthd: Default2, x: unit-> 'R -> _ , _ ) = (fun s -> x () s): 'R -> _
static member Delay (_mthd: Delay , x: unit-> _ , _ ) = async.Delay x : Async<'T>
static member Delay (_mthd: Delay , x: unit-> Task<_> , _ ) = x () : Task<'T>
Expand Down Expand Up @@ -239,7 +232,6 @@ type TryWith =
static member inline TryWith (_: unit -> ^t when ^t: null and ^t: struct, _ : exn -> 't , _: Default1, _) = ()

static member TryWith (computation: unit -> seq<_> , catchHandler: exn -> seq<_> , _: Default2, _) = seq (try (Seq.toArray (computation ())) with e -> Seq.toArray (catchHandler e))
static member TryWith (computation: unit -> NonEmptySeq<_>, catchHandler: exn -> NonEmptySeq<_>, _: Default2, _) = seq (try (Seq.toArray (computation ())) with e -> Seq.toArray (catchHandler e)) |> NonEmptySeq.unsafeOfSeq
static member TryWith (computation: unit -> 'R -> _ , catchHandler: exn -> 'R -> _ , _: Default2, _) = (fun s -> try (computation ()) s with e -> catchHandler e s) : 'R ->_
static member TryWith (computation: unit -> Async<_> , catchHandler: exn -> Async<_> , _: TryWith , _) = async.TryWith ((computation ()), catchHandler)
#if !FABLE_COMPILER
Expand All @@ -264,7 +256,6 @@ type TryFinally =
inherit Default1

static member TryFinally ((computation: unit -> seq<_> , compensation: unit -> unit), _: Default2, _, _) = seq { try for e in computation () do yield e finally compensation () }
static member TryFinally ((computation: unit -> NonEmptySeq<_>, compensation: unit -> unit), _: Default2, _, _) = seq { try for e in computation () do yield e finally compensation () } |> NonEmptySeq.unsafeOfSeq

[<CompilerMessage(MessageTryFinally, CodeTryFinally, IsError = true)>]
static member TryFinally ((_: unit -> 'R -> _ , _: unit -> unit), _: Default2 , _, _defaults: False) = raise Internals.Errors.exnUnreachable
Expand Down Expand Up @@ -306,7 +297,6 @@ type Using =
inherit Default1

static member Using (resource: 'T when 'T :> IDisposable, body: 'T -> seq<'U> , _: Using) = seq { try for e in body resource do yield e finally if not (isNull (box resource)) then resource.Dispose () } : seq<'U>
static member Using (resource: 'T when 'T :> IDisposable, body: 'T -> NonEmptySeq<'U>, _: Using) = seq { try for e in body resource do yield e finally if not (isNull (box resource)) then resource.Dispose () } |> NonEmptySeq.unsafeOfSeq : NonEmptySeq<'U>
static member Using (resource: 'T when 'T :> IDisposable, body: 'T -> 'R -> 'U , _: Using ) = (fun s -> try body resource s finally if not (isNull (box resource)) then resource.Dispose ()) : 'R->'U
static member Using (resource: 'T when 'T :> IDisposable, body: 'T -> Async<'U>, _: Using ) = async.Using (resource, body)
#if !FABLE_COMPILER
Expand Down
2 changes: 0 additions & 2 deletions src/FSharpPlus/Control/Monoid.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ open System.Runtime.InteropServices
open Microsoft.FSharp.Quotations
open System.Threading.Tasks
open FSharpPlus
open FSharpPlus.Data
open FSharpPlus.Internals
open FSharpPlus.Internals.Prelude

Expand Down Expand Up @@ -156,7 +155,6 @@ type Plus with
#if !FABLE_COMPILER
static member inline ``+`` (x: IReadOnlyDictionary<'K,'V>, y: IReadOnlyDictionary<'K,'V>, [<Optional>]_mthd: Default3) = IReadOnlyDictionary.unionWith Plus.Invoke x y
#endif
static member inline ``+`` (x: _ NonEmptySeq , y: _ NonEmptySeq , [<Optional>]_mthd: Default3) = NonEmptySeq.append x y



Expand Down
Loading

0 comments on commit f3dba96

Please sign in to comment.