diff --git a/src/FSharpPlus/Builders.fs b/src/FSharpPlus/Builders.fs index 0936ae1df..4d4c0877e 100644 --- a/src/FSharpPlus/Builders.fs +++ b/src/FSharpPlus/Builders.fs @@ -18,7 +18,6 @@ namespace FSharpPlus module GenericBuilders = open FSharpPlus.Operators - open FSharpPlus.Data // Idiom brackets type Ii = Ii diff --git a/src/FSharpPlus/Control/Alternative.fs b/src/FSharpPlus/Control/Alternative.fs index 9592be0d0..fbc5975fa 100644 --- a/src/FSharpPlus/Control/Alternative.fs +++ b/src/FSharpPlus/Control/Alternative.fs @@ -10,7 +10,6 @@ namespace FSharpPlus.Control open System.Runtime.InteropServices open FSharpPlus -open FSharpPlus.Data open FSharpPlus.Internals @@ -38,8 +37,6 @@ type Empty with type Append = inherit Default1 static member ``<|>`` (x: 'T seq , y , []_mthd: Default2) = Seq.append x y - static member ``<|>`` (x: 'T NonEmptySeq , y , []_mthd: Default2) = NonEmptySeq.append x y - static member inline ``<|>`` (x: '``Alt<'T>`` , y: '``Alt<'T>``, []_mthd: Default1) = (^``Alt<'T>`` : (static member (<|>) : _*_ -> _) x, y) : '``Alt<'T>`` static member inline ``<|>`` (_: ^t when ^t: null and ^t: struct , _, _mthd: Default1) = () @@ -114,14 +111,6 @@ type Choice = res <- Append.Invoke res e.Current res - static member inline Choice (x: ref``>>, _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``>>, _mthd: Choice) = use e = (List.toSeq x.Value ).GetEnumerator () let mutable res = Empty.Invoke () diff --git a/src/FSharpPlus/Control/Applicative.fs b/src/FSharpPlus/Control/Applicative.fs index 880b7c470..ac2898e50 100644 --- a/src/FSharpPlus/Control/Applicative.fs +++ b/src/FSharpPlus/Control/Applicative.fs @@ -8,7 +8,6 @@ open Microsoft.FSharp.Quotations open FSharpPlus.Internals open FSharpPlus -open FSharpPlus.Data type Apply = @@ -18,7 +17,6 @@ type Apply = static member ``<*>`` (struct (f: Lazy<'T->'U> , x: Lazy<'T> ) , _output: Lazy<'U> , []_mthd: Apply) = Lazy.apply f x : Lazy<'U> static member ``<*>`` (struct (f: seq<_> , x: seq<'T> ) , _output: seq<'U> , []_mthd: Apply) = Seq.apply f x : seq<'U> - static member ``<*>`` (struct (f: NonEmptySeq<_> , x: NonEmptySeq<'T> ) , _output: NonEmptySeq<'U> , []_mthd: Apply) = NonEmptySeq.apply f x : NonEmptySeq<'U> static member ``<*>`` (struct (f: IEnumerator<_> , x: IEnumerator<'T> ) , _output: IEnumerator<'U> , []_mthd: Apply) = Enumerator.map2 id f x : IEnumerator<'U> static member ``<*>`` (struct (f: list<_> , x: list<'T> ) , _output: list<'U> , []_mthd: Apply) = List.apply f x : list<'U> static member ``<*>`` (struct (f: _ [] , x: 'T [] ) , _output: 'U [] , []_mthd: Apply) = Array.apply f x : 'U [] @@ -100,8 +98,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 @@ -147,8 +144,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 @@ -192,8 +188,7 @@ type Lift3 with type IsLeftZero = inherit Default1 - static member IsLeftZero (t: ref> , _mthd: IsLeftZero) = Seq.isEmpty t.Value - static member IsLeftZero (_: ref>, _mthd: IsLeftZero) = false + static member IsLeftZero (t: ref> , _mthd: IsLeftZero) = Seq.isEmpty t.Value static member IsLeftZero (t: ref> , _mthd: IsLeftZero) = List.isEmpty t.Value static member IsLeftZero (t: ref> , _mthd: IsLeftZero) = Array.isEmpty t.Value static member IsLeftZero (t: ref> , _mthd: IsLeftZero) = Option.isNone t.Value diff --git a/src/FSharpPlus/Control/Foldable.fs b/src/FSharpPlus/Control/Foldable.fs index 60f4dc1c2..c7c8c3b4c 100644 --- a/src/FSharpPlus/Control/Foldable.fs +++ b/src/FSharpPlus/Control/Foldable.fs @@ -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 @@ -231,8 +230,7 @@ type Head = static member inline Head (x: '``Foldable<'T>``, []_impl: Default2) = Seq.head (ToSeq.Invoke x) : 'T static member inline Head (x: '``Foldable<'T>``, []_impl: Default1) = (^``Foldable<'T>`` : (member Head : 'T) x) static member Head (x: 'T option , []_impl: Head ) = x.Value - static member Head (x: 'T [] , []_impl: Head ) = x.[0] - static member Head (x: NonEmptySeq<'T> , []_impl: Head ) = x.First + static member Head (x: 'T [] , []_impl: Head ) = x.[0] static member Head (x: Id<'T> , []_impl: Head ) = x.getValue static member Head (x: ResizeArray<'T> , []_impl: Head ) = x.[0] static member Head (x: string , []_impl: Head ) = x.[0] @@ -247,8 +245,7 @@ type TryHead = inherit Default1 static member inline TryHead (x , []_impl: Default1) = Seq.tryHead <| ToSeq.Invoke x static member TryHead (x: 't list , []_impl: TryHead ) = List.tryHead x - static member TryHead (x: 't [] , []_impl: TryHead ) = Array.tryHead x - static member TryHead (x: NonEmptySeq<'T>,[]_impl: TryHead) = Some x.First + static member TryHead (x: 't [] , []_impl: TryHead ) = Array.tryHead x static member TryHead (x: Id<'T> , []_impl: TryHead ) = Some x.getValue static member TryHead (x: string , []_impl: TryHead ) = String.tryHead x static member TryHead (x: StringBuilder, []_impl: TryHead ) = if x.Length = 0 then None else Some (x.ToString().[0]) @@ -263,8 +260,7 @@ type TryLast = inherit Default1 static member inline TryLast (x , []_impl: Default1) = Seq.tryLast <| ToSeq.Invoke x static member TryLast (x: 't list , []_impl: TryLast) = List.tryLast x - static member TryLast (x: 't [] , []_impl: TryLast) = Array.tryLast x - static member TryLast (x: NonEmptySeq<'T>, []_impl: TryLast) = Some <| Seq.last x + static member TryLast (x: 't [] , []_impl: TryLast) = Array.tryLast x static member TryLast (x: Id<'T> , []_impl: TryLast ) = Some x.getValue static member TryLast (x: string , []_impl: TryLast ) = String.tryLast x static member TryLast (x: StringBuilder , []_impl: TryLast ) = if x.Length = 0 then None else Some (x.ToString().[x.Length - 1]) diff --git a/src/FSharpPlus/Control/Functor.fs b/src/FSharpPlus/Control/Functor.fs index 2058fd069..31cc1fd3d 100644 --- a/src/FSharpPlus/Control/Functor.fs +++ b/src/FSharpPlus/Control/Functor.fs @@ -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 @@ -126,8 +125,7 @@ type Map with and '``Applicative<'T->'U>`` : (static member Return : ('T -> 'U) -> '``Applicative<'T->'U>``) , f: 'T->'U), []_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,_> @@ -184,8 +182,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 @@ -205,8 +202,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> diff --git a/src/FSharpPlus/Control/Monad.fs b/src/FSharpPlus/Control/Monad.fs index aa7961006..393b6579d 100644 --- a/src/FSharpPlus/Control/Monad.fs +++ b/src/FSharpPlus/Control/Monad.fs @@ -8,7 +8,6 @@ open System.Threading.Tasks open Microsoft.FSharp.Quotations open FSharpPlus -open FSharpPlus.Data open FSharpPlus.Internals open FSharpPlus.Internals.Prelude @@ -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) @@ -115,8 +112,6 @@ type Join = dct static member Join (x: ResizeArray> , []_output: ResizeArray<'T> , []_mthd: Join) = ResizeArray (Seq.bind seq<_> x) : ResizeArray<'T> - - static member Join (x: NonEmptySeq> , []_output: NonEmptySeq<'T> , []_mthd: Join) = NonEmptySeq.concat x : NonEmptySeq<'T> static member inline Invoke (source: '``Monad>``) : '``Monad<'T>`` = let inline call (mthd: 'M, input: 'I, output: 'R) = ((^M or ^I or ^R) : (static member Join : _*_*_ -> _) input, output, mthd) @@ -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> @@ -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> @@ -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 @@ -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 [] static member TryFinally ((_: unit -> 'R -> _ , _: unit -> unit), _: Default2 , _, _defaults: False) = raise Internals.Errors.exnUnreachable @@ -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 diff --git a/src/FSharpPlus/Control/Monoid.fs b/src/FSharpPlus/Control/Monoid.fs index 2f2a6d223..57449c87a 100644 --- a/src/FSharpPlus/Control/Monoid.fs +++ b/src/FSharpPlus/Control/Monoid.fs @@ -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 @@ -156,7 +155,6 @@ type Plus with #if !FABLE_COMPILER static member inline ``+`` (x: IReadOnlyDictionary<'K,'V>, y: IReadOnlyDictionary<'K,'V>, []_mthd: Default3) = IReadOnlyDictionary.unionWith Plus.Invoke x y #endif - static member inline ``+`` (x: _ NonEmptySeq , y: _ NonEmptySeq , []_mthd: Default3) = NonEmptySeq.append x y diff --git a/src/FSharpPlus/Control/Traversable.fs b/src/FSharpPlus/Control/Traversable.fs index 9b4363c3d..062181e13 100644 --- a/src/FSharpPlus/Control/Traversable.fs +++ b/src/FSharpPlus/Control/Traversable.fs @@ -5,7 +5,6 @@ open System.Runtime.InteropServices open System.ComponentModel open FSharpPlus -open FSharpPlus.Data open FSharpPlus.Internals open FSharpPlus.Internals.MonadOps open FSharpPlus.Extensions @@ -50,14 +49,6 @@ type Traverse = let cons_f x ys = Map.Invoke (cons: 'a -> seq<_> -> seq<_>) (f x) <*> ys Seq.foldBack cons_f t (result Seq.empty) - static member inline Traverse (t: _ NonEmptySeq, f, []_output: 'R, []_impl: Default3) = - #if TEST_TRACE - Traces.add "Traverse NonEmptySeq" - #endif - let cons x y = seq {yield x; yield! y} - let cons_f x ys = Map.Invoke (cons: 'a -> seq<_> -> seq<_>) (f x) <*> ys - Map.Invoke NonEmptySeq.ofSeq (Seq.foldBack cons_f t (result Seq.empty)) - static member inline Traverse (t: seq<'T>, f: 'T -> '``Functor<'U>``, []_output: '``Functor>``, []_impl: Default2) = #if TEST_TRACE Traces.add "Traverse seq, 'T -> Functor<'U>" @@ -65,13 +56,6 @@ type Traverse = let mapped = Seq.map f t Sequence.ForInfiniteSequences (mapped, IsLeftZero.Invoke, List.toSeq) : '``Functor>`` - static member inline Traverse (t: NonEmptySeq<'T>, f: 'T -> '``Functor<'U>``, []_output: '``Functor>``, []_impl: Default2) = - #if TEST_TRACE - Traces.add "Traverse NonEmptySeq, 'T -> Functor<'U>" - #endif - let mapped = NonEmptySeq.map f t - Sequence.ForInfiniteSequences (mapped, IsLeftZero.Invoke, NonEmptySeq.ofList) : '``Functor>`` - static member inline Traverse (t: ^a, f, []_output: 'R, []_impl: Default1) : 'R = #if TEST_TRACE Traces.add "Traverse ^a" @@ -91,19 +75,6 @@ type Traverse = while enum.MoveNext() do yield Async.RunSynchronously (f enum.Current, cancellationToken = ct) }} #endif - - #if !FABLE_COMPILER - static member Traverse (t: 't NonEmptySeq, f: 't -> Async<'u>, []_output: Async>, []_impl: Traverse) : Async> = async { - #if TEST_TRACE - Traces.add "Traverse 't NonEmptySeq, 't -> Async<'u>" - #endif - - let! ct = Async.CancellationToken - return seq { - use enum = t.GetEnumerator () - while enum.MoveNext() do - yield Async.RunSynchronously (f enum.Current, cancellationToken = ct) } |> NonEmptySeq.unsafeOfSeq } - #endif static member Traverse (t: Id<'t>, f: 't -> option<'u>, []_output: option>, []_impl: Traverse) = #if TEST_TRACE @@ -200,15 +171,6 @@ type Sequence with #if !FABLE_COMPILER static member Sequence (t: seq> , []_output: Async> , []_impl: Default3) : Async> = Async.Sequence t #endif - static member inline Sequence (t: NonEmptySeq<'``Applicative<'T>``>, []_output: '``Applicative>`` , []_impl: Default4) : '``Applicative>`` = Sequence.ForInfiniteSequences (t, IsLeftZero.Invoke, NonEmptySeq.ofList) - static member Sequence (t: NonEmptySeq> , []_output: option> , []_impl: Default3) : option> = Option.Sequence t |> Option.map NonEmptySeq.unsafeOfSeq - static member Sequence (t: NonEmptySeq>, []_output: Result, 'e>, []_impl: Default3) : Result, 'e> = Result.Sequence t |> Result.map NonEmptySeq.unsafeOfSeq - static member Sequence (t: NonEmptySeq>, []_output: Choice, 'e>, []_impl: Default3) : Choice, 'e> = Choice.Sequence t |> Choice.map NonEmptySeq.unsafeOfSeq - static member Sequence (t: NonEmptySeq> , []_output: list> , []_impl: Default3) : list> = Sequence.ForInfiniteSequences(t, List.isEmpty , NonEmptySeq.ofList) - static member Sequence (t: NonEmptySeq<'t []> , []_output: NonEmptySeq<'t> [] , []_impl: Default3) : NonEmptySeq<'t> [] = Sequence.ForInfiniteSequences(t, Array.isEmpty, NonEmptySeq.ofList) - #if !FABLE_COMPILER - static member Sequence (t: NonEmptySeq> , []_output: Async> , []_impl: Default3) = Async.Sequence t |> Async.map NonEmptySeq.unsafeOfSeq : Async> - #endif static member inline Sequence (t: ^a , []_output: 'R, []_impl: Default2) : 'R = Traverse.InvokeOnInstance id t static member inline Sequence (t: ^a , []_output: 'R, []_impl: Default1) : 'R = Sequence.InvokeOnInstance t diff --git a/src/FSharpPlus/Data/NonEmptySeq.fs b/src/FSharpPlus/Data/NonEmptySeq.fs index 3a29e8524..044ce27ea 100644 --- a/src/FSharpPlus/Data/NonEmptySeq.fs +++ b/src/FSharpPlus/Data/NonEmptySeq.fs @@ -5,11 +5,117 @@ open System.Runtime.InteropServices open System.ComponentModel open System.Collections.Generic open FSharpPlus +open FSharpPlus.Extensions +open FSharpPlus.Control +open Microsoft.FSharp.Core.CompilerServices /// A type-safe sequence that contains at least one element. -type NonEmptySeq<'T> = - inherit IEnumerable<'T> - abstract member First: 'T +[] +type NonEmptySeq<'t> = + inherit IEnumerable<'t> + abstract member First: 't + + // boilerplate + static member unsafeOfSeq (seq: _ seq) = + { new NonEmptySeq<_> with + member _.First = Seq.head seq + member _.GetEnumerator() = seq.GetEnumerator() + member _.GetEnumerator() = seq.GetEnumerator() :> Collections.IEnumerator } + + static member ofList (list: _ list) = match list with [] -> invalidArg "list" "The input list was empty." | _ -> NonEmptySeq<_>.unsafeOfSeq list + static member collect (mapping: 'a -> '``#NonEmptySeq<'b>``) (source: NonEmptySeq<'a>) : NonEmptySeq<'b> when '``#NonEmptySeq<'b>`` :> NonEmptySeq<'b> = Seq.collect mapping source |> NonEmptySeq<_>.unsafeOfSeq + static member concat (sources: NonEmptySeq<'``#NonEmptySeq<'a>``>) : NonEmptySeq<'a> when '``#NonEmptySeq<'a>`` :> NonEmptySeq<'a> = Seq.concat sources |> NonEmptySeq<'a>.unsafeOfSeq + static member allPairs (source1: _ NonEmptySeq) (source2: _ NonEmptySeq) = Seq.allPairs source1 source2 |> NonEmptySeq<_>.unsafeOfSeq + static member append (source1: 'a NonEmptySeq) (source2: 'a NonEmptySeq) = Seq.append source1 source2 |> NonEmptySeq<'a>.unsafeOfSeq + static member map mapping (source: NonEmptySeq<_>) = source |> Seq.map mapping |> NonEmptySeq<_>.unsafeOfSeq + static member bind (mapping: 'T->NonEmptySeq<'U>) source = NonEmptySeq<'U>.collect mapping source + static member apply f x = NonEmptySeq<_>.bind (fun f -> NonEmptySeq<_>.map ((<|) f) x) f + static member lift2 f x1 x2 = NonEmptySeq<_>.allPairs x1 x2 |> NonEmptySeq<_>.map (fun (x, y) -> f x y) + static member lift3 f x1 x2 x3 = + NonEmptySeq<_>.allPairs x2 x3 + |> NonEmptySeq<_>.allPairs x1 + |> NonEmptySeq<_>.map (fun x -> (fst (snd x), snd (snd x), fst x)) + |> NonEmptySeq<_>.map (fun (x, y, z) -> f x y z) + + static member zip (source1: NonEmptySeq<_>) (source2: NonEmptySeq<_>) = Seq.zip source1 source2 |> NonEmptySeq<_>.unsafeOfSeq + static member singleton value = Seq.singleton value |> NonEmptySeq<_>.unsafeOfSeq + static member delay (generator: unit -> NonEmptySeq<'a>) : NonEmptySeq<'a> = Seq.delay (fun () -> generator () :> _) |> NonEmptySeq<_>.unsafeOfSeq + + // Statics + + static member (<|>) (x: 'T NonEmptySeq , y) = NonEmptySeq<'T>.append x y + + static member inline Choice (x: ref``>>, _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 + + static member (<*>) (f: NonEmptySeq<_> , x: NonEmptySeq<'T>) : NonEmptySeq<'U> = NonEmptySeq<_>.apply f x + static member Lift2 (f, x: NonEmptySeq<_> , y: NonEmptySeq<_>) = NonEmptySeq<_>.lift2 f x y + static member Lift3 (f, x: NonEmptySeq<_> , y: NonEmptySeq<_> , z: NonEmptySeq<_>) = NonEmptySeq<_>.lift3 f x y z + static member IsLeftZero (_: NonEmptySeq<_>) = false + + static member Head (x: NonEmptySeq<'T>, []_impl: Head) = x.First + + static member TryHead (x: NonEmptySeq<'T>,[]_impl: TryHead) = Some x.First + static member TryLast (x: NonEmptySeq<'T>, []_impl: TryLast) = Some <| Seq.last x + + static member Map (x: NonEmptySeq<_>, f: 'T -> 'U) = NonEmptySeq<_>.map f x : NonEmptySeq<'U> + + static member Unzip (source: NonEmptySeq<'T * 'U>) = Map.Invoke fst source, Map.Invoke snd source + static member Zip (x: NonEmptySeq<'T>, y: NonEmptySeq<'U>) = NonEmptySeq<_>.zip x y + static member (>>=) (source: NonEmptySeq<'T>, f: 'T -> NonEmptySeq<'U>) = NonEmptySeq<_>.collect f source : NonEmptySeq<'U> + + static member Join (x: NonEmptySeq>) = NonEmptySeq<_>.concat x : NonEmptySeq<'T> + + static member Return (x: 'a) = NonEmptySeq<_>.singleton x : NonEmptySeq<'a> + + static member Delay (x: unit-> _ ) = NonEmptySeq<_>.delay x : NonEmptySeq<'T> + static member TryWith (computation: unit -> NonEmptySeq<_>, catchHandler: exn -> NonEmptySeq<_>) = seq (try (Seq.toArray (computation ())) with e -> Seq.toArray (catchHandler e)) |> NonEmptySeq<_>.unsafeOfSeq + static member TryFinally (computation: unit -> NonEmptySeq<_>, compensation: unit -> unit) = seq { try for e in computation () do yield e finally compensation () } |> NonEmptySeq<_>.unsafeOfSeq + static member Using (resource: 'T when 'T :> IDisposable, body: 'T -> NonEmptySeq<'U>) = 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 inline (+) (x: _ NonEmptySeq , y: _ NonEmptySeq) = NonEmptySeq<_>.append x y + + + static member inline Traverse (t: _ seq, f) = + let cons x y = seq {yield x; yield! y} + let cons_f x ys = Map.Invoke (cons: 'a->seq<_>->seq<_>) (f x) <*> ys + Map.Invoke NonEmptySeq<_>.unsafeOfSeq (Seq.foldBack cons_f t (result Seq.empty)) + + static member inline Traverse (t: NonEmptySeq<'T>, f: 'T->'``Functor<'U>``) = + let mapped = NonEmptySeq<_>.map f t + Sequence.ForInfiniteSequences (mapped, IsLeftZero.Invoke, NonEmptySeq<_>.ofList) : '``Functor>`` + + #if !FABLE_COMPILER + static member Traverse (t: 'T NonEmptySeq, f: 'T->Async<'u>) : Async> = async { + let! ct = Async.CancellationToken + return seq { + use enum = t.GetEnumerator () + while enum.MoveNext() do + yield Async.RunSynchronously (f enum.Current, cancellationToken = ct) } |> NonEmptySeq<_>.unsafeOfSeq } + #endif + + static member inline SequenceImpl (t, _, _:obj) = printfn "Using default4"; Sequence.ForInfiniteSequences (t, IsLeftZero.Invoke, NonEmptySeq<_>.ofList) + static member SequenceImpl (t: NonEmptySeq>, _: option>, _:Sequence) : option> = printfn "Not Using default4"; Option.Sequence t |> Option.map NonEmptySeq<_>.unsafeOfSeq + + static member SequenceImpl (t: NonEmptySeq>) : Result, 'E> = Result.Sequence t |> Result.map NonEmptySeq<_>.unsafeOfSeq + static member SequenceImpl (t: NonEmptySeq>) : Choice, 'E> = Choice.Sequence t |> Choice.map NonEmptySeq<_>.unsafeOfSeq + static member SequenceImpl (t: NonEmptySeq> , _: list> , _:Sequence) : list> = printfn "Not Using default4"; Sequence.ForInfiniteSequences (t, List.isEmpty , NonEmptySeq<_>.ofList) + static member SequenceImpl (t: NonEmptySeq<'T []> ) : NonEmptySeq<'T> [] = Sequence.ForInfiniteSequences (t, Array.isEmpty, NonEmptySeq<_>.ofList) + #if !FABLE_COMPILER + static member SequenceImpl (t: NonEmptySeq> ) : Async> = Async.Sequence t |> Async.map NonEmptySeq<_>.unsafeOfSeq + #endif + + static member inline Sequence (t: NonEmptySeq<'``Applicative<'T>``>) : '``Applicative>`` = + let inline call_3 (a: ^a, b: ^b, c: ^c) = ((^a or ^b or ^c) : (static member SequenceImpl : _*_*_ -> _) b, c, a) + let inline call (a: 'a, b: 'b) = call_3 (a, b, Unchecked.defaultof<'R>) : 'R + call (Unchecked.defaultof, t) + + + + /// A type alias for NonEmptySeq<'t> type neseq<'t> = NonEmptySeq<'t> @@ -536,6 +642,8 @@ module NonEmptySeq = let reduce (reduction: 'T -> 'T -> 'T) source = Seq.reduce reduction source + + [] module NonEmptySeqBuilder = type NESeqBuilder () = diff --git a/src/FSharpPlus/FSharpPlus.fsproj b/src/FSharpPlus/FSharpPlus.fsproj index c113e81ba..2aa3570c9 100644 --- a/src/FSharpPlus/FSharpPlus.fsproj +++ b/src/FSharpPlus/FSharpPlus.fsproj @@ -21,7 +21,7 @@ true Debug;Release;Fable;Fable3;Test AnyCPU - 6.0 + 8.0 $(DefineConstants);TEST_TRACE $(DefineConstants);FABLE_COMPILER $(DefineConstants);FABLE_COMPILER;FABLE_COMPILER_3 @@ -56,7 +56,6 @@ - @@ -83,6 +82,7 @@ + diff --git a/tests/FSharpPlus.Tests/General.fs b/tests/FSharpPlus.Tests/General.fs index f6f9b0a3e..3ebdb102c 100644 --- a/tests/FSharpPlus.Tests/General.fs +++ b/tests/FSharpPlus.Tests/General.fs @@ -1144,9 +1144,9 @@ module Applicative = Assert.AreEqual ([113; 213; 123; 223; 114; 214; 124; 224], testVal) Assert.IsInstanceOf>> (Some testVal) - let testVal2 = NonEmptySeq.create 1 [2] .+. NonEmptySeq.create 10 [20] .+. NonEmptySeq.create 100 [200] .+ 2 - Assert.AreEqual ([113; 213; 123; 223; 114; 214; 124; 224], Seq.toList testVal2) - Assert.IsInstanceOf>> (Some testVal2) + // let testVal2 = NonEmptySeq.create 1 [2] .+. NonEmptySeq.create 10 [20] .+. NonEmptySeq.create 100 [200] .+ 2 + // Assert.AreEqual ([113; 213; 123; 223; 114; 214; 124; 224], Seq.toList testVal2) + // Assert.IsInstanceOf>> (Some testVal2) let testLTE1 = Some 1 .<=. Some 2 Assert.AreEqual (Some true, testLTE1)