How do I create and initialize an array in F# based on a given record type? Suppose I want to create an Array of 100 record1 records.
e.g.
type record1 = { value1:string; value2:string } let myArray = Array.init 100 ?
But it appears the Array.init does not allow for this, is there a way to do this?
Edited to add:
Of course I could do something like this:
let myArray = [|for i in 0..99 -> { value1="x"; value2="y" }|]
This should do what you need. Hope it helps.
or using Generics
Or you can create a sequence, instead of creating an array, like this:
The nice thing about the sequence, instead of an array, is that it creates items as you need them, rather than all at once. And it's simple to go back and forth between sequences and arrays if you need to.
You can use also
Array.create
, which creates an array of a given size, with all its elements initialized to a defined value:Give a look to this list of array operations.