Sakura Lang

Update list indices with List.set

Reassign the new list returned by `List.set` while preserving earlier versions.

Reassign the new list returned by List.set while preserving earlier versions.

Collections have value semantics. List.set(values, index, value) returns a new list; ignoring the result leaves values unchanged. The loop computes the length once, handles empty input, then uses the half-open range 0..n. Every read and write is in bounds because replacement preserves length. An external index needs both the nonnegative and upper-bound checks, as replace_if_present demonstrates.

The tests check changed values, the unchanged original list, empty and singleton inputs, and external indices below zero, at the length and inside the list. The repository's check also runs main and expects [3, 0, 7, 0]. No effects other than its console output, permissions or slice/concat reconstruction are needed.

Source project

Run it

From the repository root, with this checkout's sakura on PATH:

cd compiler/examples/indexed_update
sakura check --json
sakura run
sakura test --json

Source and tests

src/main.sak includes all required imports and tests.

fn replace_negatives(values: List<Int>) -> List<Int> {
  var updated = values
  let n = List.len(updated)
  if n == 0 { return updated }
  for i in 0..n {
    let value = List.get(updated, i).expect("i is inside the half-open range")
    if value < 0 { updated = List.set(updated, i, 0) }
  }
  updated
}

pub fn main() -> Unit ! {Console} { Console.print("{replace_negatives([3, -2, 7, -1])}") }

test "updates preserve earlier versions" {
  let original = [3, -2, 7, -1]
  expect_eq(replace_negatives(original), [3, 0, 7, 0])
  expect_eq(original, [3, -2, 7, -1])
}

test "empty, singleton and unchanged inputs" {
  expect_eq(replace_negatives([]), [])
  expect_eq(replace_negatives([-1]), [0])
  expect_eq(replace_negatives([0, 4]), [0, 4])
}

fn replace_if_present(values: List<Int>, index: Int, value: Int) -> List<Int> {
  if index >= 0 && index < List.len(values) { List.set(values, index, value) } else { values }
}

test "external indices are checked before replacement" {
  expect_eq(replace_if_present([7], -1, 9), [7])
  expect_eq(replace_if_present([7], 1, 9), [7])
  expect_eq(replace_if_present([], 0, 9), [])
  expect_eq(replace_if_present([7], 0, 9), [9])
}

Open in playground

Expected stdout

[3, 0, 7, 0]

Manifest and permissions

sakura.toml names the package and grants what the program may use.

[package]
name = "indexed_update"
version = "0.1.0"
edition = "2026"

[permissions]

How this recipe is checked

Every project runs offline in the repository's tests (cargo test -p sakura_cli --test examples), against the compiler in the same checkout, on the JavaScript backend. A recipe that talks to an HTTP API or a model replays a cassette in its tests: a committed file of recorded requests and responses. These cassettes are synthetic, so no network access or API key is needed. Running a project needs Node 26, whose built-in SQLite the SQLite recipe uses.

To adapt a recipe, build the CLI from the same checkout and run sakura --version and sakura prompt first. Sakura Lang is pre-1.0: read STABILITY.md before depending on an API. The initial API baseline is compiler revision 5cae349516c21714d90d36e87f1cfd6debe980c6. An installed skill's frontmatter records its compiler version and build revision. These teaching examples are separate from the agent evaluation tasks.

Each page is generated from its example's recipe.md, sakura.toml, src/main.sak and fixture files. Edit those sources, not the rendered pages, then run cargo run -p agent_docs from compiler/ to regenerate the cookbook and the skill reference.

Edit on GitHub

Last updated

On this page