September 21, 2017
On 20.09.2017 23:13, nkm1 wrote:
> 
>> Example of a good use:
>>
>> void floodFill(dchar[][] data,dchar c,int i,int j) {
>>     void dfs(int a, int b) {
>> Example of a good use:
>>
>> void floodFill(dchar[][] data,dchar c,int i,int j) {
>>     void dfs(int a, int b) {
>>         if (a<0 || a >= data.length) return;
>>         if (b<0 || b >= data[a].length) return;
>>         if (data[a][b] == c) return;
>>         data[a][b] = c;
>>         foreach(i; 0 .. 4){
>>             dfs(a + (i==0) - (i==1),
>>                 b + (i==2) - (i==3));
>>         }
>>     }
>>     dfs(i, j);
>> }
> 
> I don't agree it's a good use.

Well, you can trust me that it is. ;)

> Actually, that seems quite obfuscated to me.

It's actually straightforward. This C code is obfuscated:

#define C ,
#define S(X) "Fizz"X"Buzz"
int main(){
  char j[]="00",*k=j+1,*g[]={k,S(C),S()};
  while(':'-*j)
    ++*k>'9'&&++*j&&(1<:*g=j:>='0'),
    puts(g[!((*j+*k)%3)|2*(3==*k%5)]);
  return 0;
}

(And yet, you are probably able to guess what it does.)

> Consider:

(I had considered that.)

> foreach (point; [[1, 0], [-1, 0], [0, 1], [0, -1]]) {
>     dfs(a + point[0], b + point[1]);
> }

void floodFill(dchar[][] data,dchar c,int i,int j) @nogc {

I.e., I'd possibly call this a bad use of array literals. ;)
(The fix is to initialize a static array of static arrays.)

Also, it's not really a point, rather, it's a delta.

> Finds some random 10 programmers and ask them what's easier to understand...

Personally, I think it is a tie as both are obvious.

> Also, in my experience (in C and C++) it is extremely rare for programmers to use booleans in arithmetic. So even if in some situation you would have to replace this thing with more verbose (i == 0 ? 1 : 0), it's no big deal.

I didn't say it was.

> OTOH, booleans being numbers is a source of some bugs (just like other cases of weak typing). Not a ton of bugs, but the utility of implicit conversion to numbers is so unnoticeable that I'm sure it's just not worth it. 

So am I, but I wasn't commenting on trade-offs, only the view that there are no good uses.
September 21, 2017
On 9/21/17 11:06 AM, Timon Gehr wrote:

>>>         foreach(i; 0 .. 4){
>>>             dfs(a + (i==0) - (i==1),
>>>                 b + (i==2) - (i==3));
>>>         }

...

> So am I, but I wasn't commenting on trade-offs, only the view that there are no good uses.

This seems way easier for me to grok, and is how I would write it.

dfs(a + 1, 0); dfs(a - 1, 0);
dfs(0, b + 1); dfs(0, b - 1);

-Steve
September 21, 2017
On 9/21/17 11:48 AM, Steven Schveighoffer wrote:
> On 9/21/17 11:06 AM, Timon Gehr wrote:
> 
>>>>         foreach(i; 0 .. 4){
>>>>             dfs(a + (i==0) - (i==1),
>>>>                 b + (i==2) - (i==3));
>>>>         }
> 
> ....
> 
>> So am I, but I wasn't commenting on trade-offs, only the view that there are no good uses.
> 
> This seems way easier for me to grok, and is how I would write it.
> 
> dfs(a + 1, 0); dfs(a - 1, 0);
> dfs(0, b + 1); dfs(0, b - 1);

Of course, without bugs :)

dfs(a + 1, b); dfs(a - 1, b);
dfs(a, b + 1); dfs(a, b - 1);

-Steve
September 21, 2017
On Wednesday, 20 September 2017 at 19:25:58 UTC, Timon Gehr wrote:
>
> Actually, it is useful enough to have a Wikipedia page:
> https://en.wikipedia.org/wiki/Iverson_bracket
>
> Example of a good use:
>
> void floodFill(dchar[][] data,dchar c,int i,int j) {
>     void dfs(int a, int b) {
>         if (a<0 || a >= data.length) return;
>         if (b<0 || b >= data[a].length) return;
>         if (data[a][b] == c) return;
>         data[a][b] = c;
>         foreach(i; 0 .. 4){
>             dfs(a + (i==0) - (i==1),
>                 b + (i==2) - (i==3));
>         }
>     }
>     dfs(i, j);
> }

I would rather use an explicit function for such use cases:

int delta(bool x) { return x == true ? 1 : 0; }
September 21, 2017
On 21.09.2017 17:53, Steven Schveighoffer wrote:
> On 9/21/17 11:48 AM, Steven Schveighoffer wrote:
>> On 9/21/17 11:06 AM, Timon Gehr wrote:
>>
>>>>>         foreach(i; 0 .. 4){
>>>>>             dfs(a + (i==0) - (i==1),
>>>>>                 b + (i==2) - (i==3));
>>>>>         }
>>
>> ....
>>
>>> So am I, but I wasn't commenting on trade-offs, only the view that there are no good uses.
>>
>> This seems way easier for me to grok, and is how I would write it.
>>
>> dfs(a + 1, 0); dfs(a - 1, 0);
>> dfs(0, b + 1); dfs(0, b - 1);
> 
> Of course, without bugs :)
> ...

It was the same bug in every copy. Impressive! ;)

> dfs(a + 1, b); dfs(a - 1, b);
> dfs(a, b + 1); dfs(a, b - 1);
> 
> -Steve

This is a good alternative, maybe arrange it like this:

dfs(a + 1, b); dfs(a, b + 1);
dfs(a - 1, b); dfs(a, b - 1);

Just need to make sure no code is duplicated. (For example, there could be more work to do in each loop iteration, and then you'd need to use a local function.)
September 21, 2017
On 9/21/17 3:24 PM, Timon Gehr wrote:

> This is a good alternative, maybe arrange it like this:
> 
> dfs(a + 1, b); dfs(a, b + 1);
> dfs(a - 1, b); dfs(a, b - 1);

Yes, better!

> Just need to make sure no code is duplicated. (For example, there could be more work to do in each loop iteration, and then you'd need to use a local function.)

Hm... I suppose you would do that work at the beginning of dfs, like you check the limits?

-Steve
September 22, 2017
On Tuesday, 19 September 2017 at 18:34:13 UTC, Brad Anderson wrote:
> On Tuesday, 19 September 2017 at 18:17:47 UTC, jmh530 wrote:
>> On Tuesday, 19 September 2017 at 17:40:20 UTC, EntangledQuanta wrote:
>>>
>>> Thanks for wasting some of my life... Just curious about who will justify the behavior and what excuses they will give.
>>
>> Pretty sure it would be exactly the same thing in C...
>
> It is (and Java and C# and pretty much every other C style language though the nicer implicit conversion rules means it gets caught more easily). It is a big source of programmer mistakes. It comes up frequently in PVS Studio's open source analysis write ups.

So I checked for all the languages listed: C, C#, Java, Javascript, C++, PHP, Perl and D. All have the same order of precedence except, as always the abomination of all languages: C++ (kill it with fire).
C++ is the only language that has the ternary operator have the same precedence than the assignment operators.
This means a>=5?b=100:b=200; will compile in C++ but not in all the other languages. That's one reason why it irritates me when people continuously refer to C and C++ as if it was the same thing (yes I mean you Walter and Andrei).
Even PHP and Perl got it right, isn't that testament of poor taste Bjarne?. :-)


September 23, 2017
On Tuesday, 19 September 2017 at 18:34:13 UTC, Brad Anderson wrote:
> On Tuesday, 19 September 2017 at 18:17:47 UTC, jmh530 wrote:
>> On Tuesday, 19 September 2017 at 17:40:20 UTC, EntangledQuanta wrote:
>>>
>>> Thanks for wasting some of my life... Just curious about who will justify the behavior and what excuses they will give.
>>
>> Pretty sure it would be exactly the same thing in C...
>
> It is (and Java and C# and pretty much every other C style language though the nicer implicit conversion rules means it gets caught more easily). It is a big source of programmer mistakes. It comes up frequently in PVS Studio's open source analysis write ups.

So I checked for all the languages listed: C, C#, Java, Javascript, C++, PHP, Perl and D. All have the same order of precedence except, as always the abomination of all languages: C++ (kill it with fire).
C++ is the only language that has the ternary operator have the same precedence than the assignment operators.
This means a>=5?b=100:b=200; will compile in C++ but not in all the other languages. That's one reason why it irritates me when people continuously refer to C and C++ as if it was the same thing (yes I mean you Walter and Andrei).
Even PHP and Perl got it right, isn't that testament of poor taste Bjarne?. :-)


September 26, 2017
On Saturday, 23 September 2017 at 20:43:36 UTC, Patrick Schluter wrote:
> So I checked for all the languages listed: C, C#, Java, Javascript, C++, PHP, Perl and D. All have the same order of precedence except, as always the abomination of all languages: C++ (kill it with fire).
> C++ is the only language that has the ternary operator have the same precedence than the assignment operators.
> This means a>=5?b=100:b=200; will compile in C++ but not in all the other languages. That's one reason why it irritates me when people continuously refer to C and C++ as if it was the same thing (yes I mean you Walter and Andrei).
> Even PHP and Perl got it right, isn't that testament of poor taste Bjarne?. :-)

It's not quite as big of a deal as it seems because of the RTL associativity for both of them but still a very weird thing at face value.
October 04, 2017
On 2017/09/19 19:40, EntangledQuanta wrote:
> 
>      writeln(x + ((_win[0] == '@') ? w/2 : 0));
>      writeln(x + (_win[0] == '@') ? w/2 : 0);
> 
> The first returns x + w/2 and the second returns w/2!
> 
> WTF!!! This stupid bug has caused me considerable waste of time. Thanks Walter! I know you care so much about my time!
> 
> I assume someone is going to tell me that the compiler treats it as
> 
> writeln((x + (_win[0] == '@')) ? w/2 : 0);
> 
> Yeah, that is really logical! No wonder D sucks and has so many bugs! Always wants me to be explicit about the stuff it won't figure out but it implicitly does stuff that makes no sense. The whole point of the parenthesis is to inform the compiler about the expression to use. Not use everything to the left of ?.
> 
> Thanks for wasting some of my life... Just curious about who will justify the behavior and what excuses they will give.
When you get too angry about the little things in life. "There are people dying in the world and this angers you, chill".
1 2 3 4
Next ›   Last »