(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-mapprocedureApply 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-filterprocedureReturn 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-mergeprocedureMerge 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-updateprocedureUpdate 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-selectprocedureSelect 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-rejectprocedureRemove 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-inprocedureGet 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-founddict-update-inprocedureUpdate 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->alistprocedureConvert 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->dictprocedureConvert 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 '())
; => #{}