blob: 84ffbfeeb061214347505b24300c670dbb8f68af (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
;; SPDX-License-Identifier: EUPL-1.2
;; SPDX-FileCopyrightText: 2025 Uko Kokņevičs <perkontevs@gmail.com>
(defpackage :ukkoclot/hash-tables
(:documentation "Hash-table-oriented utilities.")
(:use :c2cl)
(:import-from :alexandria :with-gensyms)
(:export :alist->hash-table :gethash-lazy :plist->hash-table))
(in-package :ukkoclot/hash-tables)
(defun alist->hash-table (alist &rest args &key &allow-other-keys)
"Turn an association list into a hash table.
All key arguments are passed on to `make-hash-table'."
(let ((ht (apply #'make-hash-table args)))
(loop for (key . value) in alist do
(setf (gethash key ht) value))
ht))
(defmacro gethash-lazy (key hash-table default-lazy)
"`gethash' alternative with lazily evaluated default value."
(with-gensyms (res unique)
`(let* ((,unique ',unique)
(,res (gethash ,key ,hash-table ,unique)))
(if (eq ,res ,unique)
,default-lazy
,res))))
(defun plist->hash-table (plist &rest args &key &allow-other-keys)
"Turn a property list into a hash table.
All key arguments are passed on to `make-hash-table'."
(let ((ht (apply #'make-hash-table args)))
(loop for (key value) on plist by #'cddr do
(setf (gethash key ht) value))
ht))
|