Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

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

Posted on • Edited on

     

Abuzz with FizzBuzz

zeekar asked aboutFizzBuzz ideas ( redlib.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 theRaindrops exercise ( exercism.org ) — which isFizzBuzz ( en.wikipedia.org ) in disguise.

Caleb Miller's solution

Paraphrasing@steffan153 'sconcise solution ( exercism.org ) :

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

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

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

Raku happilynumifies ( perldoc.perl.org ) the booleans to integers —True becomes1 andFalse becomes0.

+True, +False# (1 0)
Enter fullscreen modeExit 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 commonedge-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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
raiph profile image
raiph
  • Joined

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 modeExit fullscreen mode
CollapseExpand
 
habere-et-dispertire profile image
habere-et-dispertire
  • Joined
• Edited on• Edited

I really enjoyedyour take on it @raiph ( redlib.zaggy.nl ) -- 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 approachusing modular arithmetic ( philcrissman.net ) which tickled :

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

RakuAST willfix this multi-line ( github.com ) solution, but we can play with thesingle line version ( codeapi.org ) in the meanwhile.

Wouldn't it be great torun and edit examples ( github.com ) in the official documentation withcodeapi ( codeapi.org ) ?

What do you think ?

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More fromhabere-et-dispertire

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp