Brace expansion using ranges: Difference between revisions

→‎{{header|Raku}}: Rearrange, make expand sub self-contained, add some more stringy handling options and fails, test
m (add the extra examples from Wren)
(→‎{{header|Raku}}: Rearrange, make expand sub self-contained, add some more stringy handling options and fails, test)
Line 1,124:
$incr.=abs;
 
if try all( +$start ~~ Numeric and, +$end ) ~~ Numeric {
$incr = - $incr if $start > $end;
 
Line 1,134:
@return = @this.map: { $string.subst($range, sprintf("%{'0' ~ max $sl, $el}d", $_) ) }
}
elsif try +$start !~~ /\d/Numeric andor +$end !~~ /\d/Numeric {
$incr = -return $incrstring if $start gt $end;#fail
}
 
else {
my @this = $start lt $end ?? ($start, (*.ord + $incr).chr …^ * gt $end) !! ($start, (*.ord + $incr).chr …^ * lt $end);
@return =my @this.map: { $string.subst($range, sprintf("%s", $_) ) };
if $start.chars + $end.chars > 2 {
return $string if $start.succ eq $start or $end.succ eq $end; # fail
@this = $start lt $end ?? ($start, (*.succ xx $incr).tail …^ * gt $end) !! ($start, (*.pred xx $incr).tail …^ * lt $end);
}
else {
$incr = -$incr if $start gt $end;
@this = $start lt $end ?? ($start, (*.ord + $incr).chr …^ * gt $end) !! ($start, (*.ord + $incr).chr …^ * lt $end);
}
@return = @this.map: { $string.subst($range, sprintf("%s", $_) ) }
}
}
Line 1,150 ⟶ 1,159:
@return = $these.join: ' '
}
}
my $cnt = 1;
while $cnt != +@return {
$cnt = +@return;
@return.=map: { |.&expand }
}
@return
Line 1,169 ⟶ 1,183:
# Test some other features
 
'stop point not in sequence-{02..10..3}.txt'
# Emoji lists
steppedAlphaRising{P..Z..2}.txt
emoji{☃,☄}{★,🇺🇸,☆}lists
'simple {just,give,me,money} list'
{thatʼs,what,I,want}
'emoji {☃,☄}{★,🇺🇸,☆} lists'
'alphanumeric mix{ab7..ac1}.txt'
'alphanumeric mix{0A..0C}.txt'
 
# Doesn'tfail endby exactly on endpointdesign
'stops after endpoint-{02..10..3}.txt'
 
'mixed terms fail {7..C}.txt'
# Simple list
'multi char emoji ranges fail {🌵🌵..🌵🌶}'
'simple {I,want,my,money,back} list'
 
# Prefix, list
_{I,want,my,money,back}
 
# Postfix, list
{I,want,my,money,back}!
 
# List, no prefix, postfix
{I,want,my,money,back}
 
# fail by design
'alphanumeric mix{a..1}.txt'
> -> $test {
say "$test ->";
mysay @strings(' = ' xx * Z~ expand $test).join: "\n";
my $cnt = -1;
while $cnt != +@strings {
$cnt = +@strings;
@strings.=map: { |.&expand }
}
 
say (' ' xx * Z~ @strings).join: "\n";
say '';
}
Line 1,244 ⟶ 1,243:
rangeless{random}string
 
stop point not in sequence-{02..10..3}.txt ->
emoji{☃,☄}{★,🇺🇸,☆}lists ->
stop point not in sequence-02.txt
emoji☃★lists
stop point not in sequence-05.txt
emoji☃🇺🇸lists
stop point not in sequence-08.txt
emoji☃☆lists
emoji☄★lists
emoji☄🇺🇸lists
emoji☄☆lists
 
stops after endpoint-steppedAlphaRising{02P..10Z..32}.txt ->
stops after endpoint-02steppedAlphaRisingP.txt
stops after endpoint-05steppedAlphaRisingR.txt
stops after endpoint-08steppedAlphaRisingT.txt
steppedAlphaRisingV.txt
steppedAlphaRisingX.txt
steppedAlphaRisingZ.txt
 
simple {Ijust,wantgive,myme,money,back} list ->
simple Ijust list
simple wantgive list
simple myme list
simple money list
simple back list
 
_{thatʼs,what,I,want,my,money,back} ->
thatʼs what I want
_I
 
_want
emoji {☃,☄}{★,🇺🇸,☆} lists ->
_my
emoji ☃★ lists
_money
emoji ☃🇺🇸 lists
_back
emoji ☃☆ lists
emoji ☄★ lists
emoji ☄🇺🇸 lists
emoji ☄☆ lists
 
alphanumeric mix{ab7..ac1}.txt ->
alphanumeric mixab7.txt
alphanumeric mixab8.txt
alphanumeric mixab9.txt
alphanumeric mixac0.txt
alphanumeric mixac1.txt
 
alphanumeric mix{0A..0C}.txt ->
{I,want,my,money,back}! ->
alphanumeric mix0A.txt
I!
alphanumeric mix0B.txt
want!
alphanumeric mix0C.txt
my!
money!
back!
 
mixed terms fail {7..C}.txt ->
{I,want,my,money,back} ->
Imixed wantterms myfail money back{7..C}.txt
 
multi char emoji ranges fail {🌵🌵..🌵🌶} ->
alphanumeric mix{a..1}.txt ->
multi char emoji ranges fail {🌵🌵..🌵🌶}
alphanumeric mix{a..1}.txt
</pre>
 
10,327

edits