[OT] Converting booleans to numbers

Timon Gehr timon.gehr at gmx.ch
Thu Sep 21 19:24:06 UTC 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.)


More information about the Digitalmars-d-learn mailing list