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
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)
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)
Challenge accepted!
Just kidding, but I like to think the following is reasonably "Clear Golf", as I've explained back on the reddit thread:
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 :
I found another approach using modular arithmetic which tickled :
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 ?