PtcRunner.Lisp.Runtime.MapOps (PtcRunner v0.9.0)

Copy Markdown View Source

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

assoc(m, k, v)

assoc_in(m, path, v)

assoc_variadic(args)

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}

dissoc(m, k)

dissoc_variadic(list)

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}

entries(m)

Convert map to a list of [key, value] pairs, sorted by key.

get(m, k)

get(m, k, default)

get_in(m, path)

get_in(m, path, default)

key(list)

Returns the key from a map entry (2-element vector).

Examples

iex> PtcRunner.Lisp.Runtime.MapOps.key([:a, 1])
:a

keys(m)

merge(m1, m2)

select_keys(m, ks)

update(m, k, f)

update_in(m, path, f)

update_in_variadic(list)

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}}

update_vals(m, f)

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_variadic(list)

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}

val(list)

Returns the value from a map entry (2-element vector).

Examples

iex> PtcRunner.Lisp.Runtime.MapOps.val([:a, 1])
1

vals(m)