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

Commit91c644d

Browse files
PatrickMcDonaldlatkin
authored andcommitted
Implement Seq.foldBack2
commit dab1082f5cd233a1525b443286fdbefd036da332Author: latkin <latkin@microsoft.com>Date: Thu Dec 11 17:04:09 2014 -0800 Escaping XML doc commentcommit 3dda1099e6884acb63b3c2de7b4b9dce2a9d1696Author: latkin <latkin@microsoft.com>Date: Thu Dec 11 16:08:28 2014 -0800 Use built-in <|| operatorcommit db210d02e92c8ecc65086c457e11fc4d5de3ee34Author: Patrick McDonald <paddymcdonald@gmail.com>Date: Wed Dec 10 11:57:38 2014 +0000 Refactor Seq.foldBack2 to use an uncurry functioncommit 83f56ce9881616da0b1517a9ae1b21288dcb1a84Author: Patrick McDonald <paddymcdonald@gmail.com>Date: Tue Dec 9 20:37:43 2014 +0000 Change Seq.foldBack2 to use zipcommit a8f3da0500bc57bdd0772c6026615373313a433fAuthor: Patrick McDonald <paddymcdonald@gmail.com>Date: Tue Nov 25 22:17:14 2014 +0000 Implement Seq.foldBack2
1 parentc3a0902 commit91c644d

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,45 @@ type SeqModule() =
811811

812812
()
813813

814+
[<Test>]
815+
memberthis.foldBack2()=
816+
// int Seq
817+
letfuncInt x y z= x+ y+ z
818+
letintSeq=seq{1..10}
819+
letresultInt= Seq.foldBack2 funcInt intSeq(seq{1..2..20})9
820+
Assert.AreEqual(164, resultInt)
821+
822+
// string Seq
823+
letfuncStr= sprintf"%s%s%s"
824+
letstrSeq= seq["A";"B";"C";"D"]
825+
letresultStr= Seq.foldBack2 funcStr strSeq(seq["a";"b";"c";"d"])"*"
826+
Assert.AreEqual("AaBbCcDd*", resultStr)
827+
828+
// single element
829+
letstrSeqSingle= seq["X"]
830+
Assert.AreEqual("XAZ", Seq.foldBack2 funcStr strSeqSingle strSeq"Z")
831+
Assert.AreEqual("AXZ", Seq.foldBack2 funcStr strSeq strSeqSingle"Z")
832+
Assert.AreEqual("XYZ", Seq.foldBack2 funcStr strSeqSingle(seq["Y"])"Z")
833+
834+
// empty Seq
835+
letemptySeq= Seq.empty
836+
Assert.AreEqual(1, Seq.foldBack2 funcInt emptySeq emptySeq1)
837+
Assert.AreEqual(1, Seq.foldBack2 funcInt emptySeq intSeq1)
838+
Assert.AreEqual(1, Seq.foldBack2 funcInt intSeq emptySeq1)
839+
840+
// infinite Seq
841+
letinfiniteSeq= Seq.initInfinite(fun i->2* i+1)
842+
Assert.AreEqual(164, Seq.foldBack2 funcInt intSeq infiniteSeq9)
843+
Assert.AreEqual(164, Seq.foldBack2 funcInt infiniteSeq intSeq9)
844+
845+
// null Seq
846+
letnullSeq:seq<'a>=null
847+
CheckThrowsArgumentNullException(fun()-> Seq.foldBack2 funcInt nullSeq intSeq1|> ignore)
848+
CheckThrowsArgumentNullException(fun()-> Seq.foldBack2 funcInt intSeq nullSeq1|> ignore)
849+
CheckThrowsArgumentNullException(fun()-> Seq.foldBack2 funcInt nullSeq nullSeq1|> ignore)
850+
851+
()
852+
814853
[<Test>]
815854
memberthis.ForAll()=
816855

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ Microsoft.FSharp.Collections.SeqModule: TResult AverageBy[T,TResult](Microsoft.F
511511
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])
512512
Microsoft.FSharp.Collections.SeqModule: TResult SumBy[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], System.Collections.Generic.IEnumerable`1[T])
513513
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])
514+
Microsoft.FSharp.Collections.SeqModule: TState FoldBack2[T1,T2,TState](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,Microsoft.FSharp.Core.FSharpFunc`2[TState,TState]]], System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2], TState)
514515
Microsoft.FSharp.Collections.SeqModule: TState FoldBack[T,TState](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[TState,TState]], System.Collections.Generic.IEnumerable`1[T], TState)
515516
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])
516517
Microsoft.FSharp.Collections.SeqModule: T[] ToArray[T](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
@@ -505,6 +505,7 @@ Microsoft.FSharp.Collections.SeqModule: TResult AverageBy[T,TResult](Microsoft.F
505505
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])
506506
Microsoft.FSharp.Collections.SeqModule: TResult SumBy[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], System.Collections.Generic.IEnumerable`1[T])
507507
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])
508+
Microsoft.FSharp.Collections.SeqModule: TState FoldBack2[T1,T2,TState](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,Microsoft.FSharp.Core.FSharpFunc`2[TState,TState]]], System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2], TState)
508509
Microsoft.FSharp.Collections.SeqModule: TState FoldBack[T,TState](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[TState,TState]], System.Collections.Generic.IEnumerable`1[T], TState)
509510
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])
510511
Microsoft.FSharp.Collections.SeqModule: T[] ToArray[T](System.Collections.Generic.IEnumerable`1[T])

‎src/fsharp/FSharp.Core/seq.fs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,11 @@ namespace Microsoft.FSharp.Collections
12551255
letlen= arr.Length
12561256
foldArraySubRight f arr0(len-1) x
12571257

1258+
[<CompiledName("FoldBack2")>]
1259+
letfoldBack2<'T1,'T2,'State>f(source1:seq<'T1>)(source2:seq<'T2>)(x:'State)=
1260+
letzipped= zip source1 source2
1261+
foldBack((<||) f) zipped x
1262+
12581263
[<CompiledName("ReduceBack")>]
12591264
letreduceBack f(source:seq<'T>)=
12601265
checkNonNull"source" source

‎src/fsharp/FSharp.Core/seq.fsi‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ namespace Microsoft.FSharp.Collections
385385
///<param name="source1">The first input sequence.</param>
386386
///<param name="source2">The second input sequence.</param>
387387
///<returns>The final state value.</returns>
388+
///<exception cref="System.ArgumentNullException">Thrown when the either of the input sequences is null.</exception>
388389
[<CompiledName("Fold2")>]
389390
val fold2<'T1,'T2,'State>:folder:('State-> 'T1-> 'T2-> 'State)->state:'State->source1:seq<'T1>->source2:seq<'T2>-> 'State
390391

@@ -399,6 +400,19 @@ namespace Microsoft.FSharp.Collections
399400
[<CompiledName("FoldBack")>]
400401
val foldBack<'T,'State>:folder:('T-> 'State-> 'State)->source:seq<'T>->state:'State-> 'State
401402

403+
///<summary>Applies a function to corresponding elements of two collections,starting from the end of the shorter collection,
404+
///threading an accumulator argument through the computation. The two sequences need not have equal lengths.
405+
///If the input function is <c>f</c> and the elements are <c>i0...iN</c> and <c>j0...jM</c>,N&lt;M
406+
///then computes <c>f i0 j0(...(f iN jN s)...)</c>.</summary>
407+
///<param name="folder">The function to update the state given the input elements.</param>
408+
///<param name="source1">The first input sequence.</param>
409+
///<param name="source2">The second input sequence.</param>
410+
///<param name="state">The initial state.</param>
411+
///<returns>The final state value.</returns>
412+
///<exception cref="System.ArgumentNullException">Thrown when the either of the input sequences is null.</exception>
413+
[<CompiledName("FoldBack2")>]
414+
val foldBack2<'T1,'T2,'State>:folder:('T1-> 'T2-> 'State-> 'State)->source1:seq<'T1>->source2:seq<'T2>->state:'State-> 'State
415+
402416
///<summary>Tests if all elements of the sequence satisfy the given predicate.</summary>
403417
///
404418
///<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