aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNicolas Petton2015-10-23 13:20:45 +0200
committerNicolas Petton2015-10-23 13:22:21 +0200
commit0f443a12368d02e256c7e94c3de574c6ceaed86e (patch)
tree5cc3271bfe6fce92a1131237d812b2fa4d86b165
parent71d6acff1cabbf983c8191d02f084faf3f56d788 (diff)
downloademacs-0f443a12368d02e256c7e94c3de574c6ceaed86e.tar.gz
emacs-0f443a12368d02e256c7e94c3de574c6ceaed86e.zip
New library thunk.el
thunk.el is extracted from stream.el in ELPA, with additional tests. * lisp/emacs-lisp/thunk.el: New file. * test/automated/thunk-tests.el: New file. * etc/NEWS: Add information about thunk.el
-rw-r--r--etc/NEWS5
-rw-r--r--lisp/emacs-lisp/thunk.el63
-rw-r--r--test/automated/thunk-tests.el55
3 files changed, 123 insertions, 0 deletions
diff --git a/etc/NEWS b/etc/NEWS
index 6588aad5bfc..58ab6bec161 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -582,6 +582,11 @@ prefixed with `seq-' and work on lists, strings and vectors.
582The map library provides map-manipulation functions that work on alists, 582The map library provides map-manipulation functions that work on alists,
583hash-table and arrays. All functions are prefixed with "map-". 583hash-table and arrays. All functions are prefixed with "map-".
584 584
585** thunk
586*** New thunk library:
587Thunk provides functions and macros to control the evaluation of
588forms.
589
585** Calendar and diary 590** Calendar and diary
586 591
587+++ 592+++
diff --git a/lisp/emacs-lisp/thunk.el b/lisp/emacs-lisp/thunk.el
new file mode 100644
index 00000000000..ab366d295f5
--- /dev/null
+++ b/lisp/emacs-lisp/thunk.el
@@ -0,0 +1,63 @@
1;;; thunk.el --- Lazy form evaluation -*- lexical-binding: t -*-
2
3;; Copyright (C) 2015 Free Software Foundation, Inc.
4
5;; Author: Nicolas Petton <nicolas@petton.fr>
6;; Keywords: sequences
7;; Version: 1.0
8;; Package: thunk
9
10;; Maintainer: emacs-devel@gnu.org
11
12;; This file is part of GNU Emacs.
13
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.
18
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.
23
24;; You should have received a copy of the GNU General Public License
25;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26
27;;; Commentary:
28;;
29;; Thunk provides functions and macros to control the evaluation of
30;; forms. Use `thunk-delay' to delay the evaluation of a form, and
31;; `thunk-force' to evaluate it. Evaluation is cached, and only
32;; happens once.
33
34;; Tests are located at test/automated/thunk-tests.el
35
36;;; Code:
37
38(defmacro thunk-delay (&rest body)
39 "Delay the evaluation of BODY."
40 (declare (debug t))
41 (let ((forced (make-symbol "forced"))
42 (val (make-symbol "val")))
43 `(let (,forced ,val)
44 (lambda (&optional check)
45 (if check
46 ,forced
47 (unless ,forced
48 (setf ,val (progn ,@body))
49 (setf ,forced t)))
50 ,val))))
51
52(defun thunk-force (delayed)
53 "Force the evaluation of DELAYED.
54The result is cached and will be returned on subsequent calls
55with the same DELAYED argument."
56 (funcall delayed))
57
58(defun thunk-evaluated-p (delayed)
59 "Return non-nil if DELAYED has been evaluated."
60 (funcall delayed t))
61
62(provide 'thunk)
63;;; thunk.el ends here
diff --git a/test/automated/thunk-tests.el b/test/automated/thunk-tests.el
new file mode 100644
index 00000000000..7abbd299ead
--- /dev/null
+++ b/test/automated/thunk-tests.el
@@ -0,0 +1,55 @@
1;;; thunk-tests.el --- Tests for thunk.el -*- lexical-binding: t -*-
2
3;; Copyright (C) 2015 Free Software Foundation, Inc.
4
5;; Author: Nicolas Petton <nicolas@petton.fr>
6;; Maintainer: emacs-devel@gnu.org
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software: you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23;;; Commentary:
24
25;; Tests for thunk.el
26
27;;; Code:
28
29(require 'ert)
30(require 'thunk)
31
32(ert-deftest thunk-should-be-lazy ()
33 (let (x)
34 (thunk-delay (setq x t))
35 (should (null x))))
36
37(ert-deftest thunk-can-be-evaluated ()
38 (let* (x
39 (thunk (thunk-delay (setq x t))))
40 (should-not (thunk-evaluated-p thunk))
41 (should (null x))
42 (thunk-force thunk)
43 (should (thunk-evaluated-p thunk))
44 (should x)))
45
46(ert-deftest thunk-evaluation-is-cached ()
47 (let* ((x 0)
48 (thunk (thunk-delay (setq x (1+ x)))))
49 (thunk-force thunk)
50 (should (= x 1))
51 (thunk-force thunk)
52 (should (= x 1))))
53
54(provide 'thunk-tests)
55;;; thunk-tests.el ends here