diff --git a/src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/ArrayModule2.fs b/src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/ArrayModule2.fs index 719a274f2e4399d3b184274f9f53c6c3c4bfe14c..888f9eb709ea091e4500ba852b63c89b86f74459 100644 --- a/src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/ArrayModule2.fs +++ b/src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/ArrayModule2.fs @@ -939,7 +939,8 @@ type ArrayModule2() = CheckThrowsArgumentNullException(fun() -> Array.truncate 1 null |> ignore) // negative count - CheckThrowsArgumentException(fun() -> Array.truncate -1 [|1..5|] |> ignore) + Assert.AreEqual([| |], Array.truncate -1 [|1..5|]) + Assert.AreEqual([| |], Array.truncate System.Int32.MinValue [|1..5|]) () diff --git a/src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/ListModule2.fs b/src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/ListModule2.fs index eb3db7ca819ed51a515939e740bef06a871d68b1..827ffba4b975372383f7a119241aba7eda6cb71f 100644 --- a/src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/ListModule2.fs +++ b/src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/ListModule2.fs @@ -815,7 +815,8 @@ type ListModule02() = Assert.AreEqual([], List.truncate 1 []) // negative count - CheckThrowsArgumentException(fun() -> List.truncate -1 [1..5] |> ignore) + Assert.AreEqual([], List.truncate -1 [1..5]) + Assert.AreEqual([], List.truncate System.Int32.MinValue [1..5]) () diff --git a/src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/SeqModule2.fs b/src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/SeqModule2.fs index 1693f926be402580b26c065d53c0fa7a45b89b9a..31de923bcc3b22a1201a50213d457c90be47493c 100644 --- a/src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/SeqModule2.fs +++ b/src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/SeqModule2.fs @@ -1533,6 +1533,11 @@ type SeqModule2() = // null Seq CheckThrowsArgumentNullException(fun() -> Seq.truncate 1 null |> ignore) + + // negative count + VerifySeqsEqual Seq.empty <| Seq.truncate -1 (seq [1;2;4;5;7]) + VerifySeqsEqual Seq.empty <| Seq.truncate System.Int32.MinValue (seq [1;2;4;5;7]) + () [] diff --git a/src/fsharp/FSharp.Core/array.fs b/src/fsharp/FSharp.Core/array.fs index 57e023123a7cf26b8ca4838ea5a641e5fb18ac4a..8501a3f19b732b50190de352390b001880eb29a3 100644 --- a/src/fsharp/FSharp.Core/array.fs +++ b/src/fsharp/FSharp.Core/array.fs @@ -1001,8 +1001,7 @@ namespace Microsoft.FSharp.Collections [] let truncate count (array:'T[]) = checkNonNull "array" array - if count < 0 then invalidArg "count" (SR.GetString(SR.inputMustBeNonNegative)) - if count = 0 then empty + if count <= 0 then empty else let len = array.Length let count' = Operators.min count len diff --git a/src/fsharp/FSharp.Core/array.fsi b/src/fsharp/FSharp.Core/array.fsi index a683eaeeba7c9cd1dc5502195c5d392a0980164b..95b8287436e840657a43942c1015212f6f582e98 100644 --- a/src/fsharp/FSharp.Core/array.fsi +++ b/src/fsharp/FSharp.Core/array.fsi @@ -981,7 +981,6 @@ namespace Microsoft.FSharp.Collections /// The input array. /// The result array. /// Thrown when the input array is null. - /// Thrown when the count is negative. [] val truncate: count:int -> array:'T[] -> 'T[] diff --git a/src/fsharp/FSharp.Core/list.fsi b/src/fsharp/FSharp.Core/list.fsi index fe1a14975084bcd0e6800369e32880e165c05436..dfffdce32ea8ef6621d0a7864c2c27da92ce8aa2 100644 --- a/src/fsharp/FSharp.Core/list.fsi +++ b/src/fsharp/FSharp.Core/list.fsi @@ -778,7 +778,6 @@ namespace Microsoft.FSharp.Collections /// The maximum number of items to return. /// The input list. /// The result list. - /// Thrown when the count is negative. [] val truncate: count:int -> list:'T list -> 'T list diff --git a/src/fsharp/FSharp.Core/local.fs b/src/fsharp/FSharp.Core/local.fs index 0b3b0a434bfcdb94de700db49eb6189d75ae7c6a..ae109683b7ee21251ddb338dcd766cdcf73bda3e 100644 --- a/src/fsharp/FSharp.Core/local.fs +++ b/src/fsharp/FSharp.Core/local.fs @@ -441,17 +441,16 @@ module internal List = truncateToFreshConsTail cons2 (count-1) t let truncate count list = - if count < 0 then invalidArg "count" (SR.GetString(SR.inputMustBeNonNegative)) match list with | [] -> list | _ :: ([] as nil) -> if count > 0 then list else nil | h::t -> - if count = 0 then [] + if count <= 0 then [] else let cons = freshConsNoTail h truncateToFreshConsTail cons (count-1) t cons - + let rec unfoldToFreshConsTail cons f s = match f s with | None -> setFreshConsTail cons []