http://qs321.pair.com?node_id=11133549


in reply to [Raku] Ranges in array slices

You can apply a sequence here:
> my @c = 'a'..'z'; @c[6...9, 23, 25].map( { qq[<$_>] } ) (<g> <h> <i> <j> <x> <z>)
It has the advantage of working downwards also:
> my @c = 'a'..'z'; @c[9...6, 23, 25].map( { qq[<$_>] } ) (<j> <i> <h> <g> <x> <z>)
You have to be careful with this by ranges:
> my @c = 'a'..'z'; @c[9..6, 23, 25].map( { qq[<$_>] } ) (<> <x> <z>)

Replies are listed 'Best First'.
Re^2: [Raku] Ranges in array slices
by Athanasius (Archbishop) on Jun 05, 2021 at 14:24 UTC

    Thanks tomasz, and a belated welcome to the Monastery!

    The sequence operator looks really powerful: my attention was particularly caught by this example from the documentation:

    This allows you to write
    say 1, 1, * + * ...^ *>= 100;
    # OUTPUT: (1 1 2 3 5 8 13 21 34 55 89)␤
    
    to generate all Fibonacci numbers up to but excluding 100.

    Of course, now that I have an operator that works the way I expected the range operator to work, I’m wondering: why would I ever need the range operator?

    Cheers,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Functionally you can express all Ranges as Sequences, albeit that'd be slower methinks. What you're asking is a bit like asking why we need multiplication if we can just keep adding.

      Check out the documentation. Range, just like List does Positional and Iterable roles. Seq only does Iterable and implements the indexing (AT-POS method et. al) itself, specifically a Seq always has to calculate all n-1 elements before you can access the n-th element.


      holli

      You can lead your users to water, but alas, you cannot drown them.
      Hi there!

      I'm not so sure, to be honest. I'm still very fresh with Raku.

      From what I've gathered so far, you'll take range when you just need two ends of a stick, and sequence, when you need the stick.

      EDIT:
      Sequence can also be characterised by the accented stickiness of the stick, to continue this sticky metaphor. It's accented even over the idea of its end, to the effect that if an end of the stick doesn't meet the stickiness, then it's ignored. Example:
      0, * + 2 ... 1
      See, where it goes?

      I wonder if it's grammatically possible to jump over both ends this way... (sic!)1

      1) ...like this...
        I'm not sure what you're trying to say. This sequence (of even numbers) however will never end.

        0,  * + 2  ... 1

        If that's what you want to say, then it better be written as

        0, 2 ... * or
        0, 2 ... Inf


        holli

        You can lead your users to water, but alas, you cannot drown them.