Hi,

I suggest you to write a to_seq conversion function:

1
2
3
let array2D_to_seq arr =
  seq {for i in 0..Array2D.length1 arr - 1 do
       for j in 0..Array2D.length2 arr - 1 do yield arr.[i,j]}

This way, you'll be able to use Seq.max, Seq.fold, Seq.sum, etc.

Laurent.

By on 9/2/2009 2:35 PM ()

Thanks. That's much better than rewriting then all.

By on 9/3/2009 12:20 PM ()

I like to (ab?)use Seq.cast to flatten an Array2D:

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
> let flatten (A:'a[,]) = A |> Seq.cast<'a>;;


val flatten : 'a [,] -> seq<'a>


> let arr = Array2D.init 3 2 (fun i j -> (i,j));;


val arr : (int * int) [,] = [[(0, 0); (0, 1)]
                             [(1, 0); (1, 1)]
                             [(2, 0); (2, 1)]]


> flatten arr;;


val it : seq<int * int> = seq [(0, 0); (0, 1); (1, 0); (1, 1); ...]
By on 9/3/2009 1:40 PM ()

Is Seq.cast a reliable way to flatten the 2d array?

By on 9/4/2009 7:38 AM ()

A quote from the C# spec:

The order in which foreach traverses the elements of an array, is as follows:
For single-dimensional arrays elements are traversed in increasing index order, starting with index 0 and ending with index Length – 1.
For multi-dimensional arrays, elements are traversed such that the indices of the rightmost dimension are increased first, then the next left dimension, and so on to the left.

I think this means that casting an Array2D to a seq should behave as expected.

By on 9/4/2009 10:38 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