[] is Coalton’s reader syntax for collection builders and collection
comprehensions.
Syntax
;; finite collection
[]
[⟨expr⟩ ...]
;; collection comprehension
[⟨expr⟩ ⟨clause⟩...]
Semantics
[...]does not denote a specific, single type. It is generic across several collection-like types.[]is an empty collection builder.- Collection builders and comprehensions must be finite.
- Keys must all have the same type, and values must all have the same type.
- Without a more specific expected type, association builders default to
Seq :t. - Collection builders, especially empty ones, often need
theto fix the intended key and value types.
Options
:for ⟨var⟩ :below ⟨ufix⟩iterates from0up to but not including the bound.:for ⟨var⟩ :in ⟨iter⟩iterates over an iterator.:with ⟨var⟩ = ⟨expr⟩introduces a lexical binding for the remaining clauses.:when ⟨expr⟩guards emitted entries.- New types can opt-in to this syntax by defining instance of
FromItemizedCollectionandFromCollectionComprehension.
Example
(define evens
[x :for x :below 10 :when (even? x)])
(define xs
(the (List F32) [1 2 3]))