On Wed, Sep 26, 2018 at 2:57 AM Todd Chester <[email protected]
multi method words(Str:D $input: $limit = Inf --> Positional)
On 9/26/18 7:21 AM, Brandon Allbery wrote:
$limit sets a limit on how many words it will make. Inf means there's no
limit. Your assumption that it must be some kind of array index doesn't
make a lot of sense; this doesn't index at all, it splits at whitespace
until it's got the number of chunks you told it to make, indexing the
result is your problem. Small pieces of functionality, not "god
functions" that try to do everything you can possibly think of.
Hi Brandon,
So, "$limit = Inf" means that I can put an infinite number of
stuff in the [] or ()
p6 '"a b c d e".words(3)[2,4].say;'
(c Nil)
$ p6 '"a b c d e".words(3).say;'
(a b c)
I really think that could be written better.
First off the parameter is not a "limit". It is
a selection.
And second, "Inf" is a "type", meaning "infinity" or larger
than Perl's memory allocation can handle. It is confusing
to use it to state that there can be any number of selections
in the parameter.
$ p6 '"a b c d e".words()[2,4,1,3,3,3,3,20].say;'
(c e b d d d d Nil)
It also does not tell that the parameter(s) is/are integers
or what happens if you supply a sting (error) or a real (it
truncates):
$ p6 '"a b c d e".words()["a"].say;'
Cannot convert string to number: base-10 number must begin
with valid digits or '.' in '⏏a' (indicated by ⏏)
in block <unit> at -e line 1
$ p6 '"a b c d e".words()[ 2.5 ].say;'
c
Third, it does not state the difference between using () and [].
Or how to mix and match them.
$ p6 '"a b c d e".words(3).say;'
(a b c)
Where (3) gives you the first three words
-T