Thread overview
Subarrays of arrays
Dec 30, 2012
Omid
Dec 30, 2012
bearophile
Dec 30, 2012
Omid
Dec 30, 2012
bearophile
Dec 31, 2012
Omid
Dec 31, 2012
Omid
December 30, 2012
Hi, I am new to D and would like to know if there is any built in feature for accessing a subarray (uni or multi dimensional) of an array. For example let

arr = [[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16,17,18]]

I want to obtain a subarray like

assert(arr[][0] == [1,7,13]);
assert(arr[1][] == [7,8,9,10,11,12];
assert(arr[0,2][1] == [2,14]);
assert(arr[0,2][0..3,5] == [[1,2,3,6],[13,14,15,18]]);

Thank you.
December 30, 2012
Omid:

> arr = [[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16,17,18]]
>
> I want to obtain a subarray like
>
> assert(arr[][0] == [1,7,13]);

This gives a range, not an array, so if you want an array, you have to call std.array.array later:

http://dlang.org/phobos/std_range.html#transversal


> assert(arr[1][] == [7,8,9,10,11,12];

Just use array[1]. Or array[1].dup is you want a copy.


> assert(arr[0,2][1] == [2,14]);

Enumerated indexes are not supported, so you need a iota + filter + transversal + array.


> assert(arr[0,2][0..3,5] == [[1,2,3,6],[13,14,15,18]]);

This requires a iota + filter + chain + array, or something like that.

If you don't want to do all this, you have to write your slicing and dicing array struct :-( Someday it will probably be present in Phobos.

Bye,
bearophile
December 30, 2012
On Sunday, 30 December 2012 at 12:51:58 UTC, bearophile wrote:
> Omid:
>
>> arr = [[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16,17,18]]
>>
>> I want to obtain a subarray like
>>
>> assert(arr[][0] == [1,7,13]);
>
> This gives a range, not an array, so if you want an array, you have to call std.array.array later:
>
> http://dlang.org/phobos/std_range.html#transversal
>
>
>> assert(arr[1][] == [7,8,9,10,11,12];
>
> Just use array[1]. Or array[1].dup is you want a copy.
>
>
>> assert(arr[0,2][1] == [2,14]);
>
> Enumerated indexes are not supported, so you need a iota + filter + transversal + array.
>
>
>> assert(arr[0,2][0..3,5] == [[1,2,3,6],[13,14,15,18]]);
>
> This requires a iota + filter + chain + array, or something like that.
>
> If you don't want to do all this, you have to write your slicing and dicing array struct :-( Someday it will probably be present in Phobos.
>
> Bye,
> bearophile

Thanks, though I didn't understand a word you said:). I should learn iota, filter, transversal first.
December 30, 2012
Omid:

> Thanks, though I didn't understand a word you said:).

This code works with the latest beta compiler:


import std.algorithm: filter, equal, map, canFind;
import std.range: transversal, iota;

void main() {
    auto arr = [[ 1, 2, 3, 4, 5, 6],
                [ 7, 8, 9,10,11,12],
                [13,14,15,16,17,18]];

    auto r1 = transversal(arr, 0);
    assert(r1.equal([1, 7, 13]));

    assert(arr[1] == [7,8,9,10,11,12]);

    auto r2 = [0, 2]
              .map!(i => arr[i])()
              .transversal(1);
    assert(r2.equal([2, 14]));

    auto r3 = [0, 2]
              .map!(i => arr[i][0..3] ~ arr[i][5])();
    assert(r3.equal([[1,2,3,6],[13,14,15,18]]));
}


Bye,
bearophile
December 31, 2012
On Sunday, 30 December 2012 at 18:55:50 UTC, bearophile wrote:
> Omid:
>
>
> This code works with the latest beta compiler:
>
>
> import std.algorithm: filter, equal, map, canFind;
> import std.range: transversal, iota;
>
> void main() {
>     auto arr = [[ 1, 2, 3, 4, 5, 6],
>                 [ 7, 8, 9,10,11,12],
>                 [13,14,15,16,17,18]];
>
>     auto r1 = transversal(arr, 0);
>     assert(r1.equal([1, 7, 13]));
>
>     assert(arr[1] == [7,8,9,10,11,12]);
>
>     auto r2 = [0, 2]
>               .map!(i => arr[i])()
>               .transversal(1);
>     assert(r2.equal([2, 14]));
>
>     auto r3 = [0, 2]
>               .map!(i => arr[i][0..3] ~ arr[i][5])();
>     assert(r3.equal([[1,2,3,6],[13,14,15,18]]));
> }
>
>
> Bye,
> bearophile

Thanks again. I started learning std.array and I should say it fits all my needs as a mathematics and combinatorics researcher. Now I think I am able to give up commercial softwares.
December 31, 2012
On Monday, 31 December 2012 at 13:29:02 UTC, Omid wrote:

>
>I started learning std.array

Actually, std.array and std.range both.