Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

32: Iterator chains: their syntax and stages

E3201

Comprehension, which iterator chains replaced. [e for i in n] was CTL-2’s comprehension. ShapeScript now writes it as an iterator chain, n |> map(i => e), and the error’s help gives the chain for the comprehension written.

dim D = 4

fn doubled(x: [n D]) -> [n D] = [x[i] * 2.0 for i in n]

Write n |> map(i => x[i] * 2.0), or, iterating the rows themselves, x |> map(row => row * 2.0, over=n). See Iterator chains.

E3202

Lambda outside an iterator stage. A lambda, v => e, says what a map or filter computes for each element. It isn’t a value: it can’t be bound to a name, passed to another function or used in an expression.

dim D = 4

fn doubled(x: [n D]) -> [n D]:
    double = v => v * 2.0
    return x |> map(double, over=n)

Write it in the stage, x |> map(v => v * 2.0, over=n), or declare a function with fn and give its name to the stage. See Functions.

E3203

Range outside an iterator chain or a loop. A range a..b is the indices from a up to b. It can only start an iterator chain, as in 0..n |> map(i => i), or be what a for loop loops over, as in for i in 0..n:; it isn’t a value.

fn f(x: [n]):
    r = 0..n

Start a chain with it, or loop over it. See What a chain iterates.

E3204

zip of iterations of different sizes. zip pairs each element of its input with the element at the same position of its other iteration, so both must have the same number of elements, known from the types.

fn f(w: [n], v: [m]) -> [n] = w |> zip(v) |> map((a, b) => a * b)

Slice the longer one, or zip iterations over the same axis. See Stages.

E3205

Pairs from zip or enumerate used as a value. zip and enumerate give pairs of elements, which only a map or filter with a lambda of two parameters can take apart. A chain can’t end with them, and another stage can’t take them.

fn f(w: [n], v: [n]) -> [n] = w |> zip(v)

Follow them with a stage that takes them apart, as in w |> zip(v) |> map((a, b) => a * b). See Stages.

E3206

Invalid function of map or filter. The function of map or filter is a lambda with a parameter for each part of an element (one, or two for the pairs of zip and enumerate), or a pipe stage, which takes one element: a function’s name, a call or to T. Anything else, a missing function, a lambda with the wrong number of parameters, and a filter whose function doesn’t give a [] mask for each element are this error.

fn f(w: [n], v: [n]) -> [n] = w |> zip(v) |> map(a => a)

Give the lambda a parameter for each part, (a, b) => a, or, for filter, compare the element, as in v => v > 0.0. See Functions.