Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reazon-run-with-config and 'timeout' option #12

Merged
merged 1 commit into from
Aug 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions reazon.el
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@

;;; Code:

;; -- Configuration --

(defvar reazon-timeout nil
"The maximum amount of time in seconds that a query can take.

If a query is interrupted, it will return any results it has
collected so far.

Consider let-binding this around your call to `reazon-run'
instead of setqing it.")

(defvar reazon--stop-time nil
"The time from epoch in seconds that a query should halt computation.

Should not be set manually, derived from `reazon-timeout' when
using `reazon-run' or `reazon-run*'.")

;; -- Variables --

;; Reazon variables need to be distinct from Lisp symbols. They are
Expand Down Expand Up @@ -165,21 +182,26 @@ STREAM-2, else append them as usual."
(funcall rest)))))))

(defun reazon--pull (stream)
"Force STREAM until it isn't a suspension, then return it."
"Force STREAM until it isn't a suspension or `reazon--stop-time' is reached."
(let ((result stream))
(while (functionp result)
(while (and (functionp result)
(or (not reazon--stop-time)
(> reazon--stop-time (float-time))))
(setq result (funcall result)))
result))

(defun reazon--take (n stream)
"Pull N values from STREAM if N is positive else pull it without stopping."
"Pull N values from STREAM if N is positive else pull it without stopping.

If `stream' is a function, then `reazon--pull' ended due to a
timeout, and the values collected so far will be returned."
(declare (indent 1))
(if (null stream)
(if (or (functionp stream) (null stream))
nil
(let ((count (if n (1- n) -1))
(result (list (car stream)))
(rest (reazon--pull (cdr stream))))
(while (and rest (not (zerop count)))
(while (and rest (not (zerop count)) (not (functionp rest)))
(setq count (1- count))
(setq result (cons (car rest) result))
(setq rest (reazon--pull (cdr rest))))
Expand Down Expand Up @@ -383,7 +405,8 @@ either a symbol or a list."
(reazon-== (list ,@vars) ,q)
,@goals)))
(let ((var var/list))
`(let ((,var (reazon--make-variable ',var)))
`(let ((,var (reazon--make-variable ',var))
(reazon--stop-time (and reazon-timeout (+ reazon-timeout (float-time)))))
(mapcar
(reazon--reify ,var)
(reazon--take ,n
Expand Down
15 changes: 15 additions & 0 deletions test/reazon-test-interface.el
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@
(reazon--should-equal '(olive _0 oil)
(reazon-run* x (reazon-disj (reazon-conj (reazon-== 'virgin x) #'reazon-!U) (reazon-disj (reazon-== 'olive x) (reazon-disj #'reazon-!S (reazon-== 'oil x)))))))

(reazon-defrel reazon-test--alwayso ()
"Infinite successful goals."
(reazon-disj #'reazon-!S (reazon-test--alwayso)))

(reazon-defrel reazon-test--nevero ()
"Infinite unsuccessful goals."
(reazon-disj #'reazon-!U (reazon-test--nevero)))

(ert-deftest reazon-test-interface-timeout ()
(reazon--should-equal '()
(let ((reazon-timeout 0.01))
(reazon-run* q (reazon-test--nevero))))
;; This test might fail if your computer is REALLY slow.
(should (> (length (let ((reazon-timeout 0.01)) (reazon-run* q (reazon-test--alwayso)))) 3)))

(ert-deftest reazon-test-interface-fresh ()
(reazon--should-equal '(t)
(reazon-run* q
Expand Down