std/core
模块 Zio Core Standard Library · 83 个符号
Auto-loaded at startup. Defines fundamental combinators, sequence operations, and type utilities.
NOTE: loaded via CLI bootstrap, not via require. All code here must work with builtins only (no require).
Basic Combinators
identity
(defun identity [x] …)
(identity x) — returns x unchanged.
constantly
(defun constantly [x] …)
(constantly x) — returns a function that ignores its arguments and always returns x.
inc
(defun inc [x] …)
(inc x) — x plus 1.
dec
(defun dec [x] …)
(dec x) — x minus 1.
Data Structures
defstruct
(defmacro defstruct [name fields] …)
(defstruct name field...) — defines a constructor and accessors. Fields may be given as [x y] or (x y). Expands to (defun name [fields...]) returning {:x x :y y ...} plus (defun name-field [obj]) per field.
assoc
(defun assoc [m & kvs] …)
(assoc m & kvs) — map with each key/value pair from kvs put in. Raises an error when the number of kvs arguments is odd.
set
(defun set [& items] …)
(set & items) → map-backed set; the reader expands #{a b c} to (set a b c). Membership is (get s item nil); conj/disj/merge and friends live in libs/std/persistent.zio.
reduce-kv
(defun reduce-kv [f init m] …)
(reduce-kv f init m) — reduces map m with f taking (acc key val). Iteration order follows (keys m) and is not defined.
get-in
(defun get-in [m ks] …)
(get-in m ks) — value at the nested key path ks, nil if any level is missing.
assoc-in
(defun assoc-in [m ks v] …)
(assoc-in m ks v) — map with v placed at the nested key path ks; missing intermediate levels are created as empty maps.
merge
(defun merge [& maps] …)
(merge & maps) → map — left-to-right overlay of every map, later maps winning. Unlike Clojure, a non-map argument is a typed refusal, not something to skip (ADR-035).
update
(defun update [m k f & args] …)
(update m k f & args) → map — m with k now holding
(apply f (get m k) args). A missing key is a typed refusal — the
same one get raises — not nil handed to f (ADR-035).
update-in
(defun update-in [m ks f & args] …)
(update-in m ks f & args) → map — updates the value at the nested key path ks (a non-empty vector) with f. A missing key anywhere along the path, a non-map at an intermediate level, or a path that is not a non-empty vector is a typed refusal (ADR-035).
contains?
(defun contains? [coll key] …)
(contains? coll key) → boolean — true when a map holds key (a map-backed set holds it as a member) or a vector index is in range; anything else is a typed refusal. Map presence is decided by dissoc shrinking the map, so a stored nil still counts as present.
Logical
not
(defun not [x] …)
(not x) — true when x is falsy, false otherwise.
and
(defun and [& args] …)
(and & args) — false on the first falsy argument, else the last argument; returns true with no arguments. Not short-circuit at the reader level — all arguments are evaluated before the call.
or
(defun or [& args] …)
(or & args) — first truthy argument, else nil; returns nil with no arguments. All arguments are evaluated before the call.
when
(defmacro when [test & body] …)
(when test & body) — evaluates body when test is truthy and returns its last value; nil otherwise. The body is not evaluated at all when the test fails.
unless
(defmacro unless [test & body] …)
(unless test & body) — evaluates body when test is falsy and returns its last value; nil otherwise. The body is not evaluated at all when the test passes.
Sequence
first
(defun first [coll] …)
(first coll) — first element: car of a list, index 0 of a vector; nil for nil, the empty sequence (aligned with Clojure).
(first [1 2 3])
; 预期 1
(first nil)
; 预期 nil
second
(defun second [coll] …)
(second coll) — second element of coll, nil when absent.
rest
(defun rest [coll] …)
(rest coll) — coll without its first element: cdr of a list, otherwise a slice from index 1 to the end; nil for nil (the empty sequence, aligned with Clojure).
(rest [1 2 3])
; 预期 [2 3]
(rest nil)
; 预期 nil
last
(defun last [coll] …)
(last coll) — last element: index count-1 of a vector, otherwise the final element via list traversal; nil for nil and for an empty collection (aligned with Clojure).
(last [1 2 3])
; 预期 3
(last nil)
; 预期 nil
nth
(defun nth [coll n] …)
(nth coll n) — n-th element of coll (0-based); nil when coll is nil, and nil when a LIST has no element at n (the historical walk behavior, kept for compatibility). A vector index beyond the length, or a negative index on any collection, is a typed error carrying the real index and the real length.
(nth [1 2 3] 1)
; 预期 2
(nth [1 2] 5)
; 预期 index out of bounds: index 5 but length is 2
Higher-order
partial
(defun partial [f & args] …)
(partial f & args) — function that calls f with args prepended to the arguments it receives. zio apply spreads its LAST argument, so the prepended args and the call-time args are concatenated into one vector first — a plain list wrap here would hand f the vector itself as a positional argument.
((partial + 1) 2)
; 预期 3
((partial list 1) 2 3)
; 预期 (1 2 3)
every?
(defun every? [f coll] …)
(every? f coll) → boolean — true when f is truthy for every element of coll; an empty collection (or nil) is true.
some
(defun some [f coll] …)
(some f coll) — the first truthy result of f over coll, nil when there is none (an empty collection or nil gives nil).
Predicate
zero?
(defun zero? [x] …)
(zero? x) — true when x equals 0.
pos?
(defun pos? [x] …)
(pos? x) — true when 0 < x.
neg?
(defun neg? [x] …)
(neg? x) — true when x < 0.
even?
(defun even? [x] …)
(even? x) — true when x mod 2 is 0.
odd?
(defun odd? [x] …)
(odd? x) — true when x is not even.
empty?
(defun empty? [coll] …)
(empty? coll) — true when coll is nil or has zero elements.
some?
(defun some? [x] …)
(some? x) — true when x is not nil.
Sequence Operations
take
(defun take [n coll] …)
(take n coll) — the first n elements of coll, in a NEW collection of coll's own kind: vector in → vector out, list in → list out, nil in → nil. This kind rule holds for take, drop and filter alike. n beyond the length stops at the length.
(take 2 [1 2 3])
; 预期 [1 2]
(take 2 '(1 2 3))
; 预期 (1 2)
drop
(defun drop [n coll] …)
(drop n coll) — coll without its first n elements; kind follows the same rule as take (vector in → vector out, list in → list out, nil in → nil); an exhausted list ends at nil, the empty list.
(drop 2 [1 2 3])
; 预期 [3]
(drop 2 '(1 2 3))
; 预期 (3)
filter
(defun filter [pred coll] …)
(filter pred coll) — the elements of coll for which pred is truthy, preserving order, in a collection of coll's own kind: vector in → vector out, list in → list out, nil in → nil (the same kind rule as take/drop). pred is invoked through apply, so a macro value in the pred slot is the same typed refusal the other higher-order natives give, not an expansion.
(filter even? [1 2 3 4])
; 预期 [2 4]
(filter odd? '(1 2 3))
; 预期 (1 3)
range-list
(defun range-list [start & more] …)
(range-list end) or (range-list start end) → list — the range sequence as a list, so car/cdr accept it: the list counterpart of range's vector (ADR-041). Elements are exactly range's and refusals are range's own typed errors.
reverse
(defun reverse [coll] …)
(reverse coll) → vector — the elements of coll (list or vector; nil is empty) in reverse order, in a new vector.
into
(defun into [to from] …)
(into to from) — to with every element of from added. A list to gets from's elements AT ITS END in order (CL append): lists and vectors both work as from, and an empty from (nil, empty list, empty vector) leaves to unchanged. A vector to gets from's elements conjed. Non-sequential to/from are typed rejections.
(into [0] (list 1)) ; 预期 [0 1]
(into (list 0) [1 2]) ; 预期 (0 1 2)
(into () [1 2]) ; 预期 (1 2)
The list branch walks to with first/rest recursion — recursion depth equals to's length on the VM's heap frames, not the native stack — and shares from's structure (CL append semantics).
core--into-from-list
(defun core--into-from-list [from] …)
(core--into-from-list from) → list — from as a list; nil counts as empty. Internal helper for into.
core--into-list
(defun core--into-list [to from] …)
(core--into-list to from) → list — CL append: to with from appended at its end, in order. The base case is empty? (not nil?): an empty list is a real value in Zio, distinct from nil. Internal helper for into.
core--vec-to-list
(defun core--vec-to-list [v i] …)
(core--vec-to-list v i) → list — v's elements from index i on, as a list in order. Internal helper for into.
Zipping and grouping
core--zip-row
(defun core--zip-row [colls i] …)
(core--zip-row colls i) → vector — the i-th element of every collection, left to right. Internal helper for zip.
zip
(defun zip [& colls] …)
(zip & colls) → vector of vectors — each row holds the elements at one index across every collection, stopping at the shortest collection's length.
concat
(defun concat [& colls] …)
(concat & colls) → vector — every element of every collection laid flat into one new vector; nil arguments contribute nothing.
core--flatten-into
(defun core--flatten-into [acc coll] …)
(core--flatten-into acc coll) → vector — acc with every element of coll appended, nested lists/vectors spliced in their place and nil elements skipped. Internal helper for flatten.
flatten
(defun flatten [coll] …)
(flatten coll) → vector — coll with nested lists/vectors spliced flat in place and nil elements skipped (aligned with Clojure); any other element, a string included, is kept as-is.
partition
(defun partition [n coll] …)
(partition n coll) → vector of vectors — consecutive chunks of n elements; the final short chunk is kept. n must be a positive integer.
group-by
(defun group-by [f coll] …)
(group-by f coll) → map — each element under the key (f x); elements sharing a key collect into one vector, in coll's order.
frequencies
(defun frequencies [coll] …)
(frequencies coll) → map — each distinct element mapped to how many times it occurs in coll.
interpose
(defun interpose [sep coll] …)
(interpose sep coll) → vector — coll's elements with sep placed between each adjacent pair.
Threading
core--snoc
(defun core--snoc [coll x] …)
(core--snoc coll x) — coll with x appended in last position. Internal helper for ->>; nil is the empty list here.
->
(defmacro -> [x & forms] …)
(-> x & forms) — thread x as the FIRST argument of each form. (-> p (slot-value :x) inc) → (inc (slot-value p :x)); a bare form is called with x: (-> coll first) → (first coll). No forms → x.
->>
(defmacro ->> [x & forms] …)
(->> x & forms) — thread x as the LAST argument of each form. (->> [1 2 3] (map #'inc) (reduce #'+ 0)); bare forms append x as the sole argument (function values pass as #'name, ADR-044). No forms → x.
Flow macros
core--case-expand
(defun core--case-expand [g clauses] …)
(core--case-expand g clauses) → form — the nested if-chain that compares g against each test value with =, ending at the (else ...) default clause. A dangling test value, or a default clause not written last, is refused. Internal helper for case; runs at expansion time.
case
(defmacro case [expr & clauses] …)
(case e & clauses) — the body of the clause whose test value equals e; test values are data, not evaluated. The optional default is written (else body...) like cond's default clause. No match and no default gives nil.
core--letfn-slots
(defun core--letfn-slots [specs] …)
(core--letfn-slots specs) → vector — a nil placeholder per name. Internal helper for letfn: the parallel let binds them all with no sibling references (every init is a literal nil), then the set! forms below fill these SAME slots in order.
core--letfn-assigns
(defun core--letfn-assigns [specs] …)
(core--letfn-assigns specs) → list of (set! name (fn ...)) forms. Internal helper for letfn.
core--list-append
(defun core--list-append [xs ys] …)
(core--list-append xs ys) → list — ys after every element of xs. Internal helper for letfn (concat builds vectors; the do body needs a list).
letfn
(defmacro letfn [specs & body] …)
(letfn [(f [x] body...) & more-specs] body...) — CL letfn: every name is bound at its function value before any body runs, so the fns may call each other (mutual recursion). Expansion reserves a nil slot per name in ONE parallel let, then assigns each fn value with set! in order — the fn bodies capture that layer's slots, not the inits, which is what makes the mutual references resolve.
as->
(defmacro as-> [expr name & forms] …)
(as-> expr name & forms) — threads expr through the forms with each result bound to name: (as-> 5 x (+ x 1) (* x 2)) reads as (let [x 5] (let [x (+ x 1)] (* x 2))). No forms → expr.
core--cond-thread
(defun core--cond-thread [g clauses thread-first] …)
(core--cond-thread g clauses thread-first) → form — the nested if-chain threading g through each truthy test's form, inserted as the form's first argument when thread-first, else appended last. The tests are evaluated as written, never threaded, and a falsy test skips only its own form — the walk continues. Internal helper for cond-> and cond->>.
cond->
(defmacro cond-> [expr & clauses] …)
(cond-> expr test1 form1 ...) — threads expr through each form whose test (evaluated as written, in order) is truthy, inserted as the form's first argument.
cond->>
(defmacro cond->> [expr & clauses] …)
(cond->> expr test1 form1 ...) — threads expr through each form whose test (evaluated as written, in order) is truthy, appended as the form's last argument.
dotimes
(defmacro dotimes [binding & body] …)
(dotimes [i n] & body) — runs body n times with i bound to 0, 1, ... n-1, as a loop/recur expansion; returns nil. The binding must be a two-element vector.
when-let
(defmacro when-let [binding & body] …)
(when-let [name expr] & body) — evaluates expr once; when the value is non-nil, binds it to name and evaluates body, returning the last body value; nil otherwise. The binding must be a two-element vector; the name slot accepts a destructuring pattern. Unlike Clojure, a false value is non-nil here and so binds (zio's boundary: the check is nil, not truthiness).
(when-let [x 5] (* x 2))
; 预期 10
(when-let [x nil] :ran)
; 预期 nil
if-let
(defmacro if-let [binding & then-else] …)
(if-let [name expr] then else?) — evaluates expr once; non-nil binds it to name and evaluates then; otherwise evaluates else, or nil when the else branch is absent. The binding must be a two-element vector; the name slot accepts a destructuring pattern. Same non-nil boundary as when-let.
(if-let [x 5] :yes :no)
; 预期 :yes
(if-let [x nil] :yes :no)
; 预期 :no
Formatting
core--string-chars
(defun core--string-chars [s] …)
(core--string-chars s) → vector — s as one-character strings. Internal helper for format.
core--format-scan
(defun core--format-scan [chars i] …)
(core--format-scan chars i) → integer — the number of {}
placeholders from index i on, refusing lone braces. Internal
helper for format.
core--format-render
(defun core--format-render [chars i args] …)
(core--format-render chars i args) → vector — the output pieces from index i on, consuming args as placeholders are met. The caller has already matched placeholder and argument counts. Internal helper for format.
format
(defun format [fmt & args] …)
(format fmt & args) → string — fmt with each {} placeholder replaced by (str arg), left to right; {{ and }} render literal braces. A placeholder/argument count mismatch, a lone brace, or a non-string fmt is a typed refusal. Returns the string; nothing is printed.
String joining
core--join-from
(defun core--join-from [items sep] …)
(core--join-from items sep) → string — items joined with sep, each rendered with str. Internal helper for str-join.
str-join
(defun str-join [sep & parts] …)
(str-join sep coll) / (str-join sep item ...) → string — join with sep. A two-argument call whose second argument is a list or vector joins that collection's elements (each rendered with str); any other call shape is variadic over the arguments after sep, the pre-0.3 form, kept for compatibility. A non-string sep is a typed refusal.
(str-join "-" ["a" "b"])
; 预期 "a-b"
(str-join "-" "a" "b")
; 预期 "a-b"
Mutable cells
make-cell
(defun make-cell [v] …)
(make-cell v) → cell — a fresh mutable box holding v. Two cells are never the same unit, even when their contents are equal.
cell-get
(defun cell-get [c] …)
(cell-get c) → the cell's current value. Anything that is not a function is refused by name; telling a non-cell function apart would mean calling it, so that boundary is documented rather than enforced (ADR-041).
cell-set!
(defun cell-set! [c v] …)
(cell-set! c v) → v — writes v into the cell, visible to the next cell-get, and returns it: the same value set! itself yields. Same non-function refusal as cell-get.
Streams (SICP 3.5)
core--memo-proc
(defun core--memo-proc [p] …)
(core--memo-proc p) → zero-argument function — calls p on the first invocation, caches the result, and hands back the cache afterwards. Internal helper behind delay/cons-stream.
delay
(defmacro delay [expr] …)
(delay expr) → promise — a memoized zero-argument function; expr runs on the first (force ...) and its value is reused afterwards.
force
(defun force [p] …)
(force p) → the promise's value, computed exactly once per promise. Forcing a non-function is the call machinery's own not-a-function refusal.
cons-stream
(defmacro cons-stream [a b] …)
(cons-stream a b) → stream — the [a (delay b)] binary table: a is evaluated now, b only when the tail is first forced.
stream-car
(defun stream-car [s] …)
(stream-car s) → the head of stream s. The empty stream (nil) and non-stream values hit car's own typed refusal.
stream-cdr
(defun stream-cdr [s] …)
(stream-cdr s) → the forced tail of s, the next stream cell.