I just wonder is there a better approach to convert an array of tuples into a two-dimensional array?
let list = [|("a", "1"); ("b", "2"); ("c", "3")|];;
let arr = Array2D.init (Array.length list) 2 (fun i j -> if j <> 0 then (fst list.[i]) else (snd list.[i]));;
A more concise way is to use array2D:
[|("a", "1"); ("b", "2"); ("c", "3")|]
|> Seq.map (fun (x, y) -> [|x; y|])
|> array2D
But is there any reason why you don't use an array of arrays from beginning for easy initialization e.g.
let arr =
[|[|"a"; "1"|];
[|"b"; "2"|];
[|"c"; "3"|]|]
|> array2D
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With