Skip tests for json.c unless compiled with native JSON support.
[emacs.git] / lisp / emacs-lisp / thunk.el
blob895fa86722d0d1abd48ead502b33a804a91b65e7
1 ;;; thunk.el --- Lazy form evaluation -*- lexical-binding: t -*-
3 ;; Copyright (C) 2015-2017 Free Software Foundation, Inc.
5 ;; Author: Nicolas Petton <nicolas@petton.fr>
6 ;; Keywords: sequences
7 ;; Version: 1.0
8 ;; Package: thunk
10 ;; Maintainer: emacs-devel@gnu.org
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
27 ;;; Commentary:
29 ;; Thunk provides functions and macros to delay the evaluation of
30 ;; forms.
32 ;; Use `thunk-delay' to delay the evaluation of a form (requires
33 ;; lexical-binding), and `thunk-force' to evaluate it. The result of
34 ;; the evaluation is cached, and only happens once.
36 ;; Here is an example of a form which evaluation is delayed:
38 ;; (setq delayed (thunk-delay (message "this message is delayed")))
40 ;; `delayed' is not evaluated until `thunk-force' is called, like the
41 ;; following:
43 ;; (thunk-force delayed)
45 ;; This file also defines macros `thunk-let' and `thunk-let*' that are
46 ;; analogous to `let' and `let*' but provide lazy evaluation of
47 ;; bindings by using thunks implicitly (i.e. in the expansion).
49 ;;; Code:
51 (eval-when-compile (require 'cl-macs))
53 (defmacro thunk-delay (&rest body)
54 "Delay the evaluation of BODY."
55 (declare (debug t))
56 (cl-assert lexical-binding)
57 (let ((forced (make-symbol "forced"))
58 (val (make-symbol "val")))
59 `(let (,forced ,val)
60 (lambda (&optional check)
61 (if check
62 ,forced
63 (unless ,forced
64 (setf ,val (progn ,@body))
65 (setf ,forced t))
66 ,val)))))
68 (defun thunk-force (delayed)
69 "Force the evaluation of DELAYED.
70 The result is cached and will be returned on subsequent calls
71 with the same DELAYED argument."
72 (funcall delayed))
74 (defun thunk-evaluated-p (delayed)
75 "Return non-nil if DELAYED has been evaluated."
76 (funcall delayed t))
78 (defmacro thunk-let (bindings &rest body)
79 "Like `let' but create lazy bindings.
81 BINDINGS is a list of elements of the form (SYMBOL EXPRESSION).
82 Any binding EXPRESSION is not evaluated before the variable
83 SYMBOL is used for the first time when evaluating the BODY.
85 It is not allowed to set `thunk-let' or `thunk-let*' bound
86 variables.
88 Using `thunk-let' and `thunk-let*' requires `lexical-binding'."
89 (declare (indent 1) (debug let))
90 (cl-callf2 mapcar
91 (lambda (binding)
92 (pcase binding
93 (`(,(pred symbolp) ,_) binding)
94 (_ (signal 'error (cons "Bad binding in thunk-let"
95 (list binding))))))
96 bindings)
97 (cl-callf2 mapcar
98 (pcase-lambda (`(,var ,binding))
99 (list (make-symbol (concat (symbol-name var) "-thunk"))
100 var binding))
101 bindings)
102 `(let ,(mapcar
103 (pcase-lambda (`(,thunk-var ,_var ,binding))
104 `(,thunk-var (thunk-delay ,binding)))
105 bindings)
106 (cl-symbol-macrolet
107 ,(mapcar (pcase-lambda (`(,thunk-var ,var ,_binding))
108 `(,var (thunk-force ,thunk-var)))
109 bindings)
110 ,@body)))
112 (defmacro thunk-let* (bindings &rest body)
113 "Like `let*' but create lazy bindings.
115 BINDINGS is a list of elements of the form (SYMBOL EXPRESSION).
116 Any binding EXPRESSION is not evaluated before the variable
117 SYMBOL is used for the first time when evaluating the BODY.
119 It is not allowed to set `thunk-let' or `thunk-let*' bound
120 variables.
122 Using `thunk-let' and `thunk-let*' requires `lexical-binding'."
123 (declare (indent 1) (debug let))
124 (cl-reduce
125 (lambda (expr binding) `(thunk-let (,binding) ,expr))
126 (nreverse bindings)
127 :initial-value (macroexp-progn body)))
129 ;; (defalias 'lazy-let #'thunk-let)
130 ;; (defalias 'lazy-let* #'thunk-let*)
133 (provide 'thunk)
134 ;;; thunk.el ends here