3 ms·
my %hash-table = ( abc => 1, def => 2, ); for %hash-table { say .key, ' => ', .value; } You can also use sub-signature deparsing
by b2gills 6y ago
my %hash-table = (
abc => 1,
def => 2,
);
for %hash-table {
say .key, ' => ', .value;
}
You can also use sub-signature deparsing.
for %hash-table -> $ (:$key, :$value) {
say "$key => $value"
}
That may take a while to get used to.
You don't have to use it, but it is a very powerful feature so it is definitely worth learning.
---
Really the most difficult thing seems to be that people aren't expecting Raku to be as consistent as it is. I think that is because most languages aren't.
For example you may expect this to work
for @array -> $element {
say $element
}
but you wouldn't necessarily expect this to work
if $var.method() -> $result {
say $result
}
In fact all keywords of the form `KEYWORD CONDITION {BLOCK}` will work with a pointy block.
You can also create a lambda using the same exact pointy block syntax.
my &multiply = -> $a, $b { $a * $b }
say multiply 4, 5; # 20
---
Also the signature part of a pointy block is exactly the same as a subroutine signature, and a method signature.
You can even create an anonymous signature object.
:( $foo, $bar )
(There aren't a lot of uses for it though.)
---
I still get surprised sometimes with how consistent Raku is.
- BruceEel 6y agoThis is a really good explanation, thank you.