Skip to content

Commit

Permalink
Implementing init for PersistentVector
Browse files Browse the repository at this point in the history
  • Loading branch information
forki committed May 29, 2012
1 parent 5f2e9cd commit da0a771
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/FSharpx.Core/DataStructures/Vector.fs
Expand Up @@ -449,4 +449,10 @@ let inline map (f:'a -> 'b) (vector:'a vector) : 'b vector =
let mutable ret = TransientVector()
for item in vector do
ret <- ret.conj(f item)
ret.persistent() :> 'b vector
ret.persistent() :> 'b vector

let inline init count (f: int -> 'a) : 'a vector =
let mutable ret = TransientVector()
for i in 0..(count-1) do
ret <- ret.conj(f i)
ret.persistent() :> 'a vector
10 changes: 9 additions & 1 deletion tests/FSharpx.DataStructures.Tests/PersistentVectorTest.fs
Expand Up @@ -138,4 +138,12 @@ let ``vector should allow map``() =

let vector2 = map (fun x -> x * 2) (!vector)

vector2 |> Seq.toList |> should equal [for i in 1..30000 -> i * 2]
vector2 |> Seq.toList |> should equal [for i in 1..30000 -> i * 2]

[<Test>]
let ``vector should allow init``() =
let vector = init 5 (fun x -> x * 2)
let s = Seq.init 5 (fun x -> x * 2)

s |> Seq.toList |> should equal [0;2;4;6;8]
vector |> Seq.toList |> should equal [0;2;4;6;8]

0 comments on commit da0a771

Please sign in to comment.