On this page:
Sequence
..
..=
Sequence.make
Sequence.instantiable
Sequence.instantiate
Sequenceable
8.12

6.39 Sequences🔗ℹ

A sequence supplies elements to a for iteration. Lists, maps, sets, and arrays are sequences, and new kinds of sequences can be defined by calling Sequence.make, Sequence.instantiable, or implementing Sequenceable.

A sequence is more general than a list or stream in that it can have internal state, and the state can even be specific to a particular instantiation of the sequence for a new iteration.

annotation

Sequence

Matches any sequence.

Static information associated by Sequence makes an expression acceptable as a sequence to for in static mode, and it is suitable when a more specialized annotation (such as List or Array) is not available.

expression

n_expr .. m_expr

 

expression

n_expr ..

If n_expr produces an integer n and m_expr (when supplied) produces an integer m, returns a listable sequence containing the integers from n (inclusive) to m (exclusive). If m_expr is not specified, the result is an infinite sequence that contains all integers starting from n.

The .. operator’s precedence is lower than the arithmetic operators +, -, *, and /.

A .. expression has static information that makes it acceptable as a sequence to for in static mode.

expression

n_expr ..= m_expr

Like n_expr .. m_expr, but with an inclusive upper bound.

function

fun Sequence.make(

  ~initial_position:

    init_pos :: Any,

  ~continue_at_position:

    continue_at_pos :: maybe(Function.of_arity(1)) = #false,

  ~position_to_element:

    pos_to_element :: Function.of_arity(1),

  ~continue_at_value:

    continue_at_val :: maybe(Function) = #false,

  ~early_position_to_next:

    early_next_pos :: maybe(Function.of_arity(1)) = #false,

  ~continue_after_position_and_value:

    continue_at_pos_val :: maybe(Function) = #false,

  ~position_to_next:

    next_pos :: Function.of_arity(1)

) :: Sequence

Creates a sequence by supplying the index value and stepper, element-to-index function, and continue conditions:

The arguments to Sequence.make are required to be a mixture of #false and function values, beware that the arguments are not checked. Errors due to invalid or inconsistent values may be detected later.

This same initial position and functions are used for every instantiation of the result sequence. To distinguish different instantiations, use Sequence.instiantiate.

fun even_strings_up_to(n :: Int):

  Sequence.make(

    ~initial_position: 0,

    ~continue_at_position: fun (i): i < n,

    ~position_to_element: to_string,

    ~position_to_next: fun (i): i + 2

  )

> for List (i: even_strings_up_to(5)): i

["0", "2", "4"]

A delaying form of Sequence.make, where thunk is called for every instantiation of the sequence. The given thunk should return the results of Sequence.instantiate.

fun even_strings_up_to(n :: Int):

  Sequence.instantiable(

    fun ():

      let mutable i = 0

      Sequence.instantiate(

        ~initial_position: #void,

        ~continue_at_position: fun (_): i < n,

        ~position_to_element: fun (_): to_string(i),

        ~position_to_next: fun (_): i := i + 2

      ))

> for List (i: even_strings_up_to(5)): i

["0", "2", "4"]

function

fun Sequence.instantiate(

  ~initial_position:

    init_pos :: Any,

  ~continue_at_position:

    continue_at_pos :: maybe(Function.of_arity(1)) = #false,

  ~position_to_element:

    pos_to_element :: Function.of_arity(1),

  ~continue_at_value:

    continue_at_val :: maybe(Function) = #false,

  ~early_position_to_next:

    early_next_pos :: maybe(Function.of_arity(1)) = #false,

  ~continue_after_position_and_value:

    continue_at_pos_val :: maybe(Function) = #false,

  ~position_to_next:

    next_pos :: Function.of_arity(1)

) :: (Any, Any, Any, Any, Any, Any, Any)

Takes arguments of the same form as Sequence.make, but simply returns them as multiple values in an unspecified order. This function is meant to be called from a function passed to Sequence.instantiable.

Provided only in the class space, not the annot or namespace space.

An interface that a class can implement (publicly or privately) to make instances of the class work with as a sequence for forin dynamic mode, but see also sequence for statically optimizing for expansions. The interface has a single abstract method:

class Posn(x, y):

  private implements Sequenceable

  private override method to_sequence():

    [x, y]

> for List (i: Posn(10, 20)): i

[10, 20]