Skip to content

Commit 15f04de

Browse files
forkilatkin
authored andcommitted
Implement Seq.fold2
commit 3c379b6deba89aeccd0af4d3744f69076f6cc34d Merge: f197816 6e6f68c Author: latkin <latkin@microsoft.com> Date: Sun Oct 12 10:23:58 2014 -0700 Merge branch 'fold2' of https://git01.codeplex.com/forks/forki/fsharp into PR commit 6e6f68ce8b1b0dbf7deb92cf1c6887792118821d Author: Steffen Forkmann <steffen.forkmann@msu-solutions.de> Date: Fri Jul 4 14:49:20 2014 +0200 Adding surface are for fold2 commit ac88069f9d2ed8196e5cdbd5294d891d52942b47 Author: Steffen Forkmann <steffen.forkmann@msu-solutions.de> Date: Fri Jul 4 14:45:05 2014 +0200 Changing Seq.fold2 - sequences need not have equal lengths commit 74adf606599ba4b648142c532249f782501f0758 Author: Steffen Forkmann <steffen.forkmann@msu-solutions.de> Date: Thu Jul 3 23:06:41 2014 +0200 Check exceptions for Seq.fold2 commit 2be78be51151b83cc9f294254b9de7b20fd71b79 Author: Steffen Forkmann <steffen.forkmann@msu-solutions.de> Date: Thu Jul 3 23:02:49 2014 +0200 Implementing Seq.fold2
1 parent f197816 commit 15f04de

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,40 @@ type SeqModule() =
667667

668668
CheckThrowsArgumentNullException (fun () -> Seq.fold funcInt 1 nullSeq |> ignore)
669669
()
670+
671+
672+
673+
[<Test>]
674+
member this.Fold2() =
675+
Assert.AreEqual([(3,5); (2,3); (1,1)],Seq.fold2 (fun acc x y -> (x,y)::acc) [] (seq [ 1..3 ]) (seq [1..2..6]))
676+
677+
// integer List
678+
let funcInt x y z = x + y + z
679+
let resultInt = Seq.fold2 funcInt 9 (seq [ 1..10 ]) (seq [1..2..20])
680+
Assert.AreEqual(164, resultInt)
681+
682+
// string List
683+
let funcStr x y z = x + y + z
684+
let resultStr = Seq.fold2 funcStr "*" ["a"; "b"; "c" ; "d" ] ["A"; "B"; "C" ; "D" ]
685+
Assert.AreEqual("*aAbBcCdD", resultStr)
686+
687+
// empty List
688+
let emptyArr:int list = [ ]
689+
let resultEpt = Seq.fold2 funcInt 5 emptyArr emptyArr
690+
Assert.AreEqual(5, resultEpt)
691+
692+
Assert.AreEqual(0,Seq.fold2 funcInt 0 Seq.empty (seq [1]))
693+
Assert.AreEqual(-1,Seq.fold2 funcInt -1 (seq [1]) Seq.empty)
694+
695+
Assert.AreEqual(2,Seq.fold2 funcInt 0 (seq [1;2]) (seq [1]))
696+
Assert.AreEqual(4,Seq.fold2 funcInt 0 (seq [1]) (seq [3;6]))
697+
698+
// null Seq
699+
let nullSeq:seq<'a> = null
700+
701+
CheckThrowsArgumentNullException (fun () -> Seq.fold2 funcInt 0 nullSeq (seq [1]) |> ignore)
702+
CheckThrowsArgumentNullException (fun () -> Seq.fold2 funcInt 0 (seq [1]) nullSeq |> ignore)
703+
()
670704

671705
[<Test>]
672706
member this.ForAll() =

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ Microsoft.FSharp.Collections.SeqModule: T Sum[T](System.Collections.Generic.IEnu
449449
Microsoft.FSharp.Collections.SeqModule: TResult AverageBy[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], System.Collections.Generic.IEnumerable`1[T])
450450
Microsoft.FSharp.Collections.SeqModule: TResult Pick[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpOption`1[TResult]], System.Collections.Generic.IEnumerable`1[T])
451451
Microsoft.FSharp.Collections.SeqModule: TResult SumBy[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], System.Collections.Generic.IEnumerable`1[T])
452+
Microsoft.FSharp.Collections.SeqModule: TState Fold2[T1,T2,TState](Microsoft.FSharp.Core.FSharpFunc`2[TState,Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,TState]]], TState, System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2])
452453
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])
453454
Microsoft.FSharp.Collections.SeqModule: T[] ToArray[T](System.Collections.Generic.IEnumerable`1[T])
454455
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])

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ Microsoft.FSharp.Collections.SeqModule: T Sum[T](System.Collections.Generic.IEnu
443443
Microsoft.FSharp.Collections.SeqModule: TResult AverageBy[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], System.Collections.Generic.IEnumerable`1[T])
444444
Microsoft.FSharp.Collections.SeqModule: TResult Pick[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpOption`1[TResult]], System.Collections.Generic.IEnumerable`1[T])
445445
Microsoft.FSharp.Collections.SeqModule: TResult SumBy[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], System.Collections.Generic.IEnumerable`1[T])
446+
Microsoft.FSharp.Collections.SeqModule: TState Fold2[T1,T2,TState](Microsoft.FSharp.Core.FSharpFunc`2[TState,Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,TState]]], TState, System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2])
446447
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])
447448
Microsoft.FSharp.Collections.SeqModule: T[] ToArray[T](System.Collections.Generic.IEnumerable`1[T])
448449
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])

src/fsharp/FSharp.Core/seq.fs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,23 @@ namespace Microsoft.FSharp.Collections
10441044
use e = source.GetEnumerator()
10451045
let mutable state = x
10461046
while e.MoveNext() do
1047-
state <- f state e.Current;
1047+
state <- f state e.Current;
1048+
state
1049+
1050+
[<CompiledName("Fold2")>]
1051+
let fold2<'T1,'T2,'State> f (state:'State) (source1: seq<'T1>) (source2: seq<'T2>) =
1052+
checkNonNull "source1" source1
1053+
checkNonNull "source2" source2
1054+
1055+
use e1 = source1.GetEnumerator()
1056+
use e2 = source2.GetEnumerator()
1057+
1058+
let f = OptimizedClosures.FSharpFunc<_,_,_,_>.Adapt(f)
1059+
1060+
let mutable state = state
1061+
while e1.MoveNext() && e2.MoveNext() do
1062+
state <- f.Invoke(state, e1.Current, e2.Current)
1063+
10481064
state
10491065

10501066
[<CompiledName("Reduce")>]

src/fsharp/FSharp.Core/seq.fsi

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,19 @@ namespace Microsoft.FSharp.Collections
351351
[<CompiledName("Fold")>]
352352
val fold<'T,'State> : folder:('State -> 'T -> 'State) -> state:'State -> source:seq<'T> -> 'State
353353

354+
/// <summary>Applies a function to corresponding elements of two collections, threading an accumulator argument
355+
/// through the computation. The two sequences need not have equal lengths:
356+
/// when one sequence is exhausted any remaining elements in the other sequence are ignored.
357+
/// If the input function is <c>f</c> and the elements are <c>i0...iN</c> and <c>j0...jN</c>
358+
/// then computes <c>f (... (f s i0 j0)...) iN jN</c>.</summary>
359+
/// <param name="folder">The function to update the state given the input elements.</param>
360+
/// <param name="state">The initial state.</param>
361+
/// <param name="source1">The first input sequence.</param>
362+
/// <param name="source2">The second input sequence.</param>
363+
/// <returns>The final state value.</returns>
364+
[<CompiledName("Fold2")>]
365+
val fold2<'T1,'T2,'State> : folder:('State -> 'T1 -> 'T2 -> 'State) -> state:'State -> source1:seq<'T1> -> source2:seq<'T2> -> 'State
366+
354367
/// <summary>Tests if all elements of the sequence satisfy the given predicate.</summary>
355368
///
356369
/// <remarks>The predicate is applied to the elements of the input sequence. If any application

0 commit comments

Comments
 (0)