Skip to content

Commit 20fe7e1

Browse files
committed
Move transpose and gather
1 parent e4aa158 commit 20fe7e1

File tree

2 files changed

+51
-53
lines changed

2 files changed

+51
-53
lines changed

src/FSharpPlus/Control/Traversable.fs

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -256,29 +256,7 @@ type Gather =
256256
let cons_f x ys = Map.Invoke (cons: 'a -> seq<_> -> seq<_>) (f x) <.> ys
257257
Seq.foldBack cons_f t (Pure.Invoke Seq.empty)
258258

259-
(*
260-
static member inline Gather (t: _ NonEmptySeq, f, [<Optional>]_output: 'R, [<Optional>]_impl: Default3) =
261-
#if TEST_TRACE
262-
Traces.add "Gather NonEmptySeq"
263-
#endif
264-
let cons x y = seq {yield x; yield! y}
265-
let cons_f x ys = Map.Invoke (cons: 'a -> seq<_> -> seq<_>) (f x) <.> ys
266-
Map.Invoke NonEmptySeq.ofSeq (Seq.foldBack cons_f t (Pure.Invoke Seq.empty))
267-
268-
static member inline Gather (t: seq<'T>, f: 'T -> '``Functor<'U>``, [<Optional>]_output: '``Functor<seq<'U>>``, [<Optional>]_impl: Default2) =
269-
#if TEST_TRACE
270-
Traces.add "Gather seq, 'T -> Functor<'U>"
271-
#endif
272-
let mapped = Seq.map f t
273-
Transpose.ForInfiniteSequences (mapped, IsZipLeftZero.Invoke, List.toSeq, Pure.Invoke) : '``Functor<seq<'U>>``
274-
275-
static member inline Gather (t: NonEmptySeq<'T>, f: 'T -> '``Functor<'U>``, [<Optional>]_output: '``Functor<NonEmptySeq<'U>>``, [<Optional>]_impl: Default2) =
276-
#if TEST_TRACE
277-
Traces.add "Gather NonEmptySeq, 'T -> Functor<'U>"
278-
#endif
279-
let mapped = NonEmptySeq.map f t
280-
Transpose.ForInfiniteSequences (mapped, IsZipLeftZero.Invoke, NonEmptySeq.ofList, Pure.Invoke) : '``Functor<NonEmptySeq<'U>>``
281-
*)
259+
282260

283261
static member inline Gather (t: ^a, f, [<Optional>]_output: 'R, [<Optional>]_impl: Default1) : 'R =
284262
#if TEST_TRACE
@@ -300,20 +278,6 @@ type Gather =
300278
yield Async.AsTask(f enum.Current, cancellationToken = ct).Result }}
301279
#endif
302280

303-
(*
304-
#if !FABLE_COMPILER
305-
static member Gather (t: 't NonEmptySeq, f: 't -> Async<'u>, [<Optional>]_output: Async<NonEmptySeq<'u>>, [<Optional>]_impl: Gather) : Async<NonEmptySeq<_>> = async {
306-
#if TEST_TRACE
307-
Traces.add "Gather 't NonEmptySeq, 't -> Async<'u>"
308-
#endif
309-
310-
let! ct = Async.CancellationToken
311-
return seq {
312-
use enum = t.GetEnumerator ()
313-
while enum.MoveNext() do
314-
yield Async.AsTask(f enum.Current, cancellationToken = ct).Result } |> NonEmptySeq.unsafeOfSeq }
315-
#endif
316-
*)
317281

318282
static member Gather (t: Id<'t>, f: 't -> option<'u>, [<Optional>]_output: option<Id<'u>>, [<Optional>]_impl: Gather) =
319283
#if TEST_TRACE
@@ -408,18 +372,6 @@ type Transpose with
408372
static member Transpose (t: seq<Async<'t>> , [<Optional>]_output: Async<seq<'t>> , [<Optional>]_impl: Default3) : Async<seq<'t>> = Async.Parallel t |> Async.map Array.toSeq
409373
#endif
410374

411-
(*
412-
static member inline Transpose (t: NonEmptySeq<'``Applicative<'T>``>, [<Optional>]_output: '``Applicative<NonEmptySeq<'T>>``, [<Optional>]_impl: Default4) : '``Applicative<NonEmptySeq<'T>>`` = Transpose.ForInfiniteSequences (t, IsZipLeftZero.Invoke, NonEmptySeq.ofList, fun _ -> Unchecked.defaultof<_>)
413-
static member Transpose (t: NonEmptySeq<option<'t>> , [<Optional>]_output: option<NonEmptySeq<'t>> , [<Optional>]_impl: Default3) : option<NonEmptySeq<'t>> = Option.Sequential t |> Option.map NonEmptySeq.unsafeOfSeq
414-
static member inline Transpose (t: NonEmptySeq<Result<'t,'e>>, [<Optional>]_output: Result<NonEmptySeq<'t>, 'e>, [<Optional>]_impl: Default3) : Result<NonEmptySeq<'t>, 'e> = Result.Parallel ((++), t) |> Result.map NonEmptySeq.unsafeOfSeq
415-
static member inline Transpose (t: NonEmptySeq<Choice<'t,'e>>, [<Optional>]_output: Choice<NonEmptySeq<'t>, 'e>, [<Optional>]_impl: Default3) : Choice<NonEmptySeq<'t>, 'e> = Choice.Parallel ((++), t) |> Choice.map NonEmptySeq.unsafeOfSeq
416-
static member Transpose (t: NonEmptySeq<list<'t>> , [<Optional>]_output: list<NonEmptySeq<'t>> , [<Optional>]_impl: Default3) : list<NonEmptySeq<'t>> = Transpose.ForInfiniteSequences (t, List.isEmpty , NonEmptySeq.ofList, fun _ -> Unchecked.defaultof<_>)
417-
static member Transpose (t: NonEmptySeq<'t []> , [<Optional>]_output: NonEmptySeq<'t> [] , [<Optional>]_impl: Default3) : NonEmptySeq<'t> [] = Transpose.ForInfiniteSequences (t, Array.isEmpty, NonEmptySeq.ofList, fun _ -> Unchecked.defaultof<_>)
418-
#if !FABLE_COMPILER
419-
static member Transpose (t: NonEmptySeq<Async<'t>> , [<Optional>]_output: Async<NonEmptySeq<'t>> , [<Optional>]_impl: Default3) = Async.Parallel t |> Async.map NonEmptySeq.unsafeOfSeq : Async<NonEmptySeq<'t>>
420-
#endif
421-
*)
422-
423375
static member inline Transpose (t: ^a , [<Optional>]_output: 'R, [<Optional>]_impl: Default2) : 'R = Gather.InvokeOnInstance id t
424376
static member inline Transpose (t: ^a , [<Optional>]_output: 'R, [<Optional>]_impl: Default1) : 'R = Transpose.InvokeOnInstance t
425377

src/FSharpPlus/Data/NonEmptySeq.fs

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,14 @@ type NonEmptySeq<'t> =
9191
#if !FABLE_COMPILER
9292

9393
static member inline Traverse (t: _ seq, f) =
94-
let cons x y = seq {yield x; yield! y}
95-
let cons_f x ys = Map.Invoke (cons: 'a->seq<_>->seq<_>) (f x) <*> ys
94+
let cons_f x ys = Map.Invoke (Seq.cons: 'a -> seq<_> -> seq<_>) (f x) <*> ys
9695
Map.Invoke NonEmptySeq<_>.unsafeOfSeq (Seq.foldBack cons_f t (result Seq.empty))
9796

98-
static member inline Traverse (t: NonEmptySeq<'T>, f: 'T->'``Functor<'U>``) =
97+
static member inline Traverse (t: NonEmptySeq<'T>, f: 'T -> '``Functor<'U>``) =
9998
let mapped = NonEmptySeq<_>.map f t
10099
Sequence.ForInfiniteSequences (mapped, IsLeftZero.Invoke, NonEmptySeq<_>.ofList, Return.Invoke) : '``Functor<NonEmptySeq<'U>>``
101100

102-
static member Traverse (t: 'T NonEmptySeq, f: 'T->Async<'u>) : Async<NonEmptySeq<_>> = async {
101+
static member Traverse (t: 'T NonEmptySeq, f: 'T -> Async<'u>) : Async<NonEmptySeq<_>> = async {
103102
let! ct = Async.CancellationToken
104103
return seq {
105104
use enum = t.GetEnumerator ()
@@ -112,6 +111,53 @@ type NonEmptySeq<'t> =
112111

113112
#endif
114113

114+
115+
static member inline Gather (t: seq<'T>, f: 'T -> '``Functor<'U>``) =
116+
#if TEST_TRACE
117+
Traces.add "Gather seq, 'T -> Functor<'U>"
118+
#endif
119+
let mapped = Seq.map f t
120+
Transpose.ForInfiniteSequences (mapped, IsZipLeftZero.Invoke, List.toSeq, Pure.Invoke) : '``Functor<seq<'U>>``
121+
122+
static member inline Gather (t: _ NonEmptySeq, f: 'T -> '``ZipFunctor<'U>``) =
123+
#if TEST_TRACE
124+
Traces.add "Gather NonEmptySeq"
125+
#endif
126+
let cons_f x ys = Map.Invoke (Seq.cons: 'a -> seq<_> -> seq<_>) (f x) <.> ys
127+
Map.Invoke NonEmptySeq<_>.unsafeOfSeq (Seq.foldBack cons_f t (Pure.Invoke Seq.empty))
128+
129+
130+
static member inline Gather (t: NonEmptySeq<'T>, f: 'T -> '``ZipFunctor<'U>``) =
131+
#if TEST_TRACE
132+
Traces.add "Gather NonEmptySeq, 'T -> Functor<'U>"
133+
#endif
134+
let mapped = NonEmptySeq<_>.map f t
135+
Transpose.ForInfiniteSequences (mapped, IsZipLeftZero.Invoke, NonEmptySeq<_>.ofList, Pure.Invoke) : '``Functor<NonEmptySeq<'U>>``
136+
137+
#if !FABLE_COMPILER
138+
static member Gather (t: 't NonEmptySeq, f: 't -> Async<'u>) : Async<NonEmptySeq<_>> = async {
139+
#if TEST_TRACE
140+
Traces.add "Gather 't NonEmptySeq, 't -> Async<'u>"
141+
#endif
142+
143+
let! ct = Async.CancellationToken
144+
return seq {
145+
use enum = t.GetEnumerator ()
146+
while enum.MoveNext() do
147+
yield Async.AsTask(f enum.Current, cancellationToken = ct).Result } |> NonEmptySeq<_>.unsafeOfSeq }
148+
#endif
149+
150+
static member inline Transpose (t: NonEmptySeq<'``ZipApplicative<'T>``>) : '``ZipApplicative<NonEmptySeq<'T>>`` = Transpose.ForInfiniteSequences (t, IsZipLeftZero.Invoke, NonEmptySeq<_>.ofList, fun _ -> Unchecked.defaultof<_>)
151+
static member Transpose (t: NonEmptySeq<option<'t>> ) : option<NonEmptySeq<'t>> = Option.Sequential t |> Option.map NonEmptySeq<_>.unsafeOfSeq
152+
static member inline Transpose (t: NonEmptySeq<Result<'t,'e>> ) : Result<NonEmptySeq<'t>, 'e> = Result.Parallel ((++), t) |> Result.map NonEmptySeq<_>.unsafeOfSeq
153+
static member inline Transpose (t: NonEmptySeq<Choice<'t,'e>> ) : Choice<NonEmptySeq<'t>, 'e> = Choice.Parallel ((++), t) |> Choice.map NonEmptySeq<_>.unsafeOfSeq
154+
static member Transpose (t: NonEmptySeq<list<'t>> ) : list<NonEmptySeq<'t>> = Transpose.ForInfiniteSequences (t, List.isEmpty , NonEmptySeq<_>.ofList, fun _ -> Unchecked.defaultof<_>)
155+
static member Transpose (t: NonEmptySeq<'t []> ) : NonEmptySeq<'t> [] = Transpose.ForInfiniteSequences (t, Array.isEmpty, NonEmptySeq<_>.ofList, fun _ -> Unchecked.defaultof<_>)
156+
#if !FABLE_COMPILER
157+
static member Transpose (t: NonEmptySeq<Async<'t>>) : Async<NonEmptySeq<'t>> = Async.Parallel t |> Async.map NonEmptySeq<_>.unsafeOfSeq
158+
#endif
159+
160+
115161

116162

117163

0 commit comments

Comments
 (0)