Possible to write a classic fizzbuzz example using a UFCS chain?

Atila Neves via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Apr 29 14:39:20 PDT 2015


On Tuesday, 28 April 2015 at 10:46:54 UTC, Gary Willoughby wrote:
> After reading the following thread:
>
> http://forum.dlang.org/thread/nczgumcdfystcjqybtku@forum.dlang.org
>
> I wondered if it was possible to write a classic fizzbuzz[1] 
> example using a UFCS chain? I've tried and failed.
>
> [1]: http://en.wikipedia.org/wiki/Fizz_buzz

import std.stdio, std.algorithm, std.range, std.conv;


void main() {
     immutable words = [3: "fizz", 5: "buzz"];
     iota(1, 100).
         map!((int i) {
             immutable res = reduce!((a, b) => a ~ ((i % b == 0) ? 
words[b] : ""))("", words.keys);
             return res.empty ? i.to!string : res;
         }).
         writeln;
}


Atila


More information about the Digitalmars-d-learn mailing list