Decision D036 · accepted
Ranges, slices, iterators and generators
A record of the language's design, as it stood on 2026-11-18: written for the people who build Diaspar, and kept as it was written. Its code is a sketch, not a program of the site, and an older record may show syntax that a later one changed. It names files of the language's source, which is not public yet.
Status: accepted (2026-11-17)
Ranges are values
range gives a Range, a value of three integers that takes constant space, not a list; it may be infinite. The end is excluded, as in Rust and Python.
(range 5) ; 0, 1, 2, 3, 4
(range 1 10) ; 1 .. 9
(range 1 10 step: 2)
(range 1 10 inclusive: true) ; 1 .. 10, Rust's 1..=10
(range from: 1) ; 1, 2, 3 … without end
(contains? (range 1 10) 3) ; true, in constant time
(count (range 1 10)) ; 9; an infinite range has no count, which raises
(for [i (range 0 n)] …) ; walks it without building a list
(vec (range 3)) ; #[0 1 2] when a vector is wanted
- A step of 0 raises
BadArgument; a negative step counts down,(range 10 0 step: -2). - No literal such as
1..10: the dot already means member access (D029), soa..bwould need a rule of its own, and(range 1 10)is hardly longer. Every Lisp writes ranges as calls (Clojure'srange, Racket'sin-range).
Slices
(slice v 1) ; Rust's v[1..]
(slice v 1 3) ; v[1..3]
(slice v to: 3) ; v[..3]
(slice s 2 5) ; a string, by graphemes (D013)
(take-last 2 v) ; from the end, by name
slice takes vectors, lists and strings; an index outside the value raises BadArgument with the value's length. subvec and subs go: slice is the one name.
No negative indices
(nth v -1) and (slice v -2) raise. In Python xs[i - 1] with i = 0 quietly gives the last element instead of an error; Rust forbids negative indices for that reason. The end of a value is named: peek, last, take-last, drop-last.
Iterators
Two protocols, as Rust's IntoIterator and Iterator:
(defprotocol Iterable
(iter [coll])) ; an iterator over coll
(defprotocol Iterator
(next [it])) ; (Step.Next value: x rest: it2) or Step.Done (D010 §3c)
nextreturns the rest, it does not change the iterator: values are immutable. Perceus updates a unique iterator in place (D004), so a walk costs what Rust's&mutiterator costs.- The end is an enum, not
nil: elements may benil, sonilcannot mean the end, the mistake Rust'sOptioncorrects. - Vectors, lists, strings (by graphemes), maps, sets and ranges are
Iterable. So is anything a program or the standard library defines: the lines of a file, the entries of a directory, the results of a query. - Resources: an iterator that holds a resource (a file) implements
Release(D023), so abreakor an error in the middle of a walk releases it; Elixir'sEnumerableneeds a:haltmessage for this. for,reduce,into,loop … over:and the other sequence functions take anyIterableand stay eager (D009): they walk to the end, or to abreak, and build what they build.- Laziness is explicit: lazy adapters live in
std.iter(iter.map,iter.filter,iter.take,iter.zip…), each returning an iterator, as Rust's adapters do. Nothing in the prelude is lazy, which avoids Clojure's lazy sequences that hold their head or realize in chunks.
(for [line (fs.lines "big.log") when: (str-includes? line "ERROR")] ; reads lazily
line)
(use std.iter)
(-> (range from: 1)
(iter.map square)
(iter.filter even?)
(iter.take 5)
vec) ; #[4 16 36 64 100]
Generators (accepted, 2026-11-17; implemented with fibers, stage D)
(gen body …) is an expression that gives an iterator; yield is an effect, (defeffect Yield [value]), so it works in any function called while the body runs.
(defn numbers [from]
(gen (loop [i from]
(yield i)
(continue (inc i)))))
(defn walk-tree [node] ; an ordinary function that yields
(yield node.value)
(for [c node.children] (walk-tree c)))
(for [v (gen (walk-tree root))] …)
- Inside: the body runs on a fiber of its own under a handler of
Yield(D005 kind 3).nextswitches to that fiber until the nextyield(aStep.Next) or the end of the body (Step.Done). - A generator is a resource: dropped in the middle of a walk (a
break), it is released, and its fiber unwinds, running itsdefers (D023). yieldoutside anygenis an unhandled effect: "yield outside a generator".- Functions stay functions. Rejected: Python's rule that a
yieldin a body makes the function a generator (one word changes what the whole function is, a hidden colouring, D006; JavaScript marks such functions withfunction*for that reason); adefgenform (a one-line macro overdefnandgen, not worth a word). Becauseyieldis an effect and not syntax, a recursive walk needs noyield fromat each level, unlike Python.
Rejected alternatives
| Alternative | Where | Why not |
|---|---|---|
A range literal 1..10 |
Rust, Ruby, Kotlin, Swift, Elixir | the dot is member access here |
| Negative indices | Python, Ruby, Elixir | i - 1 at 0 is a silent bug |
nil at the end of a walk |
Clojure's seq, Lua |
elements may be nil |
| Lazy sequences by default | Clojure, Haskell | holding the head, chunked realization, effects run late |
| An iterator that changes in place | Rust, Python | values are immutable; Perceus gives the same cost |