Hi Marc,

My understanding is that ranges are pretty cheap to construct, and in any
case, the range @x[0..*-1] is just the index of all elements in @x. The
.grep() approach may be most useful if you have a function (e.g. %, %%, and
.is-prime shown below):

> (0...9)
(0 1 2 3 4 5 6 7 8 9)
> (0...9)[0..*-1]
(0 1 2 3 4 5 6 7 8 9)
> (0...9)[(0..*-1).grep: * ]
(0 1 2 3 4 5 6 7 8 9)
> (0...9)[(0..*-1).grep: * %% 2 ]
(0 2 4 6 8)
> (0...9)[(0..*-1).grep: * % 2 ]
(1 3 5 7 9)
> (0...9)[(0..*-1).grep: *.is-prime ]
(2 3 5 7)
>

You can find a related example in the docs (
https://docs.raku.org/routine/grep#class_HyperSeq). Anyway, I'm sure each
approach has its fans,

Best, Bill.

On Tue, Aug 24, 2021 at 3:59 AM Marc Chantreux <e...@phear.org> wrote:

> hello everyone,
>
> I made a mistake while replying to all of us so anwsers never reached
> your boxes. I'll summerize in one answer:
>
> Bill:
>
> > Is it just even/odd elements that you want to separate out? If so, maybe
> > .grep() is your friend here
>
> I don't think it is: 0, 2 ... * seems to be
>
> * closer to what i have in mind when i think about the problem
>   (so i invoke readability there)
> * probably more efficient than (0..*).grep(* % 2) that
>   * generate twice the number of required elements
>   * need to filter the result
>
> Also, trying to play with this version:
>
> my ($a,$b) =
>     .[0,2...*],
>     .[1,3...*]
>     with <AaBbCc>.comb;
>
> just don't work because the lists are squashed into scalar context
> in the process.
>
> So Brian and Fernando made my day with := and the unexpected power of
> the [] operator.
>
>     my (@a,@b) := <AaBbCc>.comb[ [0,2...*], [1,3...*] ];
>
> I really like how declarative it is. Also the use of := now seems
> obvious to me.
>
> Sigils still remains something strange to me desprite all your examples
> but i'll take some time. thanks everyone.
>
> marc
>
>

Reply via email to