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

Commit5f20697

Browse files
PatrickMcDonaldlatkin
authored andcommitted
Implement Array.truncate, List.truncate
commit b2f4ec941e7388b25354c310cc693600fac2adfbAuthor: latkin <latkin@microsoft.com>Date: Tue Nov 4 14:13:06 2014 -0800 Remove slicing in favor of zeroCreateUnchecked + Array.Copycommit ed9db6767b5fd85d5e6f7119fc044c29f2fdf487Merge:c27ff34 6b2fa35Author: latkin <latkin@microsoft.com>Date: Tue Nov 4 14:02:52 2014 -0800 Merge branch 'truncate' ofhttps://git01.codeplex.com/forks/patrickmcdonald/visualfsharp into PR Conflicts: src/fsharp/FSharp.Core.Unittests/SurfaceArea.4.0.fs src/fsharp/FSharp.Core.Unittests/SurfaceArea.Portable.fs src/fsharp/FSharp.Core/array.fs src/fsharp/FSharp.Core/list.fs src/fsharp/FSharp.Core/list.fsi src/fsharp/FSharp.Core/local.fsicommit 6b2fa358a6ded7199a9085c982dba430f6b47c67Author: Patrick McDonald <paddymcdonald@gmail.com>Date: Tue Sep 9 13:42:14 2014 +0100 Update Array.truncate and List.truncate after code reviewcommit 42a7a3f12310bab42e1c1ce87664f527b426b7adAuthor: Patrick McDonald <paddymcdonald@gmail.com>Date: Tue Sep 9 00:24:10 2014 +0100 Implement List.truncate and Array.truncate
1 parentc27ff34 commit5f20697

File tree

10 files changed

+100
-3
lines changed

10 files changed

+100
-3
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,28 @@ type ArrayModule2() =
838838

839839
()
840840

841+
[<Test>]
842+
memberthis.Truncate()=
843+
// integer array
844+
Assert.AreEqual([|1..3|], Array.truncate3[|1..5|])
845+
Assert.AreEqual([|1..5|], Array.truncate10[|1..5|])
846+
Assert.AreEqual([||], Array.truncate0[|1..5|])
847+
848+
// string array
849+
Assert.AreEqual([|"str1";"str2"|], Array.truncate2[|"str1";"str2";"str3"|])
850+
851+
// empty array
852+
Assert.AreEqual([||], Array.truncate0[||])
853+
Assert.AreEqual([||], Array.truncate1[||])
854+
855+
// null array
856+
CheckThrowsArgumentNullException(fun()-> Array.truncate1null|> ignore)
857+
858+
// negative count
859+
CheckThrowsArgumentException(fun()-> Array.truncate-1[|1..5|]|> ignore)
860+
861+
()
862+
841863
[<Test>]
842864
memberthis.TryFind()=
843865
// integer array

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,25 @@ type ListModule02() =
717717

718718
()
719719

720+
[<Test>]
721+
memberthis.Truncate()=
722+
// integer list
723+
Assert.AreEqual([1..3], List.truncate3[1..5])
724+
Assert.AreEqual([1..5], List.truncate10[1..5])
725+
Assert.AreEqual([], List.truncate0[1..5])
726+
727+
// string list
728+
Assert.AreEqual(["str1";"str2"], List.truncate2["str1";"str2";"str3"])
729+
730+
// empty list
731+
Assert.AreEqual([], List.truncate0[])
732+
Assert.AreEqual([], List.truncate1[])
733+
734+
// negative count
735+
CheckThrowsArgumentException(fun()-> List.truncate-1[1..5]|> ignore)
736+
737+
()
738+
720739
[<Test>]
721740
memberthis.TryFind()=
722741
// integer List

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ Microsoft.FSharp.Collections.ArrayModule: T[] Sort[T](T[])
185185
Microsoft.FSharp.Collections.ArrayModule: T[] Tail[T](T[])
186186
Microsoft.FSharp.Collections.ArrayModule: T[] TakeWhile[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
187187
Microsoft.FSharp.Collections.ArrayModule: T[] Take[T](Int32, T[])
188+
Microsoft.FSharp.Collections.ArrayModule: T[] Truncate[T](Int32, T[])
188189
Microsoft.FSharp.Collections.ArrayModule: T[] Where[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
189190
Microsoft.FSharp.Collections.ArrayModule: T[] ZeroCreate[T](Int32)
190191
Microsoft.FSharp.Collections.ArrayModule: Void CopyTo[T](T[], Int32, T[], Int32, Int32)
@@ -336,6 +337,7 @@ Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList
336337
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Tail[T](Microsoft.FSharp.Collections.FSharpList`1[T])
337338
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] TakeWhile[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
338339
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Take[T](Int32, Microsoft.FSharp.Collections.FSharpList`1[T])
340+
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Truncate[T](Int32, Microsoft.FSharp.Collections.FSharpList`1[T])
339341
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Where[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
340342
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[System.Int32] TryFindIndexBack[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
341343
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[System.Int32] TryFindIndex[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ Microsoft.FSharp.Collections.ArrayModule: T[] Sort[T](T[])
179179
Microsoft.FSharp.Collections.ArrayModule: T[] TakeWhile[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
180180
Microsoft.FSharp.Collections.ArrayModule: T[] Tail[T](T[])
181181
Microsoft.FSharp.Collections.ArrayModule: T[] Take[T](Int32, T[])
182+
Microsoft.FSharp.Collections.ArrayModule: T[] Truncate[T](Int32, T[])
182183
Microsoft.FSharp.Collections.ArrayModule: T[] Where[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
183184
Microsoft.FSharp.Collections.ArrayModule: T[] ZeroCreate[T](Int32)
184185
Microsoft.FSharp.Collections.ArrayModule: Void CopyTo[T](T[], Int32, T[], Int32, Int32)
@@ -330,6 +331,7 @@ Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList
330331
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Tail[T](Microsoft.FSharp.Collections.FSharpList`1[T])
331332
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] TakeWhile[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
332333
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Take[T](Int32, Microsoft.FSharp.Collections.FSharpList`1[T])
334+
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Truncate[T](Int32, Microsoft.FSharp.Collections.FSharpList`1[T])
333335
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Where[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
334336
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[System.Int32] TryFindIndexBack[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
335337
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[System.Int32] TryFindIndex[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -944,14 +944,25 @@ namespace Microsoft.FSharp.Collections
944944
for i= targetIndexto targetIndex+ count-1do
945945
target.[i]<- x
946946

947-
948947
[<CompiledName("ExactlyOne")>]
949948
letexactlyOne(array:'T[])=
950949
checkNonNull"array" array
951950
if array.Length=1then array.[0]
952951
elif array.Length=0then invalidArg"array" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
953952
else invalidArg"array"(SR.GetString(SR.inputSequenceTooLong))
954953

954+
[<CompiledName("Truncate")>]
955+
lettruncate count(array:'T[])=
956+
checkNonNull"array" array
957+
if count<0then invalidArg"count"(SR.GetString(SR.inputMustBeNonNegative))
958+
if count=0then empty
959+
else
960+
letlen= array.Length
961+
letcount'= Operators.min count len
962+
letresult:'T[]= Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked count'
963+
Array.Copy(array, result, count')
964+
result
965+
955966
#if FX_NO_TPL_PARALLEL
956967
#else
957968
moduleParallel=

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,15 @@ namespace Microsoft.FSharp.Collections
861861
[<CompiledName("ToSeq")>]
862862
val toSeq:array:'T[]->seq<'T>
863863

864+
///<summary>Returns at most N elements in a new array.</summary>
865+
///<param name="count">The maximum number of items to return.</param>
866+
///<param name="array">The input array.</param>
867+
///<returns>The result array.</returns>
868+
///<exception cref="System.ArgumentNullException">Thrown when the input array is null.</exception>
869+
///<exception cref="System.ArgumentException">Thrown when the count is negative.</exception>
870+
[<CompiledName("Truncate")>]
871+
val truncate:count:int->array:'T[]-> 'T[]
872+
864873
///<summary>Returns the first element for which the given function returns <c>true</c>.
865874
///Return <c>None</c> if no such element exists.</summary>
866875
///<param name="predicate">The function to test the input elements.</param>

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,4 +570,6 @@ namespace Microsoft.FSharp.Collections
570570
|[x]-> x
571571
|[]-> invalidArg"source" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
572572
|_-> invalidArg"source"(SR.GetString(SR.inputSequenceTooLong))
573-
573+
574+
[<CompiledName("Truncate")>]
575+
lettruncate count list= Microsoft.FSharp.Primitives.Basics.List.truncate count list

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,14 @@ namespace Microsoft.FSharp.Collections
727727
[<CompiledName("TryHead")>]
728728
val tryHead:list:'T list-> 'T option
729729

730+
///<summary>Returns at most N elements in a new list.</summary>
731+
///<param name="count">The maximum number of items to return.</param>
732+
///<param name="array">The input list.</param>
733+
///<returns>The result list.</returns>
734+
///<exception cref="System.ArgumentException">Thrown when the count is negative.</exception>
735+
[<CompiledName("Truncate")>]
736+
val truncate:count:int->list:'T list-> 'T list
737+
730738
///<summary>Applies the given function to successive elements,returning <c>Some(x)</c> the first
731739
///result where function returns <c>Some(x)</c> for some x. If no such element
732740
///exists then return <c>None</c>.</summary>

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,28 @@ module internal List =
405405
if p h
406406
then cons,(partitionToFreshConsTailLeft cons p t)
407407
else(partitionToFreshConsTailRight cons p t), cons
408-
408+
409+
let rectruncateToFreshConsTail cons count list=
410+
if count=0then setFreshConsTail cons[]else
411+
match listwith
412+
|[]-> setFreshConsTail cons[]
413+
| h::t->
414+
letcons2= freshConsNoTail h
415+
setFreshConsTail cons cons2;
416+
truncateToFreshConsTail cons2(count-1) t
417+
418+
lettruncate count list=
419+
if count<0then invalidArg"count"(SR.GetString(SR.inputMustBeNonNegative))
420+
match listwith
421+
|[]-> list
422+
|_::([]as nil)->if count>0then listelse nil
423+
| h::t->
424+
if count=0then[]
425+
else
426+
letcons= freshConsNoTail h
427+
truncateToFreshConsTail cons(count-1) t
428+
cons
429+
409430
// optimized mutation-based implementation. This code is only valid in fslib, where mutation of private
410431
// tail cons cells is permitted in carefully written library code.
411432
let recunzipToFreshConsTail cons1a cons1b x=

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module internal List =
3434
val toArray: 'T list-> 'T[]
3535
val sortWith:('T-> 'T-> int)-> 'T list-> 'T list
3636
val splitAt: int-> 'T list->('T list* 'T list)
37+
val truncate: int-> 'T list-> 'T list
3738

3839
module internal Array=
3940
// The input parameter should be checked by callers if necessary

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp