www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Possible to write a classic fizzbuzz example using a UFCS chain?

reply "Gary Willoughby" <dev nomad.so> writes:
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
Apr 28 2015
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Gary Willoughby:

 I wondered if it was possible to write a classic fizzbuzz[1] 
 example using a UFCS chain? I've tried and failed.
Is this OK? void main() { import std.stdio, std.algorithm, std.range, std.conv, std.functional; 100 .iota .map!(i => ((i + 1) % 15).predSwitch( 0, "FizzBuzz", 3, "Fizz", 5, "Buzz", 6, "Fizz", 9, "Fizz", 10, "Buzz", 12, "Fizz", /*else*/ i.text)) .reverseArgs!writefln("%-(%s\n%)"); } Bye, bearophile
Apr 28 2015
parent "Idan Arye" <GenericNPC gmail.com> writes:
On Tuesday, 28 April 2015 at 11:03:09 UTC, bearophile wrote:
 Gary Willoughby:

 I wondered if it was possible to write a classic fizzbuzz[1] 
 example using a UFCS chain? I've tried and failed.
Is this OK? void main() { import std.stdio, std.algorithm, std.range, std.conv, std.functional; 100 .iota .map!(i => ((i + 1) % 15).predSwitch( 0, "FizzBuzz", 3, "Fizz", 5, "Buzz", 6, "Fizz", 9, "Fizz", 10, "Buzz", 12, "Fizz", /*else*/ i.text)) .reverseArgs!writefln("%-(%s\n%)"); } Bye, bearophile
I've change your solution to use predSwitch's custom predicate: 100 .iota .map!(i => ((i + 1) % 15).predSwitch!((a, b) => 0x10 * (0 == a % 3) + (0 == a % 5) == b)( 0x00, i.text, 0x10, "Fizz", 0x01, "Buzz", 0x11, "FizzBuzz")) .reverseArgs!writefln("%-(%s\n%)");
Apr 28 2015
prev sibling next sibling parent reply "w0rp" <devw0rp gmail.com> writes:
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
You can do this. import std.range : iota; import std.algorithm : map, each; import std.typecons : Tuple, tuple; import std.stdio : writeln; Tuple!(size_t, string) fizzbuzz(size_t number) { if (number % 3 == 0) { if (number % 5 == 0) { return tuple(number, "fizzbuzz"); } else { return tuple(number, "fizz"); } } else if (number % 5 == 0) { return tuple(number, "buzz"); } return tuple(number, ""); } void main(string[] argv) { iota(1, 101) .map!fizzbuzz .each!(x => writeln(x[0], ": ", x[1])); } The desired output may vary, depending on variations of FizzBuzz. (Some want the number always in the output, some don't.) You could maybe do crazy ternary expressions instead of writing a function, or put it directly in there as a lambda, but both just look ugly, and you might as well just write a function for it.
Apr 28 2015
parent "wobbles" <grogan.colin gmail.com> writes:
On Tuesday, 28 April 2015 at 11:04:12 UTC, w0rp wrote:
 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
You can do this. import std.range : iota; import std.algorithm : map, each; import std.typecons : Tuple, tuple; import std.stdio : writeln; Tuple!(size_t, string) fizzbuzz(size_t number) { if (number % 3 == 0) { if (number % 5 == 0) { return tuple(number, "fizzbuzz"); } else { return tuple(number, "fizz"); } } else if (number % 5 == 0) { return tuple(number, "buzz"); } return tuple(number, ""); } void main(string[] argv) { iota(1, 101) .map!fizzbuzz .each!(x => writeln(x[0], ": ", x[1])); } The desired output may vary, depending on variations of FizzBuzz. (Some want the number always in the output, some don't.) You could maybe do crazy ternary expressions instead of writing a function, or put it directly in there as a lambda, but both just look ugly, and you might as well just write a function for it.
Crazy ternary experessions: void main(){ "%s".writefln( iota(1, 100, 1) .map!(a => a%3==0 ? (a%5==0? "fizzbuzz" : "fizz") : a%5==0 ? "buzz" : a.to!string)); }
Apr 28 2015
prev sibling next sibling parent reply "Ivan Kazmenko" <gassa mail.ru> writes:
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
Here is another, hopefully readable, version with lambda built on ternary operators: ----- import std.algorithm, std.conv, std.functional, std.range, std.stdio; void main () { sequence !(q{n + 1}) .map !(x => (x % 3 == 0 ? "fizz" : "") ~ ( x % 5 == 0 ? "buzz" : "") ~ (x % 3 != 0 && x % 5 != 0 ? x.text : "")) .take (100) .join (" ") .writeln; } ----- Output: ----- 1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz 16 17 fizz 19 buzz fizz 22 23 fizz buzz 26 fizz 28 29 fizzbuzz 31 32 fizz 34 buzz fizz 37 38 fizz buzz 41 fizz 43 44 fizzbuzz 46 47 fizz 49 buzz fizz 52 53 fizz buzz 56 fizz 58 59 fizzbuzz 61 62 fizz 64 buzz fizz 67 68 fizz buzz 71 fizz 73 74 fizzbuzz 76 77 fizz 79 buzz fizz 82 83 fizz buzz 86 fizz 88 89 fizzbuzz 91 92 fizz 94 buzz fizz 97 98 fizz buzz ----- Ivan Kazmenko.
Apr 28 2015
parent reply "Andrea Fontana" <nospam example.com> writes:
On Tuesday, 28 April 2015 at 13:59:48 UTC, Ivan Kazmenko wrote:
 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
Here is another, hopefully readable, version with lambda built on ternary operators: ----- import std.algorithm, std.conv, std.functional, std.range, std.stdio; void main () { sequence !(q{n + 1}) .map !(x => (x % 3 == 0 ? "fizz" : "") ~ ( x % 5 == 0 ? "buzz" : "") ~ (x % 3 != 0 && x % 5 != 0 ? x.text : "")) .take (100) .join (" ") .writeln; } ----- Output: ----- 1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz 16 17 fizz 19 buzz fizz 22 23 fizz buzz 26 fizz 28 29 fizzbuzz 31 32 fizz 34 buzz fizz 37 38 fizz buzz 41 fizz 43 44 fizzbuzz 46 47 fizz 49 buzz fizz 52 53 fizz buzz 56 fizz 58 59 fizzbuzz 61 62 fizz 64 buzz fizz 67 68 fizz buzz 71 fizz 73 74 fizzbuzz 76 77 fizz 79 buzz fizz 82 83 fizz buzz 86 fizz 88 89 fizzbuzz 91 92 fizz 94 buzz fizz 97 98 fizz buzz ----- Ivan Kazmenko.
[2, -1, -1, 0, -1, 1, 0, -1, -1, 0, 1, -1, 0, -1, -1] .cycle .enumerate .drop(1) .take(100) .map!(a => a[1]>=0?["Fizz","Buzz","FizzBuzz"][a[1]]:a[0].to!string) .each!writeln;
Apr 28 2015
parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Tuesday, 28 April 2015 at 16:06:16 UTC, Andrea Fontana wrote:
 On Tuesday, 28 April 2015 at 13:59:48 UTC, Ivan Kazmenko wrote:
 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
Here is another, hopefully readable, version with lambda built on ternary operators: ----- import std.algorithm, std.conv, std.functional, std.range, std.stdio; void main () { sequence !(q{n + 1}) .map !(x => (x % 3 == 0 ? "fizz" : "") ~ ( x % 5 == 0 ? "buzz" : "") ~ (x % 3 != 0 && x % 5 != 0 ? x.text : "")) .take (100) .join (" ") .writeln; } ----- Output: ----- 1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz 16 17 fizz 19 buzz fizz 22 23 fizz buzz 26 fizz 28 29 fizzbuzz 31 32 fizz 34 buzz fizz 37 38 fizz buzz 41 fizz 43 44 fizzbuzz 46 47 fizz 49 buzz fizz 52 53 fizz buzz 56 fizz 58 59 fizzbuzz 61 62 fizz 64 buzz fizz 67 68 fizz buzz 71 fizz 73 74 fizzbuzz 76 77 fizz 79 buzz fizz 82 83 fizz buzz 86 fizz 88 89 fizzbuzz 91 92 fizz 94 buzz fizz 97 98 fizz buzz ----- Ivan Kazmenko.
[2, -1, -1, 0, -1, 1, 0, -1, -1, 0, 1, -1, 0, -1, -1] .cycle .enumerate .drop(1) .take(100) .map!(a => a[1]>=0?["Fizz","Buzz","FizzBuzz"][a[1]]:a[0].to!string) .each!writeln;
An inefficient but neat version of that: void main() { [3, 3, 0, 3, 1, 0, 3, 3, 0, 1, 3, 0, 3, 3, 2] .cycle .enumerate(1) .take(100) .map!(a => ["Fizz","Buzz","FizzBuzz",a[0].to!string][a[1]]) .each!writeln; } note `enumerate(1)` to avoid having to drop one. But if we always print the number then we can have: import std.range, std.stdio, std.conv, std.algorithm; void main() { ["","","Fizz","","Buzz","Fizz","","","Fizz","Buzz","","Fizz","","","FizzBuzz"] .cycle .enumerate(1) .take(100) .each!(a => writeln(a[0],'\t',a[1])); }
Apr 28 2015
prev sibling next sibling parent "Atila Neves" <atila.neves gmail.com> writes:
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
Apr 29 2015
prev sibling parent "safety0ff" <safety0ff.dev gmail.com> writes:
Just for fun:

//     map,           join,      text,     iota,      writeln,   
tuple
import std.algorithm, std.array, std.conv, std.range, std.stdio, 
std.typecons;

void main()
{
   iota(1,100)
   .map!(a => tuple(a, a % 3 == 0 ? 0 : 4, a % 5 == 0 ? 8 : 4))
   .map!(a => a[1] == a[2] ? a[0].text : "fizzbuzz"[a[1] .. a[2]])
   .join(", ")
   .writeln;
}
Apr 30 2015