Skip to content

Commit d7bcad7

Browse files
PatrickMcDonaldlatkin
authored andcommitted
Implement Seq.iteri2
commit 459c7a370991f46ae69125cf35966b750998dae3 Merge: e36641b 131b462 Author: latkin <latkin@microsoft.com> Date: Sun Oct 12 16:19:14 2014 -0700 Merge branch 'SeqIteri2' of https://git01.codeplex.com/forks/patrickmcdonald/visualfsharp into PR commit 131b46238ba4552bf57e361d40881352dfe860f0 Author: Patrick McDonald <paddymcdonald@gmail.com> Date: Thu Aug 14 00:36:26 2014 +0100 Fix surface area portable tests commit b27432c38b13d8c68dc2e7bd734b67b9c54bf5f6 Author: Patrick McDonald <paddymcdonald@gmail.com> Date: Thu Aug 7 14:11:10 2014 +0100 Add more Seq.iteri2 test coverage commit a1be9925d3cb54975722d6de140c0f8c54827ab2 Author: Patrick McDonald <paddymcdonald@gmail.com> Date: Tue Aug 5 23:02:18 2014 +0100 Add iteri2 to Seq Module
1 parent e36641b commit d7bcad7

File tree

5 files changed

+68
-1
lines changed

5 files changed

+68
-1
lines changed

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,49 @@ type SeqModule2() =
267267
let nullseq:seq<'a> = null
268268
CheckThrowsArgumentNullException (fun () -> Seq.iteri funcint nullseq |> ignore)
269269
()
270+
271+
[<Test>]
272+
member this.Iteri2() =
273+
274+
//seq int
275+
let seqint = seq [ 1..3]
276+
let cacheint = ref 0
277+
278+
let funcint x y z = cacheint := !cacheint + x + y + z
279+
Seq.iteri2 funcint seqint seqint
280+
Assert.AreEqual(15,!cacheint)
281+
282+
//seq str
283+
let seqStr = seq ["first";"second"]
284+
let cachestr = ref 0
285+
let funcstr (x:int) (y:string) (z:string) = cachestr := !cachestr + x + y.Length + z.Length
286+
Seq.iteri2 funcstr seqStr seqStr
287+
288+
Assert.AreEqual(23,!cachestr)
289+
290+
// empty seq
291+
let emptyseq = Seq.empty
292+
let resultEpt = ref 0
293+
Seq.iteri2 (fun x y z -> Assert.Fail()) emptyseq emptyseq
294+
295+
// null seq
296+
let nullseq:seq<'a> = null
297+
CheckThrowsArgumentNullException (fun () -> Seq.iteri2 funcint nullseq nullseq |> ignore)
298+
299+
// len1 <> len2
300+
let shorterSeq = seq { 1..3 }
301+
let longerSeq = seq { 2..2..100 }
302+
303+
let testSeqLengths seq1 seq2 =
304+
let cache = ref 0
305+
let f x y z = cache := !cache + x + y + z
306+
Seq.iteri2 f seq1 seq2
307+
!cache
308+
309+
Assert.AreEqual(21, testSeqLengths shorterSeq longerSeq)
310+
Assert.AreEqual(21, testSeqLengths longerSeq shorterSeq)
311+
312+
()
270313

271314
[<Test>]
272315
member this.Length() =
@@ -1309,4 +1352,4 @@ type SeqModule2() =
13091352

13101353
CheckThrowsArgumentNullException(fun () -> Seq.tryPick funcNull nullSeq |> ignore)
13111354

1312-
()
1355+
()

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ Microsoft.FSharp.Collections.SeqModule: TState Fold2[T1,T2,TState](Microsoft.FSh
457457
Microsoft.FSharp.Collections.SeqModule: TState Fold[T,TState](Microsoft.FSharp.Core.FSharpFunc`2[TState,Microsoft.FSharp.Core.FSharpFunc`2[T,TState]], TState, System.Collections.Generic.IEnumerable`1[T])
458458
Microsoft.FSharp.Collections.SeqModule: T[] ToArray[T](System.Collections.Generic.IEnumerable`1[T])
459459
Microsoft.FSharp.Collections.SeqModule: Void Iterate2[T1,T2](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,Microsoft.FSharp.Core.Unit]], System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2])
460+
Microsoft.FSharp.Collections.SeqModule: Void IterateIndexed2[T1,T2](Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,Microsoft.FSharp.Core.Unit]]], System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2])
460461
Microsoft.FSharp.Collections.SeqModule: Void IterateIndexed[T](Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.Unit]], System.Collections.Generic.IEnumerable`1[T])
461462
Microsoft.FSharp.Collections.SeqModule: Void Iterate[T](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.Unit], System.Collections.Generic.IEnumerable`1[T])
462463
Microsoft.FSharp.Collections.SetModule: Boolean Contains[T](T, Microsoft.FSharp.Collections.FSharpSet`1[T])

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ Microsoft.FSharp.Collections.SeqModule: TState Fold2[T1,T2,TState](Microsoft.FSh
451451
Microsoft.FSharp.Collections.SeqModule: TState Fold[T,TState](Microsoft.FSharp.Core.FSharpFunc`2[TState,Microsoft.FSharp.Core.FSharpFunc`2[T,TState]], TState, System.Collections.Generic.IEnumerable`1[T])
452452
Microsoft.FSharp.Collections.SeqModule: T[] ToArray[T](System.Collections.Generic.IEnumerable`1[T])
453453
Microsoft.FSharp.Collections.SeqModule: Void Iterate2[T1,T2](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,Microsoft.FSharp.Core.Unit]], System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2])
454+
Microsoft.FSharp.Collections.SeqModule: Void IterateIndexed2[T1,T2](Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,Microsoft.FSharp.Core.Unit]]], System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2])
454455
Microsoft.FSharp.Collections.SeqModule: Void IterateIndexed[T](Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.Unit]], System.Collections.Generic.IEnumerable`1[T])
455456
Microsoft.FSharp.Collections.SeqModule: Void Iterate[T](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.Unit], System.Collections.Generic.IEnumerable`1[T])
456457
Microsoft.FSharp.Collections.SetModule: Boolean Contains[T](T, Microsoft.FSharp.Collections.FSharpSet`1[T])

src/fsharp/FSharp.Core/seq.fs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,16 @@ namespace Microsoft.FSharp.Collections
931931
while (e1.MoveNext() && e2.MoveNext()) do
932932
f e1.Current e2.Current;
933933

934+
[<CompiledName("IterateIndexed2")>]
935+
let iteri2 f (source1 : seq<_>) (source2 : seq<_>) =
936+
checkNonNull "source1" source1
937+
checkNonNull "source2" source2
938+
use e1 = source1.GetEnumerator()
939+
use e2 = source2.GetEnumerator()
940+
let mutable i = 0
941+
while (e1.MoveNext() && e2.MoveNext()) do
942+
f i e1.Current e2.Current
943+
i <- i + 1
934944

935945
// Build an IEnumerble by wrapping/transforming iterators as they get generated.
936946
let revamp f (ie : seq<_>) = mkSeq (fun () -> f (ie.GetEnumerator()))

src/fsharp/FSharp.Core/seq.fsi

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,18 @@ namespace Microsoft.FSharp.Collections
522522
[<CompiledName("Iterate2")>]
523523
val iter2: action:('T1 -> 'T2 -> unit) -> source1:seq<'T1> -> source2:seq<'T2> -> unit
524524

525+
/// <summary>Applies the given function to two collections simultaneously. If one sequence is shorter than
526+
/// the other then the remaining elements of the longer sequence are ignored. The integer passed to the
527+
/// function indicates the index of element.</summary>
528+
///
529+
/// <param name="action">A function to apply to each pair of elements from the input sequences along with their index.</param>
530+
/// <param name="source1">The first input sequence.</param>
531+
/// <param name="source2">The second input sequence.</param>
532+
///
533+
/// <exception cref="System.ArgumentNullException">Thrown when either of the input sequences is null.</exception>
534+
[<CompiledName("IterateIndexed2")>]
535+
val iteri2: action:(int -> 'T1 -> 'T2 -> unit) -> source1:seq<'T1> -> source2:seq<'T2> -> unit
536+
525537
/// <summary>Returns the length of the sequence</summary>
526538
///
527539
/// <param name="source">The input sequence.</param>

0 commit comments

Comments
 (0)