Sequences & Maps API Reference¶
Detailed API documentation for Epsilon's core functional collections: sequences and maps.
Maps (epsilon.lib.map
)¶
Immutable key-value associations implemented as Hash Array Mapped Tries (HAMT).
Construction¶
(make-map &rest key-value-pairs)
¶
Create a new map from alternating keys and values.
(map:make-map :a 1 :b 2 :c 3)
;=> {:a 1, :b 2, :c 3}
(map:make-map "name" "Epsilon" "version" 1.0)
;=> {"name" "Epsilon", "version" 1.0}
(empty)
¶
Return an empty map.
(from-pairs pairs)
¶
Create a map from a sequence of key-value pairs.
Basic Operations¶
(get map key &optional default)
¶
Retrieve a value by key, returning default
if not found.
(assoc map key value &rest more-kvs)
¶
Return a new map with key(s) associated to value(s).
(map:assoc {:a 1} :b 2)
;=> {:a 1, :b 2}
(map:assoc {:a 1} :b 2 :c 3 :d 4)
;=> {:a 1, :b 2, :c 3, :d 4}
(dissoc map &rest keys)
¶
Return a new map with key(s) removed.
(contains-p map key)
¶
Test if map contains a key.
Nested Operations¶
(get-in map path &optional default)
¶
Get a value from a nested map structure.
(let ((nested {:user {:profile {:name "Alice" :age 30}}}))
(map:get-in nested '(:user :profile :name)))
;=> "Alice"
(map:get-in nested '(:user :settings :theme) "dark")
;=> "dark"
(assoc-in map path value)
¶
Associate a value in a nested map structure.
(let ((nested {:user {:profile {:name "Alice"}}}))
(map:assoc-in nested '(:user :profile :age) 30))
;=> {:user {:profile {:name "Alice", :age 30}}}
(update-in map path function &rest args)
¶
Update a value in a nested map structure by applying a function.
(let ((nested {:counters {:clicks 5 :views 10}}))
(map:update-in nested '(:counters :clicks) #'1+))
;=> {:counters {:clicks 6, :views 10}}
Collection Operations¶
(merge &rest maps)
¶
Merge multiple maps, with rightmost values taking precedence.
(select-keys map keys)
¶
Return a new map with only the specified keys.
(keys map)
¶
Return a sequence of all keys in the map.
(vals map)
¶
Return a sequence of all values in the map.
(seq map)
¶
Return a sequence of key-value pairs.
Functional Transformations¶
(map function map)
¶
Transform values using a function that takes key and value.
(filter predicate map)
¶
Return a new map with entries that satisfy the predicate.
(reduce function initial-value map)
¶
Reduce over key-value pairs.
Queries¶
(count map)
¶
Return the number of key-value pairs.
(empty-p map)
¶
Test if the map is empty.
Sequences (epsilon.lib.sequence
)¶
Lazy, functional sequences with efficient operations.
Construction¶
(seq &rest elements)
¶
Create a sequence from elements.
(from-list list)
¶
Create a sequence from a list.
(from-vector vector)
¶
Create a sequence from a vector.
(empty)
¶
Return an empty sequence.
Basic Operations¶
(first sequence)
¶
Return the first element of the sequence.
(rest sequence)
¶
Return a new sequence with all elements except the first.
(cons element sequence)
¶
Return a new sequence with element prepended.
(empty-p sequence)
¶
Test if the sequence is empty.
Functional Operations¶
(map function sequence)
¶
Apply function to each element, returning a new lazy sequence.
(seq:map #'1+ (seq:seq 1 2 3 4))
;=> <sequence: (2 3 4 5)>
;; Lazy evaluation - function not called until realized
(seq:map #'expensive-computation (seq:seq 1 2 3))
(filter predicate sequence)
¶
Return a lazy sequence of elements satisfying the predicate.
(reduce function initial-value sequence)
¶
Reduce the sequence using function and initial value.
(seq:reduce #'+ 0 (seq:seq 1 2 3 4 5))
;=> 15
(seq:reduce #'max most-negative-fixnum (seq:seq 3 1 4 1 5))
;=> 5
Sequence Manipulation¶
(take n sequence)
¶
Return a lazy sequence of the first n elements.
(drop n sequence)
¶
Return a lazy sequence with the first n elements removed.
(take-while predicate sequence)
¶
Take elements while predicate returns true.
(drop-while predicate sequence)
¶
Drop elements while predicate returns true.
Advanced Operations¶
(group-by function sequence)
¶
Group elements by the result of applying function.
(partition-when predicate sequence)
¶
Partition sequence when predicate returns true.
(iterate function initial-value)
¶
Generate an infinite sequence by repeatedly applying function.
(seq:realize (seq:take 5 (seq:iterate #'1+ 0)))
;=> (0 1 2 3 4)
(seq:realize (seq:take 5 (seq:iterate (lambda (x) (* x 2)) 1)))
;=> (1 2 4 8 16)
(cycle sequence)
¶
Create an infinite sequence by repeating the input sequence.
Realization¶
(realize sequence)
¶
Force evaluation of a lazy sequence, returning a list.
(realize-to-vector sequence)
¶
Force evaluation to a vector.
Integration Examples¶
Processing Data Pipelines¶
;; Process user data with maps and sequences
(defun process-users (user-data)
(->> user-data
(seq:from-list)
(seq:map (lambda (user)
(map:update-in user '(:profile :age) #'calculate-age)))
(seq:filter (lambda (user)
(> (map:get-in user '(:profile :age)) 18)))
(seq:group-by (lambda (user)
(map:get-in user '(:profile :country))))
(map:map #'seq:realize)))
Configuration Management¶
;; Merge configuration with environment overrides
(defun load-config (base-config env-overrides)
(let ((config (map:merge base-config env-overrides)))
(map:update-in config '(:database :url)
(lambda (url)
(if (map:get config :debug)
(concatenate 'string url "?debug=true")
url)))))
Lazy Data Processing¶
;; Process large datasets efficiently
(defun analyze-log-files (file-paths)
(->> file-paths
(seq:from-list)
(seq:map #'read-log-file) ; Lazy file reading
(seq:mapcat #'parse-log-lines) ; Flatten log lines
(seq:filter #'error-line-p) ; Only error lines
(seq:group-by #'extract-error-type)
(map:map (lambda (k v)
{:count (seq:count v)
:examples (seq:realize (seq:take 5 v))}))))
Next: JSON & YAML - Data serialization and parsing