To write such an expressive code D

FG via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Feb 10 17:40:43 PST 2015


On 2015-02-11 at 01:56, bearophile wrote:
> Alternative solution closer to the F# code:
>
> import std.stdio, std.algorithm, std.typecons;
>
> int f(T)(T t) if (isTuple!T) {
>      return t.predSwitch(
>          tuple(0, 0, 0), 0,
>          tuple(0, 1, 1), 0,
>          tuple(1, 0, 1), 0,
>          tuple(1, 1, 0), 0,
>          /*else*/ 1);
> }
>
> void main() {
>      foreach (immutable a; 0 .. 2)
>          foreach (immutable b; 0 .. 2)
>              foreach (immutable c; 0 .. 2)
>                  writefln("%d xor %d xor %d = %d", a, b, c, tuple(a, b, c).f);
> }

Why bend over and try to make it F#? Screw the F# guy.
He was cheating with a switch, so why can't we cheat?

foreach(i;0..8)writefln("%d xor %d xor %d = %s",!!(i&4),!!(i&2),!!(i&1),"01101001"[i]);

Assimilate this!

Oh wait, you needed a function. OK, here's a function
(and just replace "01101001"[i] with xxor(i&4,i&2,i&1)):

int xxor(int a, int b, int c) {return (a&&b&&c)||(!a&&!b&&c)||(!a&&b&&!c)||(a&&!b&&!c);}

If it makes him dislike D even more, great! Mission accomplished. :)


More information about the Digitalmars-d-learn mailing list