Skip to content

Commit 5bc1981

Browse files
committed
Seq.mapi2
1 parent 73e4e29 commit 5bc1981

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

src/fsharp/FSharp.Core/seq.fs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -107,25 +107,6 @@ namespace Microsoft.FSharp.Collections
107107
interface System.IDisposable with
108108
member this.Dispose() = this.Dispose()
109109

110-
let mapi2 f (e1 : IEnumerator<_>) (e2 : IEnumerator<_>) : IEnumerator<_> =
111-
let f = OptimizedClosures.FSharpFunc<_,_,_,_>.Adapt(f)
112-
let i = ref (-1)
113-
upcast
114-
{ new MapEnumerator<_>() with
115-
member this.DoMoveNext curr =
116-
i := !i + 1
117-
if (e1.MoveNext() && e2.MoveNext()) then
118-
curr <- f.Invoke(!i, e1.Current, e2.Current)
119-
true
120-
else
121-
false
122-
member this.Dispose() =
123-
try
124-
e1.Dispose()
125-
finally
126-
e2.Dispose()
127-
}
128-
129110
let unfold f x : IEnumerator<_> =
130111
let state = ref x
131112
upcast
@@ -634,6 +615,10 @@ namespace Microsoft.FSharp.Collections
634615
inherit SeqComponentFactory<'T,'U> ()
635616
override __.Create<'V> (_result:Result<'V>) (next:SeqComponent<'U,'V>) : SeqComponent<'T,'V> = upcast Mapi (mapi, next)
636617

618+
and Mapi2Factory<'First,'Second,'U> (map:int->'First->'Second->'U, input2:IEnumerable<'Second>) =
619+
inherit SeqComponentFactory<'First,'U> ()
620+
override __.Create<'V> (result:Result<'V>) (next:SeqComponent<'U,'V>) : SeqComponent<'First,'V> = upcast Mapi2 (map, input2, result, next)
621+
637622
and PairwiseFactory<'T> () =
638623
inherit SeqComponentFactory<'T,'T*'T> ()
639624
override __.Create<'V> (_result:Result<'V>) (next:SeqComponent<'T*'T,'V>) : SeqComponent<'T,'V> = upcast Pairwise next
@@ -830,6 +815,28 @@ namespace Microsoft.FSharp.Collections
830815
idx <- idx + 1
831816
Helpers.avoidTailCall (next.ProcessNext (mapi'.Invoke (idx-1, input)))
832817

818+
and Mapi2<'First,'Second,'U,'V> (map:int->'First->'Second->'U, enumerable2:IEnumerable<'Second>, result:Result<'V>, next:SeqComponent<'U,'V>) =
819+
inherit SeqComponent<'First,'V>(next)
820+
821+
let mutable idx = 0
822+
let input2 = enumerable2.GetEnumerator ()
823+
let mapi2' = OptimizedClosures.FSharpFunc<_,_,_,_>.Adapt map
824+
825+
override __.ProcessNext (input:'First) : bool =
826+
if input2.MoveNext () then
827+
idx <- idx + 1
828+
Helpers.avoidTailCall (next.ProcessNext (mapi2'.Invoke (idx-1, input, input2.Current)))
829+
else
830+
result.StopFurtherProcessing ()
831+
false
832+
833+
interface ISeqComponent with
834+
override __.OnDispose () =
835+
try
836+
input2.Dispose ()
837+
finally
838+
(Helpers.upcastISeqComponent next).OnDispose ()
839+
833840
and Pairwise<'T,'V> (next:SeqComponent<'T*'T,'V>) =
834841
inherit SeqComponent<'T,'V>(next)
835842

@@ -1500,7 +1507,7 @@ namespace Microsoft.FSharp.Collections
15001507
let mapi2 f source1 source2 =
15011508
checkNonNull "source1" source1
15021509
checkNonNull "source2" source2
1503-
revamp2 (IEnumerator.mapi2 f) source1 source2
1510+
source1 |> seqFactory (SeqComposer.Mapi2Factory (f, source2))
15041511

15051512
[<CompiledName("Map2")>]
15061513
let map2<'T,'U,'V> (f:'T->'U->'V) (source1:seq<'T>) (source2:seq<'U>) : seq<'V> =

0 commit comments

Comments
 (0)