Skip to content

Commit 627c016

Browse files
PatrickMcDonaldlatkin
authored andcommitted
Implement Array.skipWhile, List.skipWhile
commit 6a07c4f8d153e7d1c4fadcd22734ec0ee78c4308 Author: latkin <latkin@microsoft.com> Date: Tue Oct 21 12:17:57 2014 -0700 Minor doc typo commit edeec0a2100384abb541777c439ffb89ad0672f5 Merge: 12c2231 b044afb Author: latkin <latkin@microsoft.com> Date: Tue Oct 21 11:29:05 2014 -0700 Merge branch 'skipWhile' of https://git01.codeplex.com/forks/patrickmcdonald/visualfsharp into PR Conflicts: src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/ArrayModule2.fs src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/ListModule2.fs 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/array.fsi src/fsharp/FSharp.Core/list.fs src/fsharp/FSharp.Core/list.fsi commit b044afb8a82d6bea6062cd3d180566971eaa7cb5 Author: Patrick McDonald <paddymcdonald@gmail.com> Date: Tue Oct 21 10:36:31 2014 +0100 Revert array slicing in Array.skipWhile commit 8d4b4aea0c7055419ed74653cabafdd1210d2692 Author: Patrick McDonald <paddymcdonald@gmail.com> Date: Mon Sep 8 11:52:19 2014 +0100 Use array slicing for Array.skipWhile commit 5e5c25ac90577a7f50a31dc537af8669250a33a2 Author: Patrick McDonald <paddymcdonald@gmail.com> Date: Thu Aug 14 00:10:40 2014 +0100 Implement List.skipWhile and Array.skipWhile
1 parent c0037fd commit 627c016

8 files changed

Lines changed: 96 additions & 0 deletions

File tree

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,35 @@ type ArrayModule2() =
471471
CheckThrowsArgumentException (fun () -> Array.skip 1 [||] |> ignore)
472472
CheckThrowsArgumentException (fun () -> Array.skip 4 [|1; 2; 3|] |> ignore)
473473

474+
[<Test>]
475+
member this.SkipWhile() =
476+
// integer array
477+
let funcInt x = (x < 4)
478+
let intArr = [|1..10|]
479+
let resultInt = Array.skipWhile funcInt intArr
480+
if resultInt <> [|4..10|] then Assert.Fail()
481+
482+
// string array
483+
let funcStr (s:string) = s.Length < 8
484+
let strArr = [| "Lists"; "are"; "commonly" ; "list" |]
485+
let resultStr = Array.skipWhile funcStr strArr
486+
if resultStr <> [| "commonly" ; "list" |] then Assert.Fail()
487+
488+
// empty array
489+
let resultEmpt = Array.skipWhile (fun _ -> failwith "unexpected error") [| |]
490+
if resultEmpt <> [| |] then Assert.Fail()
491+
492+
// null array
493+
CheckThrowsArgumentNullException (fun () -> Array.skipWhile (fun _ -> failwith "unexpected error") null |> ignore)
494+
495+
// skip all
496+
let resultAll = Array.skipWhile (fun _ -> true) intArr
497+
if resultAll <> [| |] then Assert.Fail()
498+
499+
// skip none
500+
let resultNone = Array.skipWhile (fun _ -> false) intArr
501+
if resultNone <> intArr then Assert.Fail()
502+
474503
()
475504

476505
[<Test>]

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,32 @@ type ListModule02() =
450450
CheckThrowsArgumentException(fun () -> List.skip 1 [] |> ignore)
451451
CheckThrowsArgumentException(fun () -> List.skip 4 [1; 2; 3] |> ignore)
452452

453+
[<Test>]
454+
member this.SkipWhile() =
455+
// integer list
456+
let funcInt x = (x < 4)
457+
let intList = [1..10]
458+
let resultInt = List.skipWhile funcInt intList
459+
if resultInt <> [4..10] then Assert.Fail()
460+
461+
// string list
462+
let funcStr (s:string) = s.Length < 8
463+
let strList = [ "Lists"; "are"; "commonly" ; "list" ]
464+
let resultStr = List.skipWhile funcStr strList
465+
if resultStr <> [ "commonly" ; "list" ] then Assert.Fail()
466+
467+
// empty list
468+
let resultEmpt = List.skipWhile (fun _ -> failwith "unexpected error") []
469+
if resultEmpt <> [] then Assert.Fail()
470+
471+
// skip all
472+
let resultAll = List.skipWhile (fun _ -> true) intList
473+
if resultAll <> [] then Assert.Fail()
474+
475+
// skip none
476+
let resultNone = List.skipWhile (fun _ -> false) intList
477+
if resultNone <> intList then Assert.Fail()
478+
453479
()
454480

455481
[<Test>]

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ Microsoft.FSharp.Collections.ArrayModule: T[] Permute[T](Microsoft.FSharp.Core.F
168168
Microsoft.FSharp.Collections.ArrayModule: T[] Replicate[T](Int32, T)
169169
Microsoft.FSharp.Collections.ArrayModule: T[] Reverse[T](T[])
170170
Microsoft.FSharp.Collections.ArrayModule: T[] Singleton[T](T)
171+
Microsoft.FSharp.Collections.ArrayModule: T[] SkipWhile[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
171172
Microsoft.FSharp.Collections.ArrayModule: T[] Skip[T](Int32, T[])
172173
Microsoft.FSharp.Collections.ArrayModule: T[] SortBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], T[])
173174
Microsoft.FSharp.Collections.ArrayModule: T[] SortWith[T](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[T,System.Int32]], T[])
@@ -313,6 +314,7 @@ Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList
313314
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Replicate[T](Int32, T)
314315
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Reverse[T](Microsoft.FSharp.Collections.FSharpList`1[T])
315316
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Singleton[T](T)
317+
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] SkipWhile[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
316318
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Skip[T](Int32, Microsoft.FSharp.Collections.FSharpList`1[T])
317319
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] SortBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], Microsoft.FSharp.Collections.FSharpList`1[T])
318320
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])

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ Microsoft.FSharp.Collections.ArrayModule: T[] Permute[T](Microsoft.FSharp.Core.F
162162
Microsoft.FSharp.Collections.ArrayModule: T[] Replicate[T](Int32, T)
163163
Microsoft.FSharp.Collections.ArrayModule: T[] Reverse[T](T[])
164164
Microsoft.FSharp.Collections.ArrayModule: T[] Singleton[T](T)
165+
Microsoft.FSharp.Collections.ArrayModule: T[] SkipWhile[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
165166
Microsoft.FSharp.Collections.ArrayModule: T[] Skip[T](Int32, T[])
166167
Microsoft.FSharp.Collections.ArrayModule: T[] SortBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], T[])
167168
Microsoft.FSharp.Collections.ArrayModule: T[] SortWith[T](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[T,System.Int32]], T[])
@@ -307,6 +308,7 @@ Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList
307308
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Replicate[T](Int32, T)
308309
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Reverse[T](Microsoft.FSharp.Collections.FSharpList`1[T])
309310
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Singleton[T](T)
311+
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] SkipWhile[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
310312
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Skip[T](Int32, Microsoft.FSharp.Collections.FSharpList`1[T])
311313
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] SortBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], Microsoft.FSharp.Collections.FSharpList`1[T])
312314
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])

src/fsharp/FSharp.Core/array.fs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,20 @@ namespace Microsoft.FSharp.Collections
473473
Array.Copy(array, count, res, 0, skippedLen)
474474
res
475475

476+
[<CompiledName("SkipWhile")>]
477+
let skipWhile p (array: 'T[]) =
478+
checkNonNull "array" array
479+
let mutable i = 0
480+
let len = array.Length
481+
while i < len && p array.[i] do i <- i + 1
482+
483+
match len - i with
484+
| 0 -> [| |]
485+
| resLen ->
486+
let res : 'T[] = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked resLen
487+
Array.Copy(array, i, res, 0, resLen)
488+
res
489+
476490
[<CompiledName("Zip")>]
477491
let zip (array1: _[]) (array2: _[]) =
478492
checkNonNull "array1" array1

src/fsharp/FSharp.Core/array.fsi

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,15 @@ namespace Microsoft.FSharp.Collections
636636
[<CompiledName("Skip")>]
637637
val skip: count:int -> array:'T[] -> 'T[]
638638

639+
/// <summary>Bypasses elements in an array while the given predicate returns <c>true</c>, and then returns
640+
/// the remaining elements in a new array.</summary>
641+
/// <param name="predicate">A function that evaluates an element of the array to a boolean value.</param>
642+
/// <param name="source">The input array.</param>
643+
/// <returns>The created sub array.</returns>
644+
/// <exception cref="System.ArgumentNullException">Thrown when the input array is null.</exception>
645+
[<CompiledName("SkipWhile")>]
646+
val skipWhile: predicate:('T -> bool) -> array:'T[] -> 'T[]
647+
639648
/// <summary>Builds a new array that contains the given subrange specified by
640649
/// starting index and length.</summary>
641650
/// <param name="array">The input array.</param>

src/fsharp/FSharp.Core/list.fs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,12 @@ namespace Microsoft.FSharp.Collections
410410
| [] -> invalidArg "count" (SR.GetString(SR.outOfRange))
411411
loop count list
412412

413+
[<CompiledName("SkipWhile")>]
414+
let rec skipWhile p xs =
415+
match xs with
416+
| head :: tail when p head -> skipWhile p tail
417+
| _ -> xs
418+
413419
[<CompiledName("SortWith")>]
414420
let sortWith cmp xs = Microsoft.FSharp.Primitives.Basics.List.sortWith cmp xs
415421

src/fsharp/FSharp.Core/list.fsi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,14 @@ namespace Microsoft.FSharp.Collections
550550
[<CompiledName("Skip")>]
551551
val skip: count:int -> list: 'T list -> 'T list
552552

553+
/// <summary>Bypasses elements in a list while the given predicate returns <c>true</c>, and then returns
554+
/// the remaining elements of the list.</summary>
555+
/// <param name="predicate">A function that evaluates an element of the list to a boolean value.</param>
556+
/// <param name="list">The input list.</param>
557+
/// <returns>The result list.</returns>
558+
[<CompiledName("SkipWhile")>]
559+
val skipWhile: predicate:('T -> bool) -> list:'T list -> 'T list
560+
553561
/// <summary>Sorts the given list using the given comparison function.</summary>
554562
///
555563
/// <remarks>This is a stable sort, i.e. the original order of equal elements is preserved.</remarks>

0 commit comments

Comments
 (0)