Brace expansion using ranges: Difference between revisions

Content added Content deleted
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: Line 1,124:
$incr.=abs;
$incr.=abs;


if try +$start ~~ Numeric and +$end ~~ Numeric {
if try all( +$start, +$end ) ~~ Numeric {
$incr = - $incr if $start > $end;
$incr = - $incr if $start > $end;


Line 1,134: Line 1,134:
@return = @this.map: { $string.subst($range, sprintf("%{'0' ~ max $sl, $el}d", $_) ) }
@return = @this.map: { $string.subst($range, sprintf("%{'0' ~ max $sl, $el}d", $_) ) }
}
}
elsif $start !~~ /\d/ and $end !~~ /\d/ {
elsif try +$start ~~ Numeric or +$end ~~ Numeric {
$incr = - $incr if $start gt $end;
return $string #fail
}

else {
my @this = $start lt $end ?? ($start, (*.ord + $incr).chr …^ * gt $end) !! ($start, (*.ord + $incr).chr …^ * lt $end);
@return = @this.map: { $string.subst($range, sprintf("%s", $_) ) }
my @this;
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: Line 1,159:
@return = $these.join: ' '
@return = $these.join: ' '
}
}
}
my $cnt = 1;
while $cnt != +@return {
$cnt = +@return;
@return.=map: { |.&expand }
}
}
@return
@return
Line 1,169: Line 1,183:
# Test some other features
# 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't end exactly on endpoint
# fail by design
'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 {
> -> $test {
say "$test ->";
say "$test ->";
my @strings = expand $test;
say (' ' xx * Z~ expand $test).join: "\n";
my $cnt = -1;
while $cnt != +@strings {
$cnt = +@strings;
@strings.=map: { |.&expand }
}

say (' ' xx * Z~ @strings).join: "\n";
say '';
say '';
}
}
Line 1,244: Line 1,243:
rangeless{random}string
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-{02..10..3}.txt ->
steppedAlphaRising{P..Z..2}.txt ->
stops after endpoint-02.txt
steppedAlphaRisingP.txt
stops after endpoint-05.txt
steppedAlphaRisingR.txt
stops after endpoint-08.txt
steppedAlphaRisingT.txt
steppedAlphaRisingV.txt
steppedAlphaRisingX.txt
steppedAlphaRisingZ.txt


simple {I,want,my,money,back} list ->
simple {just,give,me,money} list ->
simple I list
simple just list
simple want list
simple give list
simple my list
simple me list
simple money list
simple money list
simple back list


_{I,want,my,money,back} ->
{thatʼs,what,I,want} ->
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} ->
I want my money back
mixed terms fail {7..C}.txt


multi char emoji ranges fail {🌵🌵..🌵🌶} ->
alphanumeric mix{a..1}.txt ->
multi char emoji ranges fail {🌵🌵..🌵🌶}
alphanumeric mix{a..1}.txt
</pre>
</pre>