You're using an array of rows to represent your 2D data. The compiler has no way to know whether you have a rectangular array or a jagged array where rows might have different lengths. That's why there's no slice operation to extract a column from a 'a[][] type.

A.[0..] means 'every row of A', so A.[0..].[0] means the first row of every row, which is the first row.
If you represent your data like that, you'll have to explicitly code up a column extractor as you have done.

If you want to be able to make 2D slices, I'd use the Array2D data type instead, which means that the compiler knows the 2D is a rectangular array.

Example:

1
2
3
4
let A = array2D [[1;2;3];[4;5;6];[7;8;9]] //A has type int[,] not int[][]

let getRow r (A:'a[,]) = A.[r..r,*] |> Seq.cast<'a> |> Seq.toArray
let getCol c (A:'a[,]) = A.[*,c..c] |> Seq.cast<'a> |> Seq.toArray

in FSI:

1
2
3
4
> getRow 2 A;;
val it : int array = [|7; 8; 9|]
> getCol 1 A;;
val it : int array = [|2; 5; 8|]

You may be wondering about Seq.cast. I had to do that because any slice from an Array2D is itself an Array2D object, and therefore still 2-dimensional. I use Seq.cast to turn the 'pseudo 1D slice' into a sequence which I then convert into a 1-D data structure: an array (I could have left it as an enumerable or turned it into a list as well).

By on 5/9/2010 5:28 AM ()

Excellent answer, thank you!!

By on 5/10/2010 8:41 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper