Skip to content

Commit 3a27cd8

Browse files
PatrickMcDonaldlatkin
authored andcommitted
Implement Seq.mapi2
commit 7a45265d8163b018c21963febe1b3ef56d6cf0a1 Merge: d7bcad7 a7f5140 Author: latkin <latkin@microsoft.com> Date: Sun Oct 12 16:32:43 2014 -0700 Merge branch 'mapi2' of https://git01.codeplex.com/forks/patrickmcdonald/visualfsharp into PR Conflicts: src/fsharp/FSharp.Core.Unittests/SurfaceArea.4.0.fs src/fsharp/FSharp.Core.Unittests/SurfaceArea.Portable.fs src/fsharp/FSharp.Core/seq.fs commit a7f51406a3981a474289230977cf2caac8975752 Author: Patrick McDonald <paddymcdonald@gmail.com> Date: Thu Aug 14 00:39:02 2014 +0100 Fix surface area portable tests commit 4bdfa7625d42884824a53e8053bc5bcdffce91b1 Author: Patrick McDonald <paddymcdonald@gmail.com> Date: Thu Aug 7 15:29:17 2014 +0100 Add more Seq.mapi2 test coverage commit 64b3ffece57e907e513f624380e3227b5e58504c Author: Patrick McDonald <paddymcdonald@gmail.com> Date: Wed Aug 6 15:03:03 2014 +0100 Add mapi2 to Seq
1 parent d7bcad7 commit 3a27cd8

5 files changed

Lines changed: 113 additions & 0 deletions

File tree

src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/SeqModule2.fs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,40 @@ type SeqModule2() =
565565
if e.MoveNext() then Assert.Fail()
566566
Assert.AreEqual(0,!i)
567567

568+
[<Test>]
569+
member this.Mapi2WithSideEffects () =
570+
let i = ref 0
571+
let f _ x y = i := !i + 1; x*x
572+
let e = (Seq.mapi2 f [1;2] [1;2]).GetEnumerator()
573+
574+
CheckThrowsInvalidOperationExn (fun _ -> e.Current|>ignore)
575+
Assert.AreEqual(0, !i)
576+
if not (e.MoveNext()) then Assert.Fail()
577+
Assert.AreEqual(1, !i)
578+
let _ = e.Current
579+
Assert.AreEqual(1, !i)
580+
let _ = e.Current
581+
Assert.AreEqual(1, !i)
582+
583+
if not (e.MoveNext()) then Assert.Fail()
584+
Assert.AreEqual(2, !i)
585+
let _ = e.Current
586+
Assert.AreEqual(2, !i)
587+
let _ = e.Current
588+
Assert.AreEqual(2, !i)
589+
590+
if e.MoveNext() then Assert.Fail()
591+
Assert.AreEqual(2,!i)
592+
CheckThrowsInvalidOperationExn (fun _ -> e.Current|>ignore)
593+
Assert.AreEqual(2, !i)
594+
595+
i := 0
596+
let e = (Seq.mapi2 f [] []).GetEnumerator()
597+
if e.MoveNext() then Assert.Fail()
598+
Assert.AreEqual(0,!i)
599+
if e.MoveNext() then Assert.Fail()
600+
Assert.AreEqual(0,!i)
601+
568602
[<Test>]
569603
member this.Collect() =
570604
// integer Seq
@@ -627,6 +661,44 @@ type SeqModule2() =
627661

628662
()
629663

664+
[<Test>]
665+
member this.Mapi2() =
666+
// integer Seq
667+
let funcInt x y z = x+y+z
668+
let resultInt = Seq.mapi2 funcInt { 1..10 } {2..2..20}
669+
let expectedint = seq [3;7;11;15;19;23;27;31;35;39]
670+
671+
VerifySeqsEqual expectedint resultInt
672+
673+
// string Seq
674+
let funcStr (x:int) (y:int) (z:string) = x+y+z.Length
675+
let resultStr = Seq.mapi2 funcStr (seq[3;6;9;11]) (seq ["Lists"; "Are"; "Commonly" ; "List" ])
676+
let expectedSeq = seq [8;10;19;18]
677+
678+
VerifySeqsEqual expectedSeq resultStr
679+
680+
// empty Seq
681+
let resultEpt = Seq.mapi2 funcInt Seq.empty Seq.empty
682+
VerifySeqsEqual Seq.empty resultEpt
683+
684+
// null Seq
685+
let nullSeq:seq<'a> = null
686+
let validSeq = seq [1]
687+
CheckThrowsArgumentNullException (fun () -> Seq.mapi2 funcInt nullSeq validSeq |> ignore)
688+
689+
// len1 <> len2
690+
let shorterSeq = seq { 1..10 }
691+
let longerSeq = seq { 2..20 }
692+
693+
let testSeqLengths seq1 seq2 =
694+
let f x y z = x + y + z
695+
Seq.mapi2 f seq1 seq2
696+
697+
VerifySeqsEqual (seq [3;6;9;12;15;18;21;24;27;30]) (testSeqLengths shorterSeq longerSeq)
698+
VerifySeqsEqual (seq [3;6;9;12;15;18;21;24;27;30]) (testSeqLengths longerSeq shorterSeq)
699+
700+
()
701+
630702
[<Test>]
631703
member this.Max() =
632704
// integer Seq

src/fsharp/FSharp.Core.Unittests/SurfaceArea.4.0.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1
407407
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TResult] Collect[T,TCollection,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TCollection], System.Collections.Generic.IEnumerable`1[T])
408408
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TResult] Map2[T1,T2,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,TResult]], System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2])
409409
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TResult] Map3[T1,T2,T3,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,Microsoft.FSharp.Core.FSharpFunc`2[T3,TResult]]], System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2], System.Collections.Generic.IEnumerable`1[T3])
410+
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TResult] MapIndexed2[T1,T2,TResult](Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,TResult]]], System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2])
410411
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TResult] MapIndexed[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,Microsoft.FSharp.Core.FSharpFunc`2[T,TResult]], System.Collections.Generic.IEnumerable`1[T])
411412
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TResult] Map[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], System.Collections.Generic.IEnumerable`1[T])
412413
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TState] Scan[T,TState](Microsoft.FSharp.Core.FSharpFunc`2[TState,Microsoft.FSharp.Core.FSharpFunc`2[T,TState]], TState, System.Collections.Generic.IEnumerable`1[T])

src/fsharp/FSharp.Core.Unittests/SurfaceArea.Portable.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1
401401
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TResult] Collect[T,TCollection,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TCollection], System.Collections.Generic.IEnumerable`1[T])
402402
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TResult] Map2[T1,T2,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,TResult]], System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2])
403403
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TResult] Map3[T1,T2,T3,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,Microsoft.FSharp.Core.FSharpFunc`2[T3,TResult]]], System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2], System.Collections.Generic.IEnumerable`1[T3])
404+
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TResult] MapIndexed2[T1,T2,TResult](Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,TResult]]], System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2])
404405
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TResult] MapIndexed[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,Microsoft.FSharp.Core.FSharpFunc`2[T,TResult]], System.Collections.Generic.IEnumerable`1[T])
405406
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TResult] Map[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], System.Collections.Generic.IEnumerable`1[T])
406407
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TState] Scan[T,TState](Microsoft.FSharp.Core.FSharpFunc`2[TState,Microsoft.FSharp.Core.FSharpFunc`2[T,TState]], TState, System.Collections.Generic.IEnumerable`1[T])

src/fsharp/FSharp.Core/seq.fs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,24 @@ namespace Microsoft.FSharp.Collections
146146
e2.Dispose()
147147
}
148148

149+
let mapi2 f (e1 : IEnumerator<_>) (e2 : IEnumerator<_>) : IEnumerator<_> =
150+
let i = ref (-1)
151+
upcast
152+
{ new MapEnumerator<_>() with
153+
member this.DoMoveNext curr =
154+
i := !i + 1
155+
if (e1.MoveNext() && e2.MoveNext()) then
156+
curr <- f !i e1.Current e2.Current
157+
true
158+
else
159+
false
160+
member this.Dispose() =
161+
try
162+
e1.Dispose()
163+
finally
164+
e2.Dispose()
165+
}
166+
149167
let map3 f (e1 : IEnumerator<_>) (e2 : IEnumerator<_>) (e3 : IEnumerator<_>) : IEnumerator<_> =
150168
upcast
151169
{ new MapEnumerator<_>() with
@@ -967,6 +985,12 @@ namespace Microsoft.FSharp.Collections
967985
checkNonNull "source" source
968986
revamp (IEnumerator.mapi f) source
969987

988+
[<CompiledName("MapIndexed2")>]
989+
let mapi2 f source1 source2 =
990+
checkNonNull "source1" source1
991+
checkNonNull "source2" source2
992+
revamp2 (IEnumerator.mapi2 f) source1 source2
993+
970994
[<CompiledName("Map2")>]
971995
let map2 f source1 source2 =
972996
checkNonNull "source1" source1

src/fsharp/FSharp.Core/seq.fsi

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,21 @@ namespace Microsoft.FSharp.Collections
603603
[<CompiledName("MapIndexed")>]
604604
val mapi: mapping:(int -> 'T -> 'U) -> source:seq<'T> -> seq<'U>
605605

606+
/// <summary>Builds a new collection whose elements are the results of applying the given function
607+
/// to the corresponding pairs of elements from the two sequences. If one input sequence is shorter than
608+
/// the other then the remaining elements of the longer sequence are ignored. The integer index passed to the
609+
/// function indicates the index (from 0) of element being transformed.</summary>
610+
///
611+
/// <param name="mapping">A function to transform pairs of items from the input sequences that also supplies the current index.</param>
612+
/// <param name="source1">The first input sequence.</param>
613+
/// <param name="source2">The second input sequence.</param>
614+
///
615+
/// <returns>The result sequence.</returns>
616+
///
617+
/// <exception cref="System.ArgumentNullException">Thrown when either of the input sequences is null.</exception>
618+
[<CompiledName("MapIndexed2")>]
619+
val mapi2: mapping:(int -> 'T1 -> 'T2 -> 'U) -> source1:seq<'T1> -> source2:seq<'T2> -> seq<'U>
620+
606621
/// <summary>Returns the greatest of all elements of the sequence, compared via Operators.max</summary>
607622
///
608623
/// <param name="source">The input sequence.</param>

0 commit comments

Comments
 (0)