4 ms·
In https://rosettacode.org/wiki/FizzBuzz https://rosettacode.org/wiki/FizzBuzz there's a nice Raku implementation, with ever-more-compact varieties following.
by bloopernova 2y ago
In https://rosettacode.org/wiki/FizzBuzz https://rosettacode.org/wiki/FizzBuzz there's a nice Raku implementation, with ever-more-compact varieties following.
It seems like a really nice language but which niche should it occupy?
Edited to add, now that I'm at a computer:
for 1 .. 100 {
when $_ %% (3 & 5) { say 'FizzBuzz'; }
when $_ %% 3 { say 'Fizz'; }
when $_ %% 5 { say 'Buzz'; }
default { .say; }
}
The multi sub version is interesting. I'm not quite sure what multi subs are, they seem to be performing a pre-function-call validation to choose which sub to use?
multi sub fizzbuzz(Int $ where * %% 15) { 'FizzBuzz' }
multi sub fizzbuzz(Int $ where * %% 5) { 'Buzz' }
multi sub fizzbuzz(Int $ where * %% 3) { 'Fizz' }
multi sub fizzbuzz(Int $number ) { $number }
(1 .. 100)».&fizzbuzz.say;
- agumonkey 2y agoThey're nearing apl density :)
- klibertp 2y agoNon-interactive shell scripting. It's actually the best language in this niche, outclassing Perl and Tcl in terms of features and Python/JS in terms of expressivity. General purpose scripting. Orders of magnitude fewer packages and batteries than in Python or Ruby, but it does have a package manager and a bunch of libs for typical tasks. It's also capable of calling Python code almost seamlessly. For small (1 file) scripts, the expressivity of Raku means often half the lines of equivalent Python code, unless you hit a package that Python has and Raku doesn't. Everything related to text processing. The grammars parsed by Grammars don't have to be context-free. Parsing just about anything is a joy. Also, light data mangling - if it fits in RAM, you might not need any other tool (like R/Pandas). It's also a general-purpose, gradually typed, JIT-compiled, multiparadigm (OO, FP, procedural, array) language, so you can use just about anywhere you want. It's less great for mission-critical things (error handling is peculiar) and shell one-liners (the JIT takes a toll on startup time). It has a REPL, instead.
- bloopernova 2y agoFantastic answer, actually made me enthusiastic for Raku! Now I want to go mess with it to see what it can do with some of my standard use cases. (DevOps infrastructure code, various AWS Lambdas, lots of build pipelines)
- klibertp 2y agoIf you want a quick overview of what you can do with Raku (as opposed to what it is/how it works), I found this book to be nice: https://leanpub.com/raku-oneliners https://leanpub.com/raku-oneliners Also, make sure to hop on Freenode (or Discord bridge to it), the community is very welcoming :)
- bloopernova 2y agoThanks for the recommendation, I immediately bought that book. I'm a huge fan of people publishing on leanpub or similar :)
- lizmat 2y agoFreenode is actually dead: the Raku IRC channels are on libera.chat!
- zokier 2y agoTo me Raku represents small-scale data mangling/processing and glue language for small scripting. Hacker oriented, personal and individualistic, not something you'd push in corporate. In many ways similar role what Python had decade ago, and Perl had two decades ago.
- klibertp 2y agomulti subs allow you to declare multiple overloaded versions of a function (subroutine, sub). Dispatch between sub bodies happens based on either the type(s) of arguments or based on a truth value of a "guard" - a boolean expression or boolean function placed after `where` in sub declaration head. `%%` is a `modulo == 0` operator, which is surprisingly commonly needed. `fizzbuzz(Int $ where * %% 15)` can also be written `fizzbuzz(Int $x where $x %% 15)` - the difference is that you don't have to name the argument just for the check. When `fizzbuzz` is called, all known definitions' guards are checked to see which will be entered. The `*` in `* %% 15` is "whatever star", you can think of it as a short syntax for creating lambdas. There are multiple ways of constructing anonymous functions and blocks, you can get the same effect with `{ $_ %% 15 }`, `-> $a { $a %% 15 }`. `»` is a "hyper operator"; the next method-call will be performed on all elements of a collection on the left. Groovy uses `*.` for this I think. You could use a `.map` method instead, `(1..100).map(&fizzbuzz)` or `.map({ .&fizzbuzz })` (`{ .meth }` is equivalent to `-> $x { $x.meth }`) `.&` is a "call as method" operator: `fizzbuzz` is a function, so you can't call `1.fizzbuzz`, however, you can call `1.&fizzbuzz`. Also, in the first solution, `(3 & 5)` is a Junction. This is an important concept that introduces a degree of nondeterminism to the language - well worth reading about! :)