Nex

Arrays and Structs

Element-wise array operations that fuse to one loop, and a small struct with a function over it.

Element-wise array operations (the fusion sweet spot)

For arrays \(a\) and \(b\) of the same length, an element-wise expression like

\[r_i \;=\; 2 a_i + b_i - 1 \qquad i = 0, \ldots, n-1\]

compiles to a single loop — no temporary array for 2a, no separate + b, no separate - 1.0. The whole expression is the fusion unit.

def main() =
  val a = [1.0, 2.0, 3.0, 4.0, 5.0]
  val b = [10.0, 20.0, 30.0, 40.0, 50.0]

  // Fuses to a single loop, no intermediate arrays:
  //   for i in 0..5: result[i] = 2 * a[i] + b[i] - 1.0
  val result = 2a + b - 1.0

  print(result)         // [11.0, 23.0, 35.0, 47.0, 59.0]

A struct and a function over it

A Point is a record of two reals, and distance is the Euclidean norm of the difference,

\[d(p_1, p_2) \;=\; \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}.\]

The function reads back as one line per term in the formula.

struct Point
  x: real
  y: real
end

def distance(p1: Point, p2: Point) =
  val dx = p1.x - p2.x
  val dy = p1.y - p2.y
  sqrt(dx^2 + dy^2)

def main() =
  val origin = Point(0.0, 0.0)
  val p = Point(3.0, 4.0)
  print(distance(origin, p))        // 5.0

Search

Esc
to navigate to open Esc to close