Rules / Types, classes & roles / composite

Seq Reference

An iterable, potentially lazy sequence of values

What it is #

Routines #

Signatures are reproduced from the documentation, including their declared return types. A routine listed here is part of the type's published interface; whether Raku++ implements it is a separate question, answered by the examples below.

method new #

proto method new(Seq: |) {*}
multi method new(Seq: Iterator:D $iter)
multi method new(Seq:)

Creates a new Seq object from the supplied iterator passed as the single argument. Creates an empty Seq if called with no argument.

method iterator #

method iterator(Seq:D:)

If the Seq is not cached, returns the underlying iterator and marks the invocant as consumed. If called on an already consumed sequence, throws an error of type X::Seq::Consumed. Otherwise returns an iterator over the cached list.

method is-lazy #

method is-lazy(Seq:D:)

Returns True if and only if the underlying iterator or cached list considers itself lazy. If called on an already consumed sequence, throws an error of type X::Seq::Consumed.

method Seq #

multi method Seq(Seq:D:)

Clones the object.

method Capture #

method Capture()

Coerces the object to a List, which is in turn coerced into a Capture.

method elems #

method elems(Seq:D:)

Returns the number of values in the sequence. If this number cannot be predicted, the Seq is cached and evaluated till the end. Because an infinite sequence cannot be evaluated till the end, such a sequence should be declared lazy. Calling .elems on a lazy Seq fails with X::Cannot::Lazy.

method from-loop #

multi method from-loop(&body, :$label)
multi method from-loop(&body, &cond, :$repeat!, :$label)
multi method from-loop(&body, &cond, :$label)
multi method from-loop(&body, &cond, &afterwards, :$label)

These methods create new Seq-based callbacks. In general, it produces an infinite Seq by calling &body each time a new element is requested, using the return value from &body as the item. This emulates (or implements) a loop { body } construct. When the multi includes &cond, it's invoked before each call to &body, and terminates the sequence if &cond returns a false value. If $repeat is set to a true

method sink #

method sink(--> Nil)

Returns Nil.

Calls sink-all if it is an Iterator, sink if the Sequence is a list. This is something you might want to do for the side effects of producing those values.

method skip #

multi method skip(Seq:D:)
multi method skip(Seq:D: Whatever)
multi method skip(Seq:D: Callable:D $w)
multi method skip(Seq:D: Int() $n)
multi method skip(Seq:D: $skip, $produce)

Returns a Seq containing whatever is left of the invocant after throwing away $n of the next available values. Negative values of $n count as 0. Also can take a WhateverCode to indicate how many values to skip from the end. Will block on lazy Seqs until the requested number of values have been discarded. Calling it with Whatever will return an empty Seq:

multi method #

method slice(Seq:D: *@indices --> Seq:D)

Returns Seq:D.

Available as of the 2021.02 release of the Rakudo compiler. The slice method takes a number of monotonically increasing indices for which to produce the value from the invocant in a new Seq. Indices can be single numbers or ranges, as long they are increasing in value. This provides a more efficient way of getting specific values out of a Seq than either caching the Seq or converting it to a List or

Examples, run three ways #

Every example below comes from the official documentation, together with the output that documentation asserts. Each was then executed by Rakudo and by Raku++ when this page was built. Where the three agree, one result is shown; where they do not, all three are — because which of them is wrong is exactly the information worth having.

5 all-differ · 3 no-output · 8 ok

class Seq is Cool does Iterable does Sequence { }

Not executed: the documentation states no expected output for this example.

my $s = (1...5);
say $s;              # OUTPUT: «(1 2 3 4 5)␤»
say $s.^name;        # OUTPUT: «Seq␤»
Output
(1 2 3 4 5)
Seq

Documentation, Rakudo and Raku++ all agree.

# The Seq created by gather ... take is consumed on the spot here.
my @a = gather do { say 'consuming...'; take 'one' };  # OUTPUT: «consuming...␤»

# The Seq here is only consumed as we iterate over @a later.
my @a = lazy gather do { say 'consuming...'; take 'one' };  # outputs nothing.
.say for @a;  # OUTPUT: «consuming...␤one␤»
Output
consuming...
consuming...
one

Documentation, Rakudo and Raku++ all agree.

for open('README.md').lines -> $line {
say $line;
}

Not executed: the documentation states no expected output for this example.

my @a = 1, 2, 3;
my @b = <a b c>;
my \c = @a Z=> @b;
.say for c;
.say for c; # fails
CATCH { default { put .^name, ': ', .Str } };
# OUTPUT: «X::Seq::Consumed: This Seq has already been iterated, and its values consumed
# (you might solve this by adding .cache on usages of the Seq, or
# by assigning the Seq into an array)»

Not executed: the documentation states no expected output for this example.

my @fib = 1,1, *+* ... *;
say @fib[^1000] ~~ /^9999/; # OUTPUT: «Nil␤»
Output
Nil

Documentation, Rakudo and Raku++ all agree.

say (1 ... 1000).sink; # OUTPUT: «Nil␤»
Output
Nil

Documentation, Rakudo and Raku++ all agree.

say (1..5).Seq.skip;      # OUTPUT: «(2 3 4 5)␤»
say (1..5).Seq.skip(3);   # OUTPUT: «(4 5)␤»
say (1..5).Seq.skip(5);   # OUTPUT: «()␤»
say (1..5).Seq.skip(-1);  # OUTPUT: «(1 2 3 4 5)␤»
Output
(2 3 4 5)
(4 5)
()
(1 2 3 4 5)

Documentation, Rakudo and Raku++ all agree.

say <1 2 3>.Seq.skip(*);  # OUTPUT: «()␤»
Output
()

Documentation, Rakudo and Raku++ all agree.

say (1..5).Seq.skip(*-3); # OUTPUT: «(3 4 5)␤»
Output
(3 4 5)

Documentation, Rakudo and Raku++ all agree.

say (1..12).Seq.skip(2,3,4); # OUTPUT: «(3 4 5 10 11 12)␤»
Documentation
(3 4 5 10 11 12)
Rakudo
Raku++
(3 4 5 6 7 8 9 10 11 12)

All three differ. Needs a human.

Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.

say (1..10).Seq.skip(2,3,1,2); # OUTPUT: «(3 4 5 7 8)␤»
say (1..10).Seq.skip(2,3,1);   # OUTPUT: «(3 4 5 7 8 9 10)␤»
Documentation
(3 4 5 7 8)
(3 4 5 7 8 9 10)
Rakudo
Raku++
(3 4 5 6 7 8 9 10)
(3 4 5 6 7 8 9 10)

All three differ. Needs a human.

Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.

say (1..10).Seq.skip(2,*);   # OUTPUT: «(3 4 5 6 7 8 9 10)␤»
say (1..10).Seq.skip(2,3,*); # OUTPUT: «(3 4 5)␤»
Documentation
(3 4 5 6 7 8 9 10)
(3 4 5)
Rakudo
Raku++
(3 4 5 6 7 8 9 10)
(3 4 5 6 7 8 9 10)

All three differ. Needs a human.

Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.

say (1..10).Seq.skip(0,3,4); # OUTPUT: «(1 2 3 8 9 10)␤»
Documentation
(1 2 3 8 9 10)
Rakudo
Raku++
(1 2 3 4 5 6 7 8 9 10)

All three differ. Needs a human.

Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.

say (^20).Seq.skip(|(2,3) xx *); # OUTPUT: «(0 1 5 6 10 11 15 16)␤»
Documentation
(0 1 5 6 10 11 15 16)
Rakudo
Raku++
(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19)

All three differ. Needs a human.

Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.

say (1..10).Seq.slice(0, 3..6, 8);  # OUTPUT: «(1 4 5 6 7 9)␤»
Output
(1 4 5 6 7 9)

Documentation, Rakudo and Raku++ all agree.