Perl 6 interpreting the output of the split function as an integer list / array
If you want numeric behavior then coerce to numerics:
say [...] +<< "1 10".split(" "); # (1 2 3 4 5 6 7 8 9 10)
This uses a <<
hyperop to apply a numeric coercion (prefix +
) to each element of the sequence generated by the split
.
Regarding sequence and range behavior with string endpoints:
SO Why does the Perl 6 sequence
'A' … 'AA'
have only one element?. What's described in the linked SO applies to the sequence you've specified, namely"1"..."10"
.The open Rakudo issue Sequence operator with string endpoints and no explicit generator produces unintuitive/undocumented results.
SO Why are some of my ranges insane?.
What you've written is equivalent to:
put gist "1"..."10";
(say
is equivalent to put gist
.)
A gist
of "1"..."10"
is (1)
.
That's because the gist
of List.new("1")
is (1)
just like the gist
of List.new("a")
is (a)
.
And "1"..."10"
evaluates to a List.new("1")
.
Why? I'm not sure yet but I'm exploring the available info.
Let's start with the doc. The doc for the infix ...
op says:
The default generator is
*.succ
or*.pred
, depending on how the end points compare
Well:
say "1" cmp "10"; # Less
which presumably means the sequence starts calling *.succ
.
And then:
say "1".succ; # 2
and:
say "2" cmp "10"; # More
It seems this results in the sequence immediately terminating after the "1" rather than including the "2" and continuing.
I'm continuing to search the bug queues and examine the code around the area that @wamba++ linked in their answer to the above linked SO "Why does the Perl 6 sequence 'A' … 'AA'
have only one element?".
As usual, raiph is giving the correct answer, but I find something missing about why it really does not work.
Main thing is that []
is a reduce operator, it's not applying whatever is inside it as an infix operator except as a side effect. For instance, this works:
say [+] <4 8>.words; # OUTPUT: «12»
But only because there are two components, and the reduce []
is applied to them, having the same effect. Ditto for ...
say [...] <4 8>.words; # OUTPUT: «(4 5 6 7 8)»
However that's not what you are looking for. You have two operands, a single operator, you want to call the operator itself. Which you can of course do by using its fully qualified name
say infix:<...>( | <3 5>.words ); # OUTPUT: «(3 4 5)»
as long as, of course, you flatten (with | ) its arguments to make it match the signature of an infix operator.
As usual, TIMTOWTDI. So do whatever suits you the best.