How would you solve this 'interview' question in D?

John Colvin john.loughran.colvin at gmail.com
Thu Jun 27 12:48:36 PDT 2013


On Thursday, 27 June 2013 at 18:37:26 UTC, Timon Gehr wrote:
> On 06/26/2013 10:51 PM, Gary Willoughby wrote:
>> Just for a bit of fun, I saw this question posted on reddit 
>> the other
>> day and wondered how *you* would solve this in D?
>>
>> http://stackoverflow.com/questions/731832/interview-question-ffn-n
>
> int f(int x){ return x?(x&1?x:-x)+(x>0?1:-1):0; }
>
> unittest{
>     foreach(x;int.min..int.max)
>         assert(f(f(x))==-x);
> }

That highly compound statement..... Why? Surely you would never 
write anything like this either in production code (unless 
absolutely necessary) or in an interview?

It's fine as a one-off, but a whole code-base full of this style 
would be horrifying.


More information about the Digitalmars-d-learn mailing list