• Download Gist
here's how to extend .fmt to treat the last value separately
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class List is also {
multi method fmt($format = '%s', $sep = ' ', $lastsep = $sep) {
my $result = '';
my $index = 0;
for self.list -> $elem {
$result ~= do given $index++ {
when 0 { }
when self.end { $lastsep }
when * { $sep }
};
$result ~= sprintf($format, $elem);
}
$result;
}
}
 
say <huey dewey louie>.fmt('%s', ', ', ', and ')

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.