zip
On this page

zip

Function

Zip combines corresponding elements from two equal-length arrays.

Compare examples

Each output element multiplies the two input elements at the same index. A zero in either input makes that product zero.

Model assumptionsxs = array(1, 2, 3)

Example 1zip(xs, array(4, 5, 6), (a, b) => a * b)
41018
Components in this example
Result
[4,10,18]
Example 2zip(xs, array(0, 0, 0), (a, b) => a * b)
000
Components in this example
Result
[0,0,0]
Example 3zip(xs, array(-1, -1, -1), (a, b) => a * b)
-1-2-3
Components in this example
Result
[-1,-2,-3]

The examples are calculation models and do not run GPU work. The reference below gives the complete call signatures. Decimal results are rounded for display. The stated assumptions determine each example; actual shader results follow the target precision and execution rules.

When to use it

Use it for pairing samples with weights or computing pairwise products.

Inputs and limits

Both arrays must have the same fixed length. The combining function decides the output element type.

For shader input rules and precision, consult WGSL and GLSL ES 3.00.

The signature, the sentence and any message are the compiler's own text at commit af54ddad5.

A fixed-size array built from two of one length, element by element: zip(xs, ys, (a, b) => a * b).

Syntax

declare function zip<A, B, R, N extends number>(
xs: array<A, N>,
ys: array<B, N>,
f: (a: A, b: B) => R,
): array<R, N>

A number in a parameter or a return position is the scalar slot. The ambient file declares a GPU scalar as a branded number whose brand is optional, so a literal and any branded scalar both fill it.

type f32 = number & { readonly [f32Tag]?: true }

A number you write as a type is inferred from its uses, and f32, i32 and u32 fix a width; the TypeScript constructs page carries that row with the compiler's emit.

Parameters

  • xs array<A, N>
  • ys array<B, N>
  • f (a: A, b: B) => R

Return value

array<R, N>

Description

Unrolled into one call per element; the function may read the variables around it.

See also

Edit this page Report a problem