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

Commit3c39e6e

Browse files
forkilatkin
authored andcommitted
Implement Array.take, List.take
Commits: implementing "take" for array and list Check for ArgumentNullException in Array.take Fix typo Performance improvement for Array.take Performance improvement for List.take Use temporay array for List.take Do not use array slicing for Array.take Do not check length of list in List.take Use mutable list instead of temporay array for List.take Add List.Take and Array.Take to surface Always create a new array in Array.take Remove special case for List.take Simplify List.take as suggested by@latkin
1 parentbd6ac78 commit3c39e6e

File tree

10 files changed

+96
-1
lines changed

10 files changed

+96
-1
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,19 @@ type ArrayModule() =
186186
letlist=new System.Collections.Generic.List<int>()
187187
Assert.AreEqual([|null, list|], Array.distinctBy id[|null, list|])
188188

189+
[<Test>]
190+
memberthis.Take()=
191+
Assert.AreEqual([||],Array.take0[||])
192+
Assert.AreEqual([||],Array.take0[|"str1";"str2";"str3";"str4"|])
193+
Assert.AreEqual([|1;2;4|],Array.take3[|1;2;4;5;7|])
194+
Assert.AreEqual([|"str1";"str2"|],Array.take2[|"str1";"str2";"str3";"str4"|])
195+
Assert.AreEqual([|"str1";"str2";"str3";"str4"|],Array.take4[|"str1";"str2";"str3";"str4"|])
196+
197+
CheckThrowsInvalidOperationExn(fun()-> Array.take1[||]|> ignore)
198+
CheckThrowsArgumentException(fun()-> Array.take-1[|0;1|]|> ignore)
199+
CheckThrowsInvalidOperationExn(fun()-> Array.take5[|"str1";"str2";"str3";"str4"|]|> ignore)
200+
CheckThrowsArgumentNullException(fun()-> Array.take5null|> ignore)
201+
189202
[<Test>]
190203
memberthis.Blit()=
191204
// int array

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,17 @@ type ListModule() =
144144
letlist=new System.Collections.Generic.List<int>()
145145
Assert.AreEqual([null, list], List.distinctBy id[null, list])
146146

147+
[<Test>]
148+
memberthis.Take()=
149+
Assert.AreEqual([],List.take0[])
150+
Assert.AreEqual([],List.take0["str1";"str2";"str3";"str4"])
151+
Assert.AreEqual([1;2;4],List.take3[1;2;4;5;7])
152+
Assert.AreEqual(["str1";"str2"],List.take2["str1";"str2";"str3";"str4"])
153+
154+
CheckThrowsInvalidOperationExn(fun()-> List.take1[]|> ignore)
155+
CheckThrowsArgumentException(fun()-> List.take-1[0;1]|> ignore)
156+
CheckThrowsInvalidOperationExn(fun()-> List.take5["str1";"str2";"str3";"str4"]|> ignore)
157+
147158
[<Test>]
148159
memberthis.Choose()=
149160
// int List

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ Microsoft.FSharp.Collections.ArrayModule: T[] Singleton[T](T)
163163
Microsoft.FSharp.Collections.ArrayModule: T[] SortBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], T[])
164164
Microsoft.FSharp.Collections.ArrayModule: T[] SortWith[T](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[T,System.Int32]], T[])
165165
Microsoft.FSharp.Collections.ArrayModule: T[] Sort[T](T[])
166+
Microsoft.FSharp.Collections.ArrayModule: T[] Take[T](Int32, T[])
166167
Microsoft.FSharp.Collections.ArrayModule: T[] Where[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
167168
Microsoft.FSharp.Collections.ArrayModule: T[] ZeroCreate[T](Int32)
168169
Microsoft.FSharp.Collections.ArrayModule: Void CopyTo[T](T[], Int32, T[], Int32, Int32)
@@ -305,6 +306,7 @@ Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList
305306
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] SortWith[T](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[T,System.Int32]], Microsoft.FSharp.Collections.FSharpList`1[T])
306307
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Sort[T](Microsoft.FSharp.Collections.FSharpList`1[T])
307308
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Tail[T](Microsoft.FSharp.Collections.FSharpList`1[T])
309+
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Take[T](Int32, Microsoft.FSharp.Collections.FSharpList`1[T])
308310
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])
309311
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])
310312
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[TResult] TryPick[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpOption`1[TResult]], 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
@@ -157,6 +157,7 @@ Microsoft.FSharp.Collections.ArrayModule: T[] Singleton[T](T)
157157
Microsoft.FSharp.Collections.ArrayModule: T[] SortBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], T[])
158158
Microsoft.FSharp.Collections.ArrayModule: T[] SortWith[T](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[T,System.Int32]], T[])
159159
Microsoft.FSharp.Collections.ArrayModule: T[] Sort[T](T[])
160+
Microsoft.FSharp.Collections.ArrayModule: T[] Take[T](Int32, T[])
160161
Microsoft.FSharp.Collections.ArrayModule: T[] Where[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
161162
Microsoft.FSharp.Collections.ArrayModule: T[] ZeroCreate[T](Int32)
162163
Microsoft.FSharp.Collections.ArrayModule: Void CopyTo[T](T[], Int32, T[], Int32, Int32)
@@ -299,6 +300,7 @@ Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList
299300
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] SortWith[T](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[T,System.Int32]], Microsoft.FSharp.Collections.FSharpList`1[T])
300301
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Sort[T](Microsoft.FSharp.Collections.FSharpList`1[T])
301302
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Tail[T](Microsoft.FSharp.Collections.FSharpList`1[T])
303+
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Take[T](Int32, Microsoft.FSharp.Collections.FSharpList`1[T])
302304
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])
303305
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])
304306
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[TResult] TryPick[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpOption`1[TResult]], Microsoft.FSharp.Collections.FSharpList`1[T])

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ namespace Microsoft.FSharp.Collections
107107
result.[i]<- f array.[i]
108108
concatArrays result
109109

110+
[<CompiledName("Take")>]
111+
lettake count(array:'T[])=
112+
checkNonNull"array" array
113+
if count<0then invalidArg"count"(SR.GetString(SR.inputMustBeNonNegative))
114+
if count=0then emptyelse
115+
if count> array.Lengththen
116+
raise<| System.InvalidOperationException(SR.GetString(SR.notEnoughElements))
117+
118+
letres:'T[]= Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked count
119+
120+
Array.Copy(array,0, res,0, count)
121+
res
122+
110123
[<CompiledName("Append")>]
111124
letappend(array1:'T[])(array2:'T[])=
112125
checkNonNull"array1" array1

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,23 @@ namespace Microsoft.FSharp.Collections
640640
when ^U:(static member(+):^U*^U->^U)
641641
and ^U:(static member Zero:^U)
642642

643+
///<summary>Returns the first N elements of the array.</summary>
644+
///<remarks>Throws <c>InvalidOperationException</c>
645+
///if the count exceeds the number of elements in the array. <c>Array.truncate</c>
646+
///returns as many items as the array contains instead of throwing an exception.</remarks>
647+
///
648+
///<param name="count">The number of items to take.</param>
649+
///<param name="array">The input array.</param>
650+
///
651+
///<returns>The result array.</returns>
652+
///
653+
///<exception cref="System.ArgumentNullException">Thrown when the input array is null.</exception>
654+
///<exception cref="System.ArgumentException">Thrown when the input array is empty.</exception>
655+
///<exception cref="System.InvalidOperationException">Thrown when count exceeds the number of elements
656+
///in the list.</exception>
657+
[<CompiledName("Take")>]
658+
val take:count:int->array:'T[]-> 'T[]
659+
643660
///<summary>Builds a list from the given array.</summary>
644661
///<param name="array">The input array.</param>
645662
///<returns>The list of array elements.</returns>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ namespace Microsoft.FSharp.Collections
8787
[<CompiledName("Choose")>]
8888
letchoose f xs= chooseAllAcc f xs[]
8989

90+
[<CompiledName("Take")>]
91+
lettake count(list:'T list)= Microsoft.FSharp.Primitives.Basics.List.take count list
92+
9093
[<CompiledName("IterateIndexed")>]
9194
letiteri f list= Microsoft.FSharp.Primitives.Basics.List.iteri f list
9295

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,22 @@ namespace Microsoft.FSharp.Collections
571571
[<CompiledName("Tail")>]
572572
val tail:list:'T list-> 'T list
573573

574+
///<summary>Returns the first N elements of the list.</summary>
575+
///<remarks>Throws <c>InvalidOperationException</c>
576+
///if the count exceeds the number of elements in the list. <c>List.truncate</c>
577+
///returns as many items as the list contains instead of throwing an exception.</remarks>
578+
///
579+
///<param name="count">The number of items to take.</param>
580+
///<param name="list">The input list.</param>
581+
///
582+
///<returns>The result list.</returns>
583+
///
584+
///<exception cref="System.ArgumentException">Thrown when the input list is empty.</exception>
585+
///<exception cref="System.InvalidOperationException">Thrown when count exceeds the number of elements
586+
///in the list.</exception>
587+
[<CompiledName("Take")>]
588+
val take:count:int->list:'T list-> 'T list
589+
574590
///<summary>Builds an array from the given list.</summary>
575591
///<param name="list">The input list.</param>
576592
///<returns>The array containing the elements of the list.</returns>

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,24 @@ module internal List =
266266
initToFreshConsTail res1 count f
267267
res
268268

269-
269+
let rectakeFreshConsTail cons n l=
270+
if n=0then setFreshConsTail cons[]else
271+
match lwith
272+
|[]-> raise<| System.InvalidOperationException(SR.GetString(SR.notEnoughElements))
273+
| x::xs->
274+
letcons2= freshConsNoTail x
275+
setFreshConsTail cons cons2
276+
takeFreshConsTail cons2(n-1) xs
277+
278+
lettake n l=
279+
if n<0then invalidArg"count" InputMustBeNonNegativeString
280+
if n=0then[]else
281+
match lwith
282+
|[]-> raise<| System.InvalidOperationException(SR.GetString(SR.notEnoughElements))
283+
| x::xs->
284+
letcons= freshConsNoTail x
285+
takeFreshConsTail cons(n-1) xs
286+
cons
270287

271288
// optimized mutation-based implementation. This code is only valid in fslib, where mutation of private
272289
// tail cons cells is permitted in carefully written library code.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module internal List =
2727
val zip: 'T1 list-> 'T2 list->('T1* 'T2) list
2828
val zip3: 'T1 list-> 'T2 list-> 'T3 list->('T1* 'T2* 'T3) list
2929
val ofArray: 'T[]-> 'T list
30+
val take: int-> 'T list-> 'T list
3031
val toArray: 'T list-> 'T[]
3132
val sortWith:('T-> 'T-> int)-> 'T list-> 'T list
3233

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp