Scott Nimrod
Scott Nimrod

Reputation: 11595

Is there a shorthand method for constructing a 2D array?

Is there a shorthand method for constructing a 2D array?

I really don't want to do the following:

let board = array2D [| 
                        [| 0;0 |]
                        [| 0;1 |]
                        [| 0;2 |]
                        [| 0;3 |]
                        [| 0;4 |]
                        [| 0;5 |]
                        [| 0;6 |]
                        [| 0;7 |]
                        [| 1;0 |]
                        [| 1;1 |]
                        [| 1;2 |]
                        ...
                        [| 7;7 |]
                    |]

I thought I could do something like this:

let board = array2D [| 
                        [| 0;0 |]..[|7;7|]
                    |]

Or this:

let board = array2D [| 
                        [| 0.., 0..7 |]
                    |]

Any guidance?

Upvotes: 2

Views: 157

Answers (2)

Jason Down
Jason Down

Reputation: 22161

Ringil's answer is more correct (for what you asked). However, if you are looking for another way with a slightly different array shape, you can do the following:

You can use Array2D.init:

> let board = Array2D.init 8 8 (fun x y -> (x,y));;

val board : (int * int) [,] =
  [[(0, 0); (0, 1); (0, 2); (0, 3); (0, 4); (0, 5); (0, 6); (0, 7)]
   [(1, 0); (1, 1); (1, 2); (1, 3); (1, 4); (1, 5); (1, 6); (1, 7)]
   [(2, 0); (2, 1); (2, 2); (2, 3); (2, 4); (2, 5); (2, 6); (2, 7)]
   [(3, 0); (3, 1); (3, 2); (3, 3); (3, 4); (3, 5); (3, 6); (3, 7)]
   [(4, 0); (4, 1); (4, 2); (4, 3); (4, 4); (4, 5); (4, 6); (4, 7)]
   [(5, 0); (5, 1); (5, 2); (5, 3); (5, 4); (5, 5); (5, 6); (5, 7)]
   [(6, 0); (6, 1); (6, 2); (6, 3); (6, 4); (6, 5); (6, 6); (6, 7)]
   [(7, 0); (7, 1); (7, 2); (7, 3); (7, 4); (7, 5); (7, 6); (7, 7)]]

I'll leave this here in case anyone finds it useful, as I think this data shape makes sense in many cases.

Upvotes: 6

Ringil
Ringil

Reputation: 6527

Maybe what you need is a comprehension:

array2D [for i in 0..7 do for j in 0..7 do yield [|i;j|] ]

This returns what seems to be what you want in the order you want:

val it : int [,] = [[0; 0]
                    [0; 1]
                    [0; 2]
                    [0; 3]
                    [0; 4]
                    [0; 5]
                    [0; 6]
                    [0; 7]
                    [1; 0]
                    [1; 1]
                    [1; 2]
                    [1; 3]
                    [1; 4]
                    [1; 5]
                    [1; 6]
                    [1; 7]
                    [2; 0]
                    [2; 1]
                    [2; 2]
                    [2; 3]
                    [2; 4]
                    [2; 5]
                    [2; 6]
                    [2; 7]
                    [3; 0]
                    [3; 1]
                    [3; 2]
                    [3; 3]
                    [3; 4]
                    [3; 5]
                    [3; 6]
                    [3; 7]
                    [4; 0]
                    [4; 1]
                    [4; 2]
                    [4; 3]
                    [4; 4]
                    [4; 5]
                    [4; 6]
                    [4; 7]
                    [5; 0]
                    [5; 1]
                    [5; 2]
                    [5; 3]
                    [5; 4]
                    [5; 5]
                    [5; 6]
                    [5; 7]
                    [6; 0]
                    [6; 1]
                    [6; 2]
                    [6; 3]
                    [6; 4]
                    [6; 5]
                    [6; 6]
                    [6; 7]
                    [7; 0]
                    [7; 1]
                    [7; 2]
                    [7; 3]
                    [7; 4]
                    [7; 5]
                    [7; 6]
                    [7; 7]]

Upvotes: 6

Related Questions