Home > Raku > We can do more

We can do more

DataKinds proceeds with his educational translations of the way of the Python to the way of the butterfly. His quest is to show how to do stuff in Raku that is done in Python. As always TIMTOWTDI applies.

Looking at the examples one can see that Raku lacks nothing Python can provide. That’s nice to know but why would you want to switch if you can’t do more? So lets explore what Raku can do better.

For a Positional container we want to switch each even element with each uneven element. Flip element 1 with 2, then 3 with 4 and so on. We also want to provide a sensible error message for the case where this is not possible. Rakudo is providing and using the X::Parameter namespace. So that’s a natural place for our custom exception.

class X::Parameter::UnevenElementList is Exception {
    method message {
        'Positional with uneven elements in where clause.'
    }
}

Now we need a sub that takes one Positional in a positional parameter and does a type check on it. We shall move that type check into an aptly named sub. We don’t need to reduce readability if we don’t have to. Using a Callable container in a where clause is not in the docs. I will check Roast later and file an issue if needed.

sub swap-each-pair(@a is raw where &check-for-even-elements ) {
    sub check-for-even-elements {
        .elems %% 2
        || X::Parameter::UnevenElementList.new.throw
    }

    for @a <-> $a, $b {
        ($a, $b) = ($b, $a)
    }
}

my @l = 1..6;
@l.&swap-each-pair;
say @l;

I put the where clause sub into the main sub to not pollute the global namespace of my script. In a module we could stick it outside and reuse it. The argument Array is marked as is raw. That’s a bit redundant because @-sigiled arguments are call-by-reference anyway. By doing so I make it clear that this sub is meant to be a mutator. If you are a Haskell lubber, you may want to look away now. The <-> pointer is applying is rw to all arguments. That allows us to modify elements of @a with a destructuring assignment.

Using a good name for the where clause sub and throwing an exception we get a helpful error message with a stacktrace. We must do so or fear the LTA tag.

Positional with uneven elements in where clause.
  in sub check-for-even-elements at /home/dex/tmp/tmp.raku line 28
  in sub swap-each-pair at /home/dex/tmp/tmp.raku line 26
  in block <unit> at /home/dex/tmp/tmp.raku line 38

Both languages are late bound and don’t have strict type checks. In a large code base that can bite you. In that case it can be very helpful to do type and value checks on strategic points and provide error messages that actually help with debugging.

Raku got lots of features that make the life of module authors easier who want to make the life of module users easier. One might think that the language designers have written CPAN modules before.

Categories: Raku
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: