Skip to content

Instantly share code, notes, and snippets.

@Xliff
Last active May 23, 2023 08:48
Show Gist options
  • Save Xliff/4c39e2e85ab71da61b3aa427414ae82c to your computer and use it in GitHub Desktop.
Save Xliff/4c39e2e85ab71da61b3aa427414ae82c to your computer and use it in GitHub Desktop.
AI's are Writing in Raku, now!

I went to [bart.gooogle.com] and asked it: "How do you write lambda functions in Raku?"

Here's how it responded:

===

To write lambda functions in Raku, you can use the following syntax:

Code snippet

-> {
   # body of the lambda function
}

Use code with caution. Learn more For example, the following lambda function squares its input:

Code snippet

-> $x {
 $x * $x
}

[...] Lambda functions can be used anywhere that a subroutine can be used. For example, you can use a lambda function to filter a list:

Code snippet

my @numbers = (1, 2, 3, 4, 5);
 
my @even_numbers = @numbers.filter(-> $x { $x % 2 == 0 });
 
say @even_numbers;

[...] This will print the following output:

Code snippet [2, 4] [...] Lambda functions are a powerful tool that can be used to simplify and improve your Raku code.

Here are some additional tips for writing lambda functions in Raku:

Use the -> sigil to declare a lambda function. The body of the lambda function is a block of code. You can use variables, constants, and subroutines in the body of the lambda function. Lambda functions can be passed to other functions and subroutines. Lambda functions can be used to filter lists, map values, and perform other operations.

I then went on and asked it something that was vexing me in my $dayJob:

How can I prevent a malformed utf8 sequence from crashing my Raku program?

...and here's how that went:

There are a few things you can do to prevent a malformed UTF-8 sequence from crashing your Raku program.

First, you can use the decode method to decode a string before you use it. This will help to ensure that the string is in a valid UTF-8 format.

Second, you can use the is-valid-utf8 method to check if a string is in a valid UTF-8 format. If it is not, you can take appropriate action, such as displaying an error message or skipping the string.

Finally, you can use the warn-on-malformed-utf8 pragma to enable warnings for malformed UTF-8 sequences. This will help you > to identify and fix problems with your code.

Here is an example of how to use the decode method to decode a string before you use it:

Code snippet

my $string = "This is a malformed UTF-8 sequence";

my $decoded_string = $string.decode;

say $decoded_string;
[...]

This will print the following output:

Code snippet

This is a malformed UTF-8 sequence
[...]

Here is an example of how to use the is-valid-utf8 method to check if a string is in a valid UTF-8 format:

Code snippet

my $string = "This is a valid UTF-8 sequence";

if ($string.is-valid-utf8) {
  say "The string is in a valid UTF-8 format";
} else {
  say "The string is not in a valid UTF-8 format";
}
[...]

This will print the following output:

Code snippet

The string is in a valid UTF-8 format
[...]

Here is an example of how to use the warn-on-malformed-utf8 pragma to enable warnings for malformed UTF-8 sequences:

Code snippet

use warnings qw(warn-on-malformed-utf8);

my $string = "This is a malformed UTF-8 sequence";

say $string;
[...]

This will print the following output:

Code snippet

This is a malformed UTF-8 sequence

DANGEROUS: malformed UTF-8 sequence at -e line 5.
[...]

I hope this helps!

Of course, NONE of the suggestions in the second question were usable, which displays one of the drawbacks to AI: they are prone to hallucination... possibly due to error in training data, or a misfiled association between factoids.

However one thing is for certain: they speak Raku!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment