summaryrefslogtreecommitdiff
path: root/src/hash-tables.lisp
blob: 429038746c5aeed8eed987234069e07bcbff22cb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
;; 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 :gethash-lazy))
(in-package :ukkoclot/hash-tables)

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