Front page | perl.perl6.users |
Postings from December 2020
Re: The ,= operator
Thread Previous
|
Thread Next
From:
Ralph Mellor
Date:
December 7, 2020 23:53
Subject:
Re: The ,= operator
Message ID:
CAPLR5ScCgzNiaNdzjQ=He_VjppbGdMsK8V80vsnRtJjoT=4U3Q@mail.gmail.com
> > @fib[1, 5, 10..15, 20]
> (1 8 (89 144 233 377 610 987) 10946)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
> Why???
As Joe noted in his last email:
> remember, back in perl-land the default behavior is to flatten,
> in raku ... by default [raku is] oriented toward building up
> complex structures like arrays of arrays.
So Raku does *not* flatten the `10..15`, yielding the result you see.
----
You may have been thrown by seeing that, with `@fib[1..5]`, the
range *is* flattened. That's because of the "single argument rule".
This is an important Raku concept.
In brief, in contexts that are inherently "listy", not "scalar", Raku
takes the view that if it's given a *single* thing that is itself listy,
Raku should treat that thing as the list of elements it contains.
Whereas, if it's given a list of things, then it just accepts that list
as is.
Thus `@fib[1..5]` treats the `1..5` as 5 elements, i.e. flattening
the range, but treats `@fib[1, 5, 10..15, 20]` as 4 elements, with
the third being a nested 6 element list that it does *not* flatten.
----
Perhaps you want the whole lot flattened.
Quoting Joe again:
> arrays don't flatten unless you do something to make it happen
One option:
@fib[flat 1, 5, 10..15, 20]
> (1 8 89 144 233 377 610 987 10946)
love, raiph
Thread Previous
|
Thread Next