DEV Community

Cover image for Abuzz with FizzBuzz
habere-et-dispertire
habere-et-dispertire

Posted on

Abuzz with FizzBuzz

zeekar asked about FizzBuzz ideas ( zaggy.nl ) in raku.

¡ Exercism to the rescue ! The programming practice site has some seventy exercises to try in raku. One of particular interest here is the Raindrops exercise ( exercism.org ) -- which is FizzBuzz ( en.wikipedia.org ) in disguise.

Caleb Miller's solution

Paraphrasing @steffan153 's concise solution ( exercism.org ) :

say ([~] <Fizz Buzz> Zx $_ <<%%<< <3 5> or $_) for 1..100
Enter fullscreen mode Exit fullscreen mode

The somewhat unintuitive magic here is that the string repetition operator x can be used to repeat Fizz or Buzz "boolean times". 🧐

¿ What on earth ( docs.raku.org ) ?

Raku happily numifies ( perldoc.perl.org ) the booleans to integers -- True becomes 1 and False becomes 0.

+True, +False
# (1 0)
Enter fullscreen mode Exit fullscreen mode

I remain in wonder at the thoughtful consideration of raku's edge-case handling. I didn't imagine that booleans would be cast to integer and so help in the reduction.

Boundaries are an opportunity to experience growth but all too often elicit carelessness. I sense raku's mindful approach here as extending consideration beyond concern for the common case to include the common edge-case.

The dance is joined when interplay and interrelatedness are akin to first-class citizens... and so I've come to describe raku not as a multi-paradigm language but as a synthesis or constellated language.

Top comments (2)

Collapse
 
raiph profile image
raiph

Paraphrasing @steffan153's concise solution:

Challenge accepted!

Just kidding, but I like to think the following is reasonably "Clear Golf", as I've explained back on the reddit thread:

say "{'Fizz' when *%%3}{'Buzz' when *%%5}" or $_ for 1..100;
Enter fullscreen mode Exit fullscreen mode
Collapse
 
habere-et-dispertire profile image
habere-et-dispertire • Edited

I really enjoyed your take on it @raiph -- so thank you for sharing.
I sense raku's expressivity lending a hand when we re-imagine obfuscatory code golf with conceptual clear golf :

Raku is designed to be more concise in concepts than in keystrokes.
Perl to Raku guide - in a nutshell, Whitespace ( docs.raku.org )

I found another approach using modular arithmetic which tickled :

map {
    < 0 FizzBuzz 6 Fizz 10 Buzz >
    .pairup
    .Hash{ $_⁴ mod 15 }
    // $_
}, 1..100
Enter fullscreen mode Exit fullscreen mode

RakuAST should fix this multi-line solution, but we can play with the single line version ( codeapi.org ) in the meanwhile.

Wouldn't it be great to run and edit examples ( github.com ) in the official documentation with codeapi ?

What do you think ?