Map operations for PTC-Lisp runtime.
Provides get, assoc, update, merge, and other map manipulation functions.
Summary
Functions
Associate key-value pairs with a map.
Remove keys from a map.
Convert map to a list of [key, value] pairs, sorted by key.
Returns the key from a map entry (2-element vector).
Update a nested value in a map by applying a function.
Apply a function to each value in a map, returning a new map with the same keys.
Matches Clojure 1.11's update-vals signature: (update-vals m f)
Update a value in a map by applying a function.
Returns the value from a map entry (2-element vector).
Functions
Associate key-value pairs with a map.
Supports both standard 3-arg form and variadic form with multiple pairs:
- (assoc m k v)
- (assoc m k1 v1 k2 v2 k3 v3)
Examples
iex> PtcRunner.Lisp.Runtime.MapOps.assoc_variadic([%{a: 1}, :b, 2])
%{a: 1, b: 2}
iex> PtcRunner.Lisp.Runtime.MapOps.assoc_variadic([%{}, :a, 1, :b, 2, :c, 3])
%{a: 1, b: 2, c: 3}
Remove keys from a map.
Supports both 2-arg form and variadic form with multiple keys:
- (dissoc m k)
- (dissoc m k1 k2 k3)
Examples
iex> PtcRunner.Lisp.Runtime.MapOps.dissoc_variadic([%{a: 1, b: 2}, :a])
%{b: 2}
iex> PtcRunner.Lisp.Runtime.MapOps.dissoc_variadic([%{a: 1, b: 2, c: 3}, :a, :c])
%{b: 2}
Convert map to a list of [key, value] pairs, sorted by key.
Returns the key from a map entry (2-element vector).
Examples
iex> PtcRunner.Lisp.Runtime.MapOps.key([:a, 1])
:a
Update a nested value in a map by applying a function.
Supports Clojure-style extra arguments that are passed to the function:
- (update-in m path f) - calls (f old-val)
- (update-in m path f arg1) - calls (f old-val arg1)
Examples
iex> PtcRunner.Lisp.Runtime.MapOps.update_in_variadic([%{a: %{b: 1}}, [:a, :b], &Kernel.+/2, 5])
%{a: %{b: 6}}
Apply a function to each value in a map, returning a new map with the same keys.
Matches Clojure 1.11's update-vals signature: (update-vals m f)
Examples
iex> PtcRunner.Lisp.Runtime.MapOps.update_vals(%{a: [1, 2], b: [3]}, &length/1)
%{a: 2, b: 1}
iex> PtcRunner.Lisp.Runtime.MapOps.update_vals(%{}, &length/1)
%{}
Update a value in a map by applying a function.
Supports Clojure-style extra arguments that are passed to the function:
- (update m k f) - calls (f old-val)
- (update m k f arg1) - calls (f old-val arg1)
- (update m k f arg1 arg2) - calls (f old-val arg1 arg2)
Examples
iex> PtcRunner.Lisp.Runtime.MapOps.update_variadic([%{n: 1}, :n, &Kernel.+/2, 5])
%{n: 6}
iex> PtcRunner.Lisp.Runtime.MapOps.update_variadic([%{n: nil}, :n, &PtcRunner.Lisp.Runtime.Predicates.fnil(&Kernel.+/2, 0), 5])
%{n: 5}
Returns the value from a map entry (2-element vector).
Examples
iex> PtcRunner.Lisp.Runtime.MapOps.val([:a, 1])
1