Glazing Raku
Always when I see Raku code and write my self some, I always feel like there are design decisions that are crazy at first but later make so much sense.
The Sigil
A sigil in Raku determines how a variable should be treated. In JavaScript, we can write
let hello = () => Console.Log("Hello, World!")
let hello = "Hello, World";
In Raku we explicitly have to say that hello is a function using the sigil &
my &hello = sub { say "Hello, World!" }
my $hello = "Hello, World";
Sigils are a concept I only knew related to Ruby, and know that they do exist also in Perl 5 but work a little different. For more on Raku sigils read the doc.
The C2 wiki has some great discussions about some problems others have with sigils.
Calling C++
Something that shocked me a little bit was that Raku can call into C++. It can construct a Raku object with the fields and methods of an C++ object. I thought this was not possible because of C++ name mangling. After reading how names are mangled by each compiler its actually possible to account for each compiler mangling. They don’t say that the way the names are mangled is stable and won’t guarantee that, but it works.
Memorization
A common optimization is caching output values of a function so we don’t have to recompute them over and over again. The example given is usually the Fibonacci sequence. In Raku we can add the is cached trait to a function, to enable automatic memorization.
sub fib(Int $n where * >= 0) is cached {
return 1 if $n < 2;
return fib($n - 1) + fib($n - 2);
}
And in python using no automatic memoization:
fib_cache = {}
def fib_manual(n):
if n in fib_cache:
return fib_cache[n]
if n < 2:
result = 1
else:
result = fib_manual(n - 1) + fib_manual(n - 2)
fib_cache[n] = result
return result