Skip to content

Commit d9cf4ba

Browse files
committed
Merge pull request #264 from gwinsky/patch-1
optimized "init" and "create"
2 parents 4e071f2 + 4338d34 commit d9cf4ba

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

src/FSharpx.Core/Collections/ResizeArray.fs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@ module ResizeArray =
1414
let length (arr: ResizeArray<'T>) = arr.Count
1515
let get (arr: ResizeArray<'T>) (n: int) = arr.[n]
1616
let set (arr: ResizeArray<'T>) (n: int) (x:'T) = arr.[n] <- x
17-
let create (n: int) x = new ResizeArray<_> (seq { for _ in 1 .. n -> x })
18-
let init (n: int) (f: int -> 'T) = new ResizeArray<_> (seq { for i in 0 .. n-1 -> f i })
17+
let create (n: int) x =
18+
let arr = ResizeArray<_>(n)
19+
for i=0 to n-1 do arr.Add x
20+
arr
21+
22+
let init (n: int) (f: int -> 'T) =
23+
let arr = ResizeArray<_>(n)
24+
for i=0 to n-1 do arr.Add (f i)
25+
arr
1926

2027
let blit (arr1: ResizeArray<'T>) start1 (arr2: ResizeArray<'T>) start2 len =
2128
if start1 < 0 then invalidArg "start1" "index must be positive"

0 commit comments

Comments
 (0)