[] is reader syntax for finite collection builders and collection comprehensions, overloaded across collection types.
Syntax
;; Empty collection builder
[]
;; Itemized collection builder
[⟨elem-expr⟩+]
;; Collection comprehension
[⟨head-expr⟩ ⟨clause⟩+]
;; ⟨clause⟩ := :for ⟨var-or-_⟩ :below ⟨bound-expr⟩
;; | :for ⟨var-or-_⟩ :in ⟨iterable-expr⟩
;; | :with ⟨var-or-_⟩ = ⟨expr⟩
;; | :when ⟨boolean-expr⟩
Semantics
[...]is polymorphic over any collection type:cwith element type:e:- Itemized builders
[e1 ... en](including[]) desugar to theFromItemizedCollection :c :e :stypeclass protocol (begin-collection-builder,adjoin-to-collection-builder,finalize-collection-builder), passing the exact element countnup front. - Comprehensions
[head ⟨clause⟩+]desugar to theFromCollectionComprehension :c :e :stypeclass protocol (begin-collection-comprehension,adjoin-to-collection-comprehension,finalize-collection-comprehension). When the first clause is:for ⟨var⟩ :in ⟨iter⟩, the iterator’ssize-hint(Optional UFix) is passed tobegin-collection-comprehension.
- Itemized builders
- All element expressions in an itemized builder must unify to the same element type
:eand are evaluated left-to-right in index order. - Comprehension clauses are nested left-to-right from outermost to innermost:
:for ⟨var-or-_⟩ :below ⟨bound-expr⟩evaluates⟨bound-expr⟩once as an upper bound and iterates⟨var-or-_⟩from0by1+while(< i bound)(for any type implementingNumandOrd).:for ⟨var-or-_⟩ :in ⟨iterable-expr⟩converts⟨iterable-expr⟩viacoalton/iterator:into-iter(IntoIterator) and folds over its elements.:with ⟨var-or-_⟩ = ⟨expr⟩binds⟨var-or-_⟩to⟨expr⟩for the remaining clauses and⟨head-expr⟩.:when ⟨boolean-expr⟩filters iterations: subsequent clauses and⟨head-expr⟩run only when⟨boolean-expr⟩evaluates toTrue.
- Type defaulting: If the surrounding context does not determine a specific collection type
:c, Coalton defaults:cto(Seq :e)while still inferring the element type:enormally. Empty builders[]whose element type is also unconstrained often needthe(e.g.,(the (Vector Integer) [])).
Examples
;; Itemized collection with type annotation
(coalton (the (List Integer) [1 2 3 4]))
; => (1 2 3 4)
;; Comprehension with multiple generators, `:with`, and `:when`
(coalton
(the (Vector (Tuple UFix UFix))
[(Tuple x y)
:for x :below 4
:for y :below 4
:with sum = (+ x y)
:when (even? sum)]))
; => #(#.(TUPLE 0 0) #.(TUPLE 0 2) #.(TUPLE 1 1) #.(TUPLE 1 3)
; #.(TUPLE 2 0) #.(TUPLE 2 2) #.(TUPLE 3 1) #.(TUPLE 3 3))
;; Defaulting to `Seq` when consumed by a generic `Foldable` operation
(coalton (fold + 0 [10 20 30]))
; => 60
Other notes
- Standard library types supporting
[]areSeq,List,Vector,Queue, andLispArray. (For key-value maps and associations, use[=>].) - User-defined collection types can opt into
[]syntax by implementingFromItemizedCollectionandFromCollectionComprehensionfromcoalton/classes.