Skip to content

Commit 2d72eea

Browse files
bogdangaliceanulatkin
authored andcommitted
Implement Array.tail, Seq.tail
commit f33a239bbc68783d9518c5f2b4f5ea8f1d9f6517 Author: latkin <latkin@microsoft.com> Date: Tue Oct 21 16:59:38 2014 -0700 Changing resource string for empty array commit 58c74dc63ebf3cb382ef984f16d29c9e99c3292d Author: latkin <latkin@microsoft.com> Date: Tue Oct 21 16:59:15 2014 -0700 Expanding tests commit 030b11a8f7217cc053e6149934482e7a79cde548 Author: latkin <latkin@microsoft.com> Date: Tue Oct 21 16:51:10 2014 -0700 Small update to docs commit 44ae13aba8cf45fa194fd81ac65704bc4493ff0d Merge: a0f6460 d374804 Author: latkin <latkin@microsoft.com> Date: Tue Oct 21 16:43:31 2014 -0700 Merge branch 'tail' of https://git01.codeplex.com/forks/bogdangaliceanu/fsharp 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.fsi src/fsharp/FSharp.Core/seq.fs commit d3748048c038dfb6409e2df5eab0b45b08c407c3 Author: bogdangaliceanu <bogdan.galiceanu@hotmail.com> Date: Sat Sep 6 20:24:26 2014 +0300 implementation for Array.tail and Seq.tail
1 parent a0f6460 commit 2d72eea

8 files changed

Lines changed: 79 additions & 0 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,25 @@ type ArrayModule2() =
707707
CheckThrowsArgumentNullException (fun () -> Array.sumBy float32 nullArr |> ignore)
708708
()
709709

710+
[<Test>]
711+
member this.Tl() =
712+
// integer array
713+
let resultInt = Array.tail [|1..10|]
714+
Assert.AreEqual([|2..10|], resultInt)
715+
716+
// string array
717+
let resultStr = Array.tail [| "a"; "b"; "c"; "d" |]
718+
Assert.AreEqual([| "b"; "c" ; "d" |], resultStr)
719+
720+
// 1-element array
721+
let resultStr2 = Array.tail [| "a" |]
722+
Assert.AreEqual([| |], resultStr2)
723+
724+
CheckThrowsArgumentException(fun () -> Array.tail [||] |> ignore)
725+
726+
CheckThrowsArgumentNullException(fun () -> Array.tail null |> ignore)
727+
()
728+
710729
[<Test>]
711730
member this.To_List() =
712731
// integer array

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,24 @@ type SeqModule2() =
6262
CheckThrowsArgumentNullException (fun () ->Seq.head nullSeq)
6363
()
6464

65+
[<Test>]
66+
member this.Tl() =
67+
// integer seq
68+
let resultInt = Seq.tail <| seq { 1..10 }
69+
Assert.AreEqual(seq { 2..10 }, resultInt)
70+
71+
// string seq
72+
let resultStr = Seq.tail <| seq { yield "a"; yield "b"; yield "c"; yield "d" }
73+
Assert.AreEqual(seq { yield "b"; yield "c" ; yield "d" }, resultStr)
6574

75+
// 1-element seq
76+
let resultStr2 = Seq.tail <| seq { yield "a" }
77+
Assert.AreEqual(Seq.empty, resultStr2)
78+
79+
CheckThrowsArgumentNullException(fun () -> Seq.tail null |> ignore)
80+
CheckThrowsArgumentException(fun () -> Seq.tail Seq.empty |> Seq.iter (fun _ -> failwith "Should not be reached"))
81+
()
82+
6683
[<Test>]
6784
member this.Last() =
6885

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ Microsoft.FSharp.Collections.ArrayModule: T[] Skip[T](Int32, T[])
173173
Microsoft.FSharp.Collections.ArrayModule: T[] SortBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], T[])
174174
Microsoft.FSharp.Collections.ArrayModule: T[] SortWith[T](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[T,System.Int32]], T[])
175175
Microsoft.FSharp.Collections.ArrayModule: T[] Sort[T](T[])
176+
Microsoft.FSharp.Collections.ArrayModule: T[] Tail[T](T[])
176177
Microsoft.FSharp.Collections.ArrayModule: T[] TakeWhile[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
177178
Microsoft.FSharp.Collections.ArrayModule: T[] Take[T](Int32, T[])
178179
Microsoft.FSharp.Collections.ArrayModule: T[] Where[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
@@ -443,6 +444,7 @@ Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1
443444
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Skip[T](Int32, System.Collections.Generic.IEnumerable`1[T])
444445
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] SortBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], System.Collections.Generic.IEnumerable`1[T])
445446
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Sort[T](System.Collections.Generic.IEnumerable`1[T])
447+
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Tail[T](System.Collections.Generic.IEnumerable`1[T])
446448
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] TakeWhile[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], System.Collections.Generic.IEnumerable`1[T])
447449
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Take[T](Int32, System.Collections.Generic.IEnumerable`1[T])
448450
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Truncate[T](Int32, System.Collections.Generic.IEnumerable`1[T])

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ Microsoft.FSharp.Collections.ArrayModule: T[] SortBy[T,TKey](Microsoft.FSharp.Co
168168
Microsoft.FSharp.Collections.ArrayModule: T[] SortWith[T](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[T,System.Int32]], T[])
169169
Microsoft.FSharp.Collections.ArrayModule: T[] Sort[T](T[])
170170
Microsoft.FSharp.Collections.ArrayModule: T[] TakeWhile[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
171+
Microsoft.FSharp.Collections.ArrayModule: T[] Tail[T](T[])
171172
Microsoft.FSharp.Collections.ArrayModule: T[] Take[T](Int32, T[])
172173
Microsoft.FSharp.Collections.ArrayModule: T[] Where[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
173174
Microsoft.FSharp.Collections.ArrayModule: T[] ZeroCreate[T](Int32)
@@ -437,6 +438,7 @@ Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1
437438
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Skip[T](Int32, System.Collections.Generic.IEnumerable`1[T])
438439
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] SortBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], System.Collections.Generic.IEnumerable`1[T])
439440
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Sort[T](System.Collections.Generic.IEnumerable`1[T])
441+
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Tail[T](System.Collections.Generic.IEnumerable`1[T])
440442
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] TakeWhile[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], System.Collections.Generic.IEnumerable`1[T])
441443
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Take[T](Int32, System.Collections.Generic.IEnumerable`1[T])
442444
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Truncate[T](Int32, System.Collections.Generic.IEnumerable`1[T])

src/fsharp/FSharp.Core/array.fs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ namespace Microsoft.FSharp.Collections
6363
checkNonNull "array" array
6464
(array.Length = 0)
6565

66+
[<CompiledName("Tail")>]
67+
let tail (array : 'T[]) =
68+
checkNonNull "array" array
69+
if array.Length = 0 then invalidArg "array" (SR.GetString(SR.notEnoughElements))
70+
let len = array.Length - 1
71+
let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked<'T> len
72+
Array.Copy(array, 1, result, 0, len)
73+
result
74+
6675
[<CompiledName("Empty")>]
6776
let empty<'T> = ([| |] : 'T [])
6877

src/fsharp/FSharp.Core/array.fsi

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,15 @@ namespace Microsoft.FSharp.Collections
769769
[<CompiledName("TakeWhile")>]
770770
val takeWhile: predicate:('T -> bool) -> array:'T[] -> 'T[]
771771

772+
/// <summary>Returns a new array containing the elements of the original except the first element.</summary>
773+
///
774+
/// <param name="array">The input array.</param>
775+
/// <exception cref="System.ArgumentException">Thrown when the array is empty.</exception>
776+
/// <exception cref="System.ArgumentNullException">Thrown when the input array is null.</exception>
777+
/// <returns>A new array containing the elements of the original except the first element.</returns>
778+
[<CompiledName("Tail")>]
779+
val tail: array:'T[] -> 'T[]
780+
772781
/// <summary>Builds a list from the given array.</summary>
773782
/// <param name="array">The input array.</param>
774783
/// <returns>The list of array elements.</returns>

src/fsharp/FSharp.Core/seq.fs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,6 +1680,15 @@ namespace Microsoft.FSharp.Collections
16801680
if (e.MoveNext()) then Some e.Current
16811681
else None
16821682

1683+
[<CompiledName("Tail")>]
1684+
let tail (source: seq<'T>) =
1685+
checkNonNull "source" source
1686+
seq { use e = source.GetEnumerator()
1687+
if not (e.MoveNext()) then
1688+
invalidArg "source" (SR.GetString(SR.notEnoughElements))
1689+
while e.MoveNext() do
1690+
yield e.Current }
1691+
16831692
[<CompiledName("Last")>]
16841693
let last (source : seq<_>) =
16851694
checkNonNull "source" source

src/fsharp/FSharp.Core/seq.fsi

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,18 @@ namespace Microsoft.FSharp.Collections
943943
when ^U : (static member ( + ) : ^U * ^U -> ^U)
944944
and ^U : (static member Zero : ^U)
945945

946+
/// <summary>Returns a sequence that skips 1 element of the underlying sequence and then yields the
947+
/// remaining elements of the sequence.</summary>
948+
///
949+
/// <param name="source">The input sequence.</param>
950+
///
951+
/// <returns>The result sequence.</returns>
952+
///
953+
/// <exception cref="System.ArgumentNullException">Thrown when the input sequence is null.</exception>
954+
/// <exception cref="System.InvalidOperationException">Thrown when the input sequence is empty.</exception>
955+
[<CompiledName("Tail")>]
956+
val tail: source:seq<'T> -> seq<'T>
957+
946958
/// <summary>Returns the first N elements of the sequence.</summary>
947959
/// <remarks>Throws <c>InvalidOperationException</c>
948960
/// if the count exceeds the number of elements in the sequence. <c>Seq.truncate</c>

0 commit comments

Comments
 (0)