Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit15f04de

Browse files
forkilatkin
authored andcommitted
Implement Seq.fold2
commit 3c379b6deba89aeccd0af4d3744f69076f6cc34dMerge:f197816 6e6f68cAuthor: latkin <latkin@microsoft.com>Date: Sun Oct 12 10:23:58 2014 -0700 Merge branch 'fold2' ofhttps://git01.codeplex.com/forks/forki/fsharp into PRcommit 6e6f68ce8b1b0dbf7deb92cf1c6887792118821dAuthor: Steffen Forkmann <steffen.forkmann@msu-solutions.de>Date: Fri Jul 4 14:49:20 2014 +0200 Adding surface are for fold2commit ac88069f9d2ed8196e5cdbd5294d891d52942b47Author: Steffen Forkmann <steffen.forkmann@msu-solutions.de>Date: Fri Jul 4 14:45:05 2014 +0200 Changing Seq.fold2 - sequences need not have equal lengthscommit 74adf606599ba4b648142c532249f782501f0758Author: Steffen Forkmann <steffen.forkmann@msu-solutions.de>Date: Thu Jul 3 23:06:41 2014 +0200 Check exceptions for Seq.fold2commit 2be78be51151b83cc9f294254b9de7b20fd71b79Author: Steffen Forkmann <steffen.forkmann@msu-solutions.de>Date: Thu Jul 3 23:02:49 2014 +0200 Implementing Seq.fold2
1 parentf197816 commit15f04de

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 funcInt1 nullSeq|> ignore)
669669
()
670+
671+
672+
673+
[<Test>]
674+
memberthis.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+
letfuncInt x y z= x+ y+ z
679+
letresultInt= Seq.fold2 funcInt9(seq[1..10])(seq[1..2..20])
680+
Assert.AreEqual(164, resultInt)
681+
682+
// string List
683+
letfuncStr x y z= x+ y+ z
684+
letresultStr= Seq.fold2 funcStr"*"["a";"b";"c";"d"]["A";"B";"C";"D"]
685+
Assert.AreEqual("*aAbBcCdD", resultStr)
686+
687+
// empty List
688+
letemptyArr:int list=[]
689+
letresultEpt= Seq.fold2 funcInt5 emptyArr emptyArr
690+
Assert.AreEqual(5, resultEpt)
691+
692+
Assert.AreEqual(0,Seq.fold2 funcInt0 Seq.empty(seq[1]))
693+
Assert.AreEqual(-1,Seq.fold2 funcInt-1(seq[1]) Seq.empty)
694+
695+
Assert.AreEqual(2,Seq.fold2 funcInt0(seq[1;2])(seq[1]))
696+
Assert.AreEqual(4,Seq.fold2 funcInt0(seq[1])(seq[3;6]))
697+
698+
// null Seq
699+
letnullSeq:seq<'a>=null
700+
701+
CheckThrowsArgumentNullException(fun()-> Seq.fold2 funcInt0 nullSeq(seq[1])|> ignore)
702+
CheckThrowsArgumentNullException(fun()-> Seq.fold2 funcInt0(seq[1]) nullSeq|> ignore)
703+
()
670704

671705
[<Test>]
672706
memberthis.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 mutablestate= 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+
letfold2<'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+
letf= OptimizedClosures.FSharpFunc<_,_,_,_>.Adapt(f)
1059+
1060+
let mutablestate= 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)

[8]ページ先頭

©2009-2025 Movatter.jp