Or you can create a sequence instead of creating an array, for example:
// nItems, given n and an item, returns a sequence of item repeated n times let rec nItems n item = seq { match n with | n when n > 0 -> yield item; yield! nItems (n - 1) item | _ -> () } type Fnord = { foo: int } printfn "%A" (nItems 99999999 {foo = 3}) // seq [{foo = 3;}; {foo = 3;}; {foo = 3;}; {foo = 3;}; ...] printfn "%A" (nItems 3 3 |> Seq.toArray) [|3; 3; 3|]
The good thing about consistency, not an array, is that it creates elements as needed, not all at once. And it's just to go back and forth between sequences and arrays if you need to.
James moore
source share