Skip to content

Commit

Permalink
Merge pull request #1 from svetlyak40wt/keep-all-values
Browse files Browse the repository at this point in the history
Now lazy:lazy-value returns all values returned by original form.
  • Loading branch information
massung committed Aug 31, 2020
2 parents 939a7e9 + d9d9490 commit fa6ec5f
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions lazy.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@

(defclass thunk ()
((form :initarg :form)
(value :initarg :value))
(values :initarg :values))
(:documentation "A form to be evaluated later."))

;;; ----------------------------------------------------

(defmethod print-object ((obj thunk) stream)
"Output a lazy form."
(print-unreadable-object (obj stream :type t)
(if (slot-boundp obj 'value)
(if (slot-boundp obj 'values)
(prin1 (lazy-value obj) stream)
(princ "UNREALIZED" stream))))

;;; ----------------------------------------------------

(defmethod thunk-realized-p ((thunk thunk))
"T if the thunk has been realized to a value."
(slot-boundp thunk 'value))
(slot-boundp thunk 'values))

;;; ----------------------------------------------------

Expand All @@ -57,8 +57,9 @@

(defun lazy-value (thunk)
"Get the value of a lazy form."
(with-slots (form value)
(with-slots (form values)
thunk
(if (thunk-realized-p thunk)
value
(setf value (funcall form)))))
(unless (thunk-realized-p thunk)
(setf values (multiple-value-list
(funcall form))))
(values-list values)))

0 comments on commit fa6ec5f

Please sign in to comment.