My fun module Acme::Rautavistic::Sort is now uploaded to zef:
- https://raku.land/zef:renormalist/Acme::Rautavistic::Sort
but phew... it took me a while to grok how to do it right.
Here are some learned lessons:
1/
My fun module Acme::Rautavistic::Sort is now uploaded to zef:
- https://raku.land/zef:renormalist/Acme::Rautavistic::Sort
but phew... it took me a while to grok how to do it right.
Here are some learned lessons:
1/
Types
Unlike Perl, Raku is really a typed language.
Beginners who want to start with a Perl'ish (seemingly) untyped style need to know the operators and types that encapsulate the DWIM-work you know from Perl.
The keyword is "allomorphs". My revelation came from those two links:
- https://rakujourney.wordpress.com/2024/04/18/allomorphs-unicode/
- https://github.com/rakudo/rakudo/blob/main/src/core.c/allomorphs.rakumod
2/
Operators to help you make the code DWIM:
<1 2 3> ... like qw() but create allomorphs
cmp ... compare type-aware, returns a type "Order" (values Same, More, Less) you can feed into smart match
~~ ... smart match
3/
Pitfalls:
- foo(1, 2, 3) is not foo([1, 2, 3]) is not foo([<1 2 3>])
- <1 2 3> is more than just quote words but creates allomorphs. You need it.
- To create "nothing" in lists, instead of Perl's () relying on list flattening, you can return Empty
- undefined values warn loud when used because of missing type, so you often need to handle it explicitly
- Nil is not Any
/4
Life savers:
- dd ... simple built-in Data::Dumper:
$ raku -e 'dd(5 cmp "6")'
Order::Less
$ raku -e 'dd(5 cmp "6" ~~ Same|Less)'
Bool::True
- Data::Dump::Tree ... fancy Data::Dumper
https://raku.land/github:nkh/Data::Dump::Tree
That's it for today.
5/5
Bonus:
Did you see the Same|Less?
That's a junction, i.e. multiple values in one place, here Same and Less which all get processed implicitly without you doing stunts. In the example the smartmatch
... ~~ Same|Less
matches if left side matches Same or if it matches Less.