Skip to content

Instantly share code, notes, and snippets.

@sdondley
Created June 11, 2022 17:44
Show Gist options
  • Save sdondley/6ac51bb98c8f571f71e1543fc40f0d7e to your computer and use it in GitHub Desktop.
Save sdondley/6ac51bb98c8f571f71e1543fc40f0d7e to your computer and use it in GitHub Desktop.
Raku List Basics
#! /usr/bin/env raku
use v6;
# se
my $pause = False;
blank;
say "LIST BASICS";
blank;
das '(1, 2)', "This is a simple list with two elements";
das '(1, 2)', "The 'say' function will output lists in parentheses";
das '()', "An empty list";
das '(1, 2, 3)[2]', "Lists are positional and elements can be accessed with an index number in square brackets";
das '(1, 2; 3)', "Semis delimit lists";
das '(1, 2), (3, 4)', "A List of lists, multidimensional array syntax";
das '(1; 2)', "Lists with one element are flattened";
das '(7,)', "A list with one element. Commas create lists";
das '(1)', "This is not a list because there is no comma";
das '(1,2).WHAT';
das '(1,2).^name';
das "('home team', 'away team')", "Note also that list elements are separated with white space when output";
das '(,)', "This throws a compile time error:";
das '(1, 2, 3).elems', 'Get the number of elements with .elems';
das ('my $list = (1, 2, 3);',
'$list.elems;'),
'Lists can be assigned to a scalar';
das ('my @list = (1, 2, 3);',
'@list[0]'),
'Lists can be assigned to an array and they will become Positional';
sub blank(Int:D $lines = 0) {
(loop { say '' })[$lines];
}
sub das-result(Str:D $code) {
print 'Output: ';
try { say $code.EVAL };
if $! { say 'ERROR!' }
blank if $pause;
}
sub das-preview(Str:D $preview) {
say "$preview:" if $preview;
}
# do and say
multi sub das(List:D $code, Str:D $preview = '', $suffix?) {
my $count = $code.elems - 1;
$code[^$count]>>.say;
print 'say ';
print $code[$count];
print ";\n";
das-result($code.Str);
getc;
}
multi sub das(Str:D $code, Str:D $preview = '', $suffix?) {
say "$preview:" if $preview;
print 'say ';
print $code;
print ";\n";
das-result($code);
getc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment