sigildocs

(sigil dict)

(sigil dict) - Dict Operations

Extended dict operations for transforming, selecting, and converting dicts.

Transformations

(import (sigil dict))

(dict-map (lambda (k v) (cons k (* v 2))) #{ a: 1 b: 2 })
; => #{ a: 2 b: 4 }

(dict-filter (lambda (k v) (> v 1)) #{ a: 1 b: 2 c: 3 })
; => #{ b: 2 c: 3 }

(dict-merge #{ a: 1 } #{ b: 2 })
; => #{ a: 1 b: 2 }

Selection

(dict-select #{ a: 1 b: 2 c: 3 } '(a: c:))
; => #{ a: 1 c: 3 }

(dict-reject #{ a: 1 b: 2 c: 3 } '(b:))
; => #{ a: 1 c: 3 }

Nested Access

(dict-get-in #{ a: #{ b: #{ c: 42 }}} '(a: b: c:))
; => 42

(dict-update-in #{ a: #{ b: 0 }} '(a: b:) (lambda (n) (+ n 1)))
; => #{ a: #{ b: 1 }}

Conversions

(dict->alist #{ a: 1 b: 2 })
; => ((a . 1) (b . 2))

(alist->dict '((a . 1) (b . 2)))
; => #{ a: 1 b: 2 }

Exports

dict-mapprocedure

Apply a procedure to each key-value pair in a dict, returning a new dict.

The procedure receives two arguments: the key and value. It should return a cons pair of (new-key . new-value).

(dict-map (lambda (k v) (cons k (* v 2))) #{ a: 1 b: 2 })
; => #{ a: 2 b: 4 }

;; Transform keys
(dict-map (lambda (k v) (cons (string->keyword (string-upcase (keyword->string k))) v))
          #{ name: "alice" })
; => #{ NAME: "alice" }
dict-filterprocedure

Return a new dict containing only entries that satisfy a predicate.

The predicate receives two arguments: the key and value.

(dict-filter (lambda (k v) (> v 1)) #{ a: 1 b: 2 c: 3 })
; => #{ b: 2 c: 3 }

(dict-filter (lambda (k v) (string? v)) #{ name: "alice" age: 30 })
; => #{ name: "alice" }
dict-mergeprocedure

Merge two dicts, with the second dict's values taking precedence.

(dict-merge #{ a: 1 b: 2 } #{ b: 3 c: 4 })
; => #{ a: 1 b: 3 c: 4 }
dict-updateprocedure

Update a dict key by applying a function to its current value.

If the key exists, calls (f current-value) and sets the result. If the key doesn't exist, calls (f default) where default is #f unless a third argument is provided.

(dict-update #{ count: 0 } count: (lambda (n) (+ n 1)))
; => #{ count: 1 }

(dict-update #{} count: (lambda (n) (+ n 1)) 0)
; => #{ count: 1 }
dict-selectprocedure

Select a subset of keys from a dict.

Keys can be provided as keywords or symbols.

(dict-select #{ a: 1 b: 2 c: 3 } '(a: c:))
; => #{ a: 1 c: 3 }

(dict-select #{ name: "alice" age: 30 } '(name:))
; => #{ name: "alice" }
dict-rejectprocedure

Remove a subset of keys from a dict.

Keys can be provided as keywords or symbols.

(dict-reject #{ a: 1 b: 2 c: 3 } '(b:))
; => #{ a: 1 c: 3 }

(dict-reject #{ name: "alice" age: 30 password: "secret" } '(password:))
; => #{ name: "alice" age: 30 }
dict-get-inprocedure

Get a nested value from a dict using a path of keys.

Returns the value at the path, or #f (or default if provided) if any key in the path is missing.

(dict-get-in #{ a: #{ b: #{ c: 42 }}} '(a: b: c:))
; => 42

(dict-get-in #{ a: #{ b: 1 }} '(a: x:))
; => #f

(dict-get-in #{ a: #{ b: 1 }} '(a: x:) 'not-found)
; => not-found

Update a nested value in a dict by applying a function.

Traverses the path, creating empty dicts as needed, and applies f to the value at the path (or default if not present).

(dict-update-in #{ a: #{ b: 0 }} '(a: b:) (lambda (n) (+ n 1)))
; => #{ a: #{ b: 1 }}

(dict-update-in #{} '(a: b:) (lambda (n) (+ n 1)) 0)
; => #{ a: #{ b: 1 }}
dict->alistprocedure

Convert a dict to an association list.

Converts keyword keys to symbols for compatibility with traditional Scheme alist operations.

(dict->alist #{ a: 1 b: 2 })
; => ((a . 1) (b . 2))

(dict->alist #{})
; => ()
alist->dictprocedure

Convert an association list to a dict.

Converts symbol or string keys to keywords.

(alist->dict '((a . 1) (b . 2)))
; => #{ a: 1 b: 2 }

(alist->dict '())
; => #{}