Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl 6 interpreting the output of the split function as an integer list / array

Tags:

raku

say "1 10".split(" ") 

returns (1,10)

When I use those 1 and 10 as arguments to the sequence operator [...]

say [...] "1 10".split(" ")

returns just (1) while it's supposed to return (1 2 3 4 5 6 7 8 9 10) I guess it's because the output of the split function is interpreted as string.

How to solve that problem? Thank you.

like image 580
Terry Avatar asked Aug 07 '19 13:08

Terry


1 Answers

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?".

like image 169
raiph Avatar answered Sep 26 '22 14:09

raiph