June 03, 2015
Ooops, this is what I meant to post:

struct CoOrd
{
	int x, y;
}

CoOrd[][NumPaths]pathList;


I append values like so...

pathList[][n] ~= CoOrd(cX, cY);

But to get the expected values, I need to access them like this example for last x co-ord of last item:


int xx = pathList[NumPaths-1][$ - 1].x;


Doesn't seem right (but it works)...

June 03, 2015
On 06/03/2015 01:28 PM, Paul wrote:

> Ooops, this is what I meant to post:
>
> struct CoOrd
> {
>      int x, y;
> }
>
> CoOrd[][NumPaths]pathList;
>
>
> I append values like so...
>
> pathList[][n] ~= CoOrd(cX, cY);

I don't think you need the empty [] there. pathList[n] is one of the paths and you are adding a coordinate to it:

    pathList[n] ~= CoOrd(cX, cY);

> But to get the expected values, I need to access them like this example
> for last x co-ord of last item:
>
>
> int xx = pathList[NumPaths-1][$ - 1].x;
>
>
> Doesn't seem right (but it works)...

I makes perfect sense! ;) pathList[NumPaths-1] is the last path and you are accessing the last coordinate in it. C did it wrong. Luckily, I've always found C's syntax unbelievably backward (or inside-out?).

Ali

June 03, 2015
On Wednesday, 3 June 2015 at 20:28:57 UTC, Paul wrote:
> Ooops, this is what I meant to post:
>
> struct CoOrd
> {
> 	int x, y;
> }
>
> CoOrd[][NumPaths]pathList;
>
>
> I append values like so...
>
> pathList[][n] ~= CoOrd(cX, cY);

The "[]" does nothing here. You can leave it (or add more) out without changing the meaning:

pathList[][n] ~= CoOrd(cX, cY);
pathList[n] ~= CoOrd(cX, cY);
pathList[][][n] ~= CoOrd(cX, cY);

... all the same.

> But to get the expected values, I need to access them like this example for last x co-ord of last item:
>
>
> int xx = pathList[NumPaths-1][$ - 1].x;
>
>
> Doesn't seem right (but it works)...

Declarations are right to left, accesses are left to right. NumPaths is on the right in the declaration, and it's on the left in the access. Makes sense to me.
June 03, 2015
On Wednesday, 3 June 2015 at 20:33:02 UTC, Ali Çehreli wrote:
> > pathList[][n] ~= CoOrd(cX, cY);
>
> I don't think you need the empty [] there. pathList[n] is one of the paths and you are adding a coordinate to it:

Urgh, *that* is how I was confusing myself, the rest of the code 'looks right'. Strange that it compiles without warning and the spurious [] have no effect though. No doubt there is a reason for this that I probably won't understand!

Thanks again

Paul



June 03, 2015
On Wednesday, 3 June 2015 at 20:38:54 UTC, anonymous wrote:
> Declarations are right to left, accesses are left to right.
Succinctly put, even I should be able to remember that.

Paul

June 03, 2015
On 06/03/2015 01:45 PM, Paul wrote:

> On Wednesday, 3 June 2015 at 20:33:02 UTC, Ali Çehreli wrote:
>> > pathList[][n] ~= CoOrd(cX, cY);
>>
>> I don't think you need the empty [] there. pathList[n] is one of the
>> paths and you are adding a coordinate to it:
>
> Urgh, *that* is how I was confusing myself, the rest of the code 'looks
> right'. Strange that it compiles without warning and the spurious []
> have no effect though.

[] means "a slice to all elements". There is no difference in your code because pathList is already "a slice to all elements".

Here are a couple of use cases:

1) Being able to traverse a fixed-length array as a range:

import std.stdio;
import std.algorithm;

void main()
{
    int[2] arr = [ 1, 2 ];

    writeln(arr.map!(a => a * 10)); // COMPILATION ERROR
}

The last line cannot be compiled because a fixed-length array cannot provide popFront() because its length cannot be changed.

The fix is to slice all elements first by inserting []:

    writeln(arr[].map!(a => a * 10));

There, arr[] is a temporary slice that gets consumed as a range.


2) [] enables array-wise operations:

    int[] arr = [ 1, 2 ];
    arr[] *= 10;    // Multiplies all elements by 10

    arr[] = 42;     // sets all elements to 42


Note the important difference it makes here:

    int[] a = [ 1, 2 ];
    int[] b = [ 3, 4 ];

    a[] = b;    // makes the elements of 'a' same as 'b's elements
    a = b;      // makes 'a' be another slice to 'b's elements

You won't see any difference between the two lines above if you print 'a', but in the first case a.ptr != b.ptr and in the second case a.ptr == b.ptr.

Ali

June 04, 2015
On Wednesday, 3 June 2015 at 21:05:42 UTC, Ali Çehreli wrote:
> On 06/03/2015 01:45 PM, Paul wrote:
>
> > On Wednesday, 3 June 2015 at 20:33:02 UTC, Ali Çehreli wrote:
> >> > pathList[][n] ~= CoOrd(cX, cY);
> >>
> >> I don't think you need the empty [] there. pathList[n] is
> one of the
> >> paths and you are adding a coordinate to it:
> >
> > Urgh, *that* is how I was confusing myself, the rest of the
> code 'looks
> > right'. Strange that it compiles without warning and the
> spurious []
> > have no effect though.
>
> [] means "a slice to all elements". There is no difference in your code because pathList is already "a slice to all elements".
>
> Here are a couple of use cases:
>
> 1) Being able to traverse a fixed-length array as a range:
>
> import std.stdio;
> import std.algorithm;
>
> void main()
> {
>     int[2] arr = [ 1, 2 ];
>
>     writeln(arr.map!(a => a * 10)); // COMPILATION ERROR
> }
>
> The last line cannot be compiled because a fixed-length array cannot provide popFront() because its length cannot be changed.
>
> The fix is to slice all elements first by inserting []:
>
>     writeln(arr[].map!(a => a * 10));
>
> There, arr[] is a temporary slice that gets consumed as a range.
>
>
> 2) [] enables array-wise operations:
>
>     int[] arr = [ 1, 2 ];
>     arr[] *= 10;    // Multiplies all elements by 10
>
>     arr[] = 42;     // sets all elements to 42
>
>
> Note the important difference it makes here:
>
>     int[] a = [ 1, 2 ];
>     int[] b = [ 3, 4 ];
>
>     a[] = b;    // makes the elements of 'a' same as 'b's elements
>     a = b;      // makes 'a' be another slice to 'b's elements
>
> You won't see any difference between the two lines above if you print 'a', but in the first case a.ptr != b.ptr and in the second case a.ptr == b.ptr.
>
> Ali

Isn't a[] = b deprecated syntax? I believe you have to use a[] = b[] now.
June 04, 2015
On 06/04/2015 04:27 AM, Meta wrote:
> On Wednesday, 3 June 2015 at 21:05:42 UTC, Ali Çehreli wrote:

>>     a[] = b;    // makes the elements of 'a' same as 'b's elements

> Isn't a[] = b deprecated syntax? I believe you have to use a[] = b[] now.

Thanks. Unfortunately, 2.067.1 'dmd -w -de' is still quiet about that one.

Ali

June 04, 2015
On Thursday, 4 June 2015 at 16:52:30 UTC, Ali Çehreli wrote:
> On 06/04/2015 04:27 AM, Meta wrote:
> > On Wednesday, 3 June 2015 at 21:05:42 UTC, Ali Çehreli wrote:
>
> >>     a[] = b;    // makes the elements of 'a' same as 'b's
> elements
>
> > Isn't a[] = b deprecated syntax? I believe you have to use
> a[] = b[] now.
>
> Thanks. Unfortunately, 2.067.1 'dmd -w -de' is still quiet about that one.
>
> Ali

Then maybe I'm wrong. I was sure, though, that Andrei created a thread complaining about the change sometime last year (which is why I remember that it was changed in the first place).
1 2
Next ›   Last »