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

Commitdf72269

Browse files
PatrickMcDonaldlatkin
authored andcommitted
Implement Seq.scanBack
commit c5c33d7595e1f14ad9976e6ba65cf48b48fbf2caAuthor: latkin <latkin@microsoft.com>Date: Thu Oct 30 17:31:31 2014 -0700 Fix testcommit 834d377c35556362070bcce07ae1f4022286a6b6Author: latkin <latkin@microsoft.com>Date: Thu Oct 30 17:21:48 2014 -0700 Removing bounds checks from scanSubRightcommit fe2facfcb81d60da5beb614e1f80e85c10d1700eMerge: d290360 2278393Author: latkin <latkin@microsoft.com>Date: Thu Oct 30 17:03:18 2014 -0700 Merge branch 'scanBack' ofhttps://git01.codeplex.com/forks/patrickmcdonald/visualfsharp into PR Conflicts: src/fsharp/FSharp.Core/local.fs src/fsharp/FSharp.Core/seq.fscommit 22783931672028fd6004d02f00d010c1af11e4a5Author: Patrick McDonald <paddymcdonald@gmail.com>Date: Tue Sep 16 11:12:27 2014 +0100 Add more Seq.scanBack tests; update XML docscommit 23267bdda1fb421ac808308bb64e1210f3b99053Author: Patrick McDonald <paddymcdonald@gmail.com>Date: Mon Sep 8 22:07:46 2014 +0100 Implement seq.scanBack
1 parent45d622c commitdf72269

File tree

8 files changed

+76
-13
lines changed

8 files changed

+76
-13
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,46 @@ type SeqModule2() =
11001100
CheckThrowsArgumentNullException(fun()-> Seq.scan funcInt5 seqNull|> ignore)
11011101
()
11021102

1103+
[<Test>]
1104+
memberthis.ScanBack()=
1105+
// integer Seq
1106+
letfuncInt x y= x+y
1107+
letresultInt= Seq.scanBack funcInt{1..10}9
1108+
letexpectedInt= seq[64;63;61;58;54;49;43;36;28;19;9]
1109+
VerifySeqsEqual expectedInt resultInt
1110+
1111+
// string Seq
1112+
letfuncStr x y= x+y
1113+
letresultStr= Seq.scanBack funcStr(seq["A";"B";"C";"D"])"X"
1114+
letexpectedStr= seq["ABCDX";"BCDX";"CDX";"DX";"X"]
1115+
VerifySeqsEqual expectedStr resultStr
1116+
1117+
// empty Seq
1118+
letresultEpt= Seq.scanBack funcInt Seq.empty5
1119+
letexpectedEpt= seq[5]
1120+
VerifySeqsEqual expectedEpt resultEpt
1121+
1122+
// null Seq
1123+
letseqNull:seq<'a>=null
1124+
CheckThrowsArgumentNullException(fun()-> Seq.scanBack funcInt seqNull5|> ignore)
1125+
1126+
// exception cases
1127+
letfuncEx x(s:'State)= raise<|new System.FormatException(): 'State
1128+
// calling scanBack with funcEx does not throw
1129+
letresultEx= Seq.scanBack funcEx(seq{1..10})0
1130+
// reading from resultEx throws
1131+
CheckThrowsFormatException(fun()-> Seq.head resultEx|> ignore)
1132+
1133+
// Result consumes entire input sequence as soon as it is accesses an element
1134+
leti= ref0
1135+
letfuncState x s=(i:=!i+ x); x+s
1136+
letresultState= Seq.scanBack funcState(seq{1..3})0
1137+
Assert.AreEqual(0,!i)
1138+
use e= resultState.GetEnumerator()
1139+
Assert.AreEqual(6,!i)
1140+
1141+
()
1142+
11031143
[<Test>]
11041144
memberthis.Singleton()=
11051145
// 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
@@ -444,6 +444,7 @@ Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1
444444
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])
445445
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])
446446
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])
447+
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TState] ScanBack[T,TState](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[TState,TState]], System.Collections.Generic.IEnumerable`1[T], TState)
447448
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])
448449
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T[]] Windowed[T](Int32, System.Collections.Generic.IEnumerable`1[T])
449450
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Append[T](System.Collections.Generic.IEnumerable`1[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
@@ -438,6 +438,7 @@ Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1
438438
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])
439439
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])
440440
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])
441+
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TState] ScanBack[T,TState](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[TState,TState]], System.Collections.Generic.IEnumerable`1[T], TState)
441442
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])
442443
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T[]] Windowed[T](Int32, System.Collections.Generic.IEnumerable`1[T])
443444
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Append[T](System.Collections.Generic.IEnumerable`1[T], System.Collections.Generic.IEnumerable`1[T])

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -682,16 +682,6 @@ namespace Microsoft.FSharp.Collections
682682
res<- f.Invoke(array.[i],res)
683683
res
684684

685-
letscanSubRight f(array:_[])start fin initState=
686-
checkNonNull"array" array
687-
letf= OptimizedClosures.FSharpFunc<_,_,_>.Adapt(f)
688-
let mutablestate= initState
689-
letres= create(2+fin-start) initState
690-
for i= findownto startdo
691-
state<- f.Invoke(array.[i],state);
692-
res.[i- start]<- state
693-
res
694-
695685
letscanSubLeft f initState(array:_[])start fin=
696686
checkNonNull"array" array
697687
letf= OptimizedClosures.FSharpFunc<_,_,_>.Adapt(f)
@@ -711,8 +701,7 @@ namespace Microsoft.FSharp.Collections
711701
[<CompiledName("ScanBack")>]
712702
letscanBack<'T,'State>f(array:'T[])(acc:'State)=
713703
checkNonNull"array" array
714-
letlen= array.Length
715-
scanSubRight f array0(len-1) acc
704+
Microsoft.FSharp.Primitives.Basics.Array.scanSubRight f array0(array.Length-1) acc
716705

717706
[<CompiledName("Singleton")>]
718707
let inlinesingleton value=[|value|]

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,16 @@ module internal Array =
769769
acc<- s'
770770
res, acc
771771

772+
letscanSubRight f(array:_[])start fin initState=
773+
letf= OptimizedClosures.FSharpFunc<_,_,_>.Adapt(f)
774+
let mutablestate= initState
775+
letres= zeroCreateUnchecked(fin-start+2)
776+
res.[fin- start+1]<- state
777+
for i= findownto startdo
778+
state<- f.Invoke(array.[i], state);
779+
res.[i- start]<- state
780+
res
781+
772782
letunstableSortInPlaceBy(f:'T->'U)(array:array<'T>)=
773783
letlen= array.Length
774784
if len<2then()

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ module internal Array =
5555

5656
val permute: indexMap:(int-> int)-> 'T[]-> 'T[]
5757

58+
val scanSubRight: f:('T-> 'State-> 'State)-> array:'T[]-> start:int-> fin:int-> initState:'State-> 'State[]
59+
5860
val unstableSortInPlaceBy: projection:('T-> 'Key)-> array:'T[]-> unit when 'Key: comparison
5961

6062
val unstableSortInPlace: array:'T[]-> unit when 'T: comparison

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,14 @@ namespace Microsoft.FSharp.Collections
13181318
checkNonNull"source" source
13191319
source|> toArray|> Array.findBack f
13201320

1321+
[<CompiledName("ScanBack")>]
1322+
letscanBack<'T,'State>f(source:seq<'T>)(acc:'State)=
1323+
checkNonNull"source" source
1324+
mkDelayedSeq(fun()->
1325+
letarr= source|> toArray
1326+
letres= Array.scanSubRight f arr0(arr.Length-1) acc
1327+
res:> seq<_>)
1328+
13211329
[<CompiledName("FindIndex")>]
13221330
letfindIndex p(source:seq<_>)=
13231331
checkNonNull"source" source
@@ -1788,4 +1796,4 @@ namespace Microsoft.FSharp.Collections
17881796
checkNonNull"source" source
17891797
letarray= source|> toArray
17901798
letarr,state= Array.mapFoldBack f array acc
1791-
readonly arr, state
1799+
readonly arr, state

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,18 @@ namespace Microsoft.FSharp.Collections
916916
[<CompiledName("Scan")>]
917917
val scan<'T,'State>:folder:('State-> 'T-> 'State)->state:'State->source:seq<'T>->seq<'State>
918918

919+
///<summary>Like <c>foldBack</c>,but returns the sequence of intermediary and final results.</summary>
920+
///<remarks>This function returns a sequence that digests the whole initial sequence as soon as that
921+
///sequence is iterated. As a result this function should not be used with large or infinite sequences.
922+
///</remarks>
923+
///<param name="folder">A function that updates the state with each element from the sequence.</param>
924+
///<param name="source">The input sequence.</param>
925+
///<param name="state">The initial state.</param>
926+
///<returns>The resulting sequence of computed states.</returns>
927+
///<exception cref="System.ArgumentNullException">Thrown when the input sequence is null.</exception>
928+
[<CompiledName("ScanBack")>]
929+
val scanBack<'T,'State>:folder:('T-> 'State-> 'State)->source:seq<'T>->state:'State->seq<'State>
930+
919931
///<summary>Returns a sequence that yields one item only.</summary>
920932
///
921933
///<param name="value">The input item.</param>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp