Skip to content

Commit

Permalink
Implement Array.replicate, Seq.replicate
Browse files Browse the repository at this point in the history
Commits:
    Create missing test for List.replicate

    Implement Array.replicate

    Implement Seq.replicate

    Make the docs for replicate a bit clearer

    Use only one test method for replicate

    Test for negative arguments for replicate

    Variable i  not used in Seq.replicate

    Use mutable cons cell for List.replicate

    Use System.Linq.Enumerable.Repeat in Seq.replicate if we are on .NET 4.0 or higher

    Adding surface area for replicate

    Small tweaks

    Rolling back change to List.replicate
  • Loading branch information
forki authored and latkin committed Oct 12, 2014
1 parent b10392d commit 173d833
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,16 @@ type ArrayModule() =

CheckThrowsArgumentNullException (fun () -> Array.splitAt 0 null |> ignore)
CheckThrowsArgumentNullException (fun () -> Array.splitAt 1 null |> ignore)

[<Test>]
member this.replicate() =
// replicate should create multiple copies of the given value
Assert.AreEqual([||],Array.replicate 0 null)
Assert.AreEqual([||],Array.replicate 0 1)
Assert.AreEqual([|null|],Array.replicate 1 null)
Assert.AreEqual([|"1";"1"|],Array.replicate 2 "1")

CheckThrowsArgumentException (fun () -> Array.replicate -1 null |> ignore)

[<Test>]
member this.Blit() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,16 @@ type ListModule() =

()

[<Test>]
member this.replicate() =
// replicate should create multiple copies of the given value
Assert.AreEqual([],List.replicate 0 null)
Assert.AreEqual([],List.replicate 0 1)
Assert.AreEqual([null],List.replicate 1 null)
Assert.AreEqual(["1";"1"],List.replicate 2 "1")

CheckThrowsArgumentException (fun () -> List.replicate -1 null |> ignore)

[<Test>]
member this.FindIndex() =
// integer List
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ type SeqModule() =
VerifySeqsEqual expectedResultNull appendNullSeq

()

[<Test>]
member this.replicate() =
// replicate should create multiple copies of the given value
Assert.IsTrue(Seq.isEmpty <| Seq.replicate 0 null)
Assert.IsTrue(Seq.isEmpty <| Seq.replicate 0 1)
Assert.AreEqual(null, Seq.head <| Seq.replicate 1 null)
Assert.AreEqual(["1";"1"],Seq.replicate 2 "1" |> Seq.toList)

CheckThrowsArgumentException (fun () -> Seq.replicate -1 null |> ignore)


[<Test>]
Expand Down
2 changes: 2 additions & 0 deletions src/fsharp/FSharp.Core.Unittests/SurfaceArea.4.0.fs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ Microsoft.FSharp.Collections.ArrayModule: T[] Initialize[T](Int32, Microsoft.FSh
Microsoft.FSharp.Collections.ArrayModule: T[] OfList[T](Microsoft.FSharp.Collections.FSharpList`1[T])
Microsoft.FSharp.Collections.ArrayModule: T[] OfSeq[T](System.Collections.Generic.IEnumerable`1[T])
Microsoft.FSharp.Collections.ArrayModule: T[] Permute[T](Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,System.Int32], T[])
Microsoft.FSharp.Collections.ArrayModule: T[] Replicate[T](Int32, T)
Microsoft.FSharp.Collections.ArrayModule: T[] Reverse[T](T[])
Microsoft.FSharp.Collections.ArrayModule: T[] Singleton[T](T)
Microsoft.FSharp.Collections.ArrayModule: T[] SortBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], T[])
Expand Down Expand Up @@ -419,6 +420,7 @@ Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] OfArray[T](T[])
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] OfList[T](Microsoft.FSharp.Collections.FSharpList`1[T])
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] ReadOnly[T](System.Collections.Generic.IEnumerable`1[T])
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Replicate[T](Int32, T)
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Singleton[T](T)
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] SkipWhile[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], System.Collections.Generic.IEnumerable`1[T])
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Skip[T](Int32, System.Collections.Generic.IEnumerable`1[T])
Expand Down
2 changes: 2 additions & 0 deletions src/fsharp/FSharp.Core.Unittests/SurfaceArea.Portable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ Microsoft.FSharp.Collections.ArrayModule: T[] Initialize[T](Int32, Microsoft.FSh
Microsoft.FSharp.Collections.ArrayModule: T[] OfList[T](Microsoft.FSharp.Collections.FSharpList`1[T])
Microsoft.FSharp.Collections.ArrayModule: T[] OfSeq[T](System.Collections.Generic.IEnumerable`1[T])
Microsoft.FSharp.Collections.ArrayModule: T[] Permute[T](Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,System.Int32], T[])
Microsoft.FSharp.Collections.ArrayModule: T[] Replicate[T](Int32, T)
Microsoft.FSharp.Collections.ArrayModule: T[] Reverse[T](T[])
Microsoft.FSharp.Collections.ArrayModule: T[] Singleton[T](T)
Microsoft.FSharp.Collections.ArrayModule: T[] SortBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], T[])
Expand Down Expand Up @@ -413,6 +414,7 @@ Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] OfArray[T](T[])
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] OfList[T](Microsoft.FSharp.Collections.FSharpList`1[T])
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] ReadOnly[T](System.Collections.Generic.IEnumerable`1[T])
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Replicate[T](Int32, T)
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Singleton[T](T)
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] SkipWhile[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], System.Collections.Generic.IEnumerable`1[T])
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Skip[T](Int32, System.Collections.Generic.IEnumerable`1[T])
Expand Down
8 changes: 8 additions & 0 deletions src/fsharp/FSharp.Core/array.fs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ namespace Microsoft.FSharp.Collections
match arrays with
| :? ('T[][]) as ts -> ts |> concatArrays // avoid a clone, since we only read the array
| _ -> arrays |> Seq.toArray |> concatArrays

[<CompiledName("Replicate")>]
let replicate count x =
if count < 0 then invalidArg "count" (SR.GetString(SR.inputMustBeNonNegative))
let arr = (Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked count : 'T array)
for i = 0 to count - 1 do
arr.[i] <- x
arr

[<CompiledName("Collect")>]
let collect (f : 'T -> 'U[]) (array : 'T[]) : 'U[]=
Expand Down
7 changes: 7 additions & 0 deletions src/fsharp/FSharp.Core/array.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,13 @@ namespace Microsoft.FSharp.Collections
[<CompiledName("ReduceBack")>]
val reduceBack: reduction:('T -> 'T -> 'T) -> array:'T[] -> 'T

/// <summary>Creates an array by replicating the given initial value.</summary>
/// <param name="count">The number of elements to replicate.</param>
/// <param name="initial">The value to replicate</param>
/// <returns>The generated array.</returns>
[<CompiledName("Replicate")>]
val replicate: count:int -> initial:'T -> 'T[]

/// <summary>Returns a new array with the elements in reverse order.</summary>
/// <param name="array">The input array.</param>
/// <returns>The reversed array.</returns>
Expand Down
2 changes: 1 addition & 1 deletion src/fsharp/FSharp.Core/list.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ namespace Microsoft.FSharp.Collections
[<CompiledName("ReduceBack")>]
val reduceBack: reduction:('T -> 'T -> 'T) -> list:'T list -> 'T

/// <summary>Creates a list by calling the given generator on each index.</summary>
/// <summary>Creates a list by replicating the given initial value.</summary>
/// <param name="count">The number of elements to replicate.</param>
/// <param name="initial">The value to replicate</param>
/// <returns>The generated list.</returns>
Expand Down
11 changes: 10 additions & 1 deletion src/fsharp/FSharp.Core/seq.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,16 @@ namespace Microsoft.FSharp.Collections

let fromGenerator f = mkSeq(fun () -> Generator.EnumerateFromGenerator (f()))
let toGenerator (ie : seq<_>) = Generator.GenerateFromEnumerator (ie.GetEnumerator())


[<CompiledName("Replicate")>]
let replicate count x =
#if FX_ATLEAST_40
System.Linq.Enumerable.Repeat(x,count)
#else
if count < 0 then invalidArg "count" (SR.GetString(SR.inputMustBeNonNegative))
seq { for _ in 1 .. count -> x }
#endif


[<CompiledName("Append")>]
let append (source1: seq<'T>) (source2: seq<'T>) =
Expand Down
7 changes: 7 additions & 0 deletions src/fsharp/FSharp.Core/seq.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,13 @@ namespace Microsoft.FSharp.Collections
[<CompiledName("Reduce")>]
val reduce: reduction:('T -> 'T -> 'T) -> source:seq<'T> -> 'T

/// <summary>Creates a sequence by replicating the given initial value.</summary>
/// <param name="count">The number of elements to replicate.</param>
/// <param name="initial">The value to replicate</param>
/// <returns>The generated sequence.</returns>
[<CompiledName("Replicate")>]
val replicate: count:int -> initial:'T -> seq<'T>

/// <summary>Like fold, but computes on-demand and returns the sequence of intermediary and final results.</summary>
///
/// <param name="folder">A function that updates the state with each element from the sequence.</param>
Expand Down

0 comments on commit 173d833

Please sign in to comment.