summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2017-04-27 22:26:06 -0400
committerDavid Thompson <dthompson2@worcester.edu>2017-04-27 22:26:06 -0400
commit5e8aab990b355ed51b409fb1916afd6b8848c8b8 (patch)
tree8edad47385de41cdc6c731560c94e69d95a1d92c
parent2c3d74bc23947a998fa1f330c5ab025804866c99 (diff)
scripting: agenda: Add 'every' scheduling option.
* chickadee/scripting/agenda.scm (schedule-every): New procedure. (every): New syntax. * doc/api.texi (Agendas): Document it.
-rw-r--r--chickadee/scripting/agenda.scm24
-rw-r--r--doc/api.texi12
2 files changed, 35 insertions, 1 deletions
diff --git a/chickadee/scripting/agenda.scm b/chickadee/scripting/agenda.scm
index c815afa..f16dc45 100644
--- a/chickadee/scripting/agenda.scm
+++ b/chickadee/scripting/agenda.scm
@@ -28,8 +28,10 @@
clear-agenda
schedule-at
schedule-after
+ schedule-every
at
- after))
+ after
+ every))
(define-record-type <agenda>
(%make-agenda time queue)
@@ -93,8 +95,28 @@
"Schedule THUNK to be run after DELAY."
(schedule (current-agenda) (+ (agenda-time) delay) thunk))
+(define* (schedule-every interval thunk #:optional n)
+ "Schedule THUNK to run every INTERVAL amount of time. Repeat this N
+times, or indefinitely if not specified."
+ (schedule-after interval
+ (lambda ()
+ (cond
+ ((not n)
+ (thunk)
+ (schedule-every interval thunk))
+ ((> n 0)
+ (thunk)
+ (schedule-every interval thunk (- n 1)))))))
+
(define-syntax-rule (at time body ...)
(schedule-at time (lambda () body ...)))
(define-syntax-rule (after delay body ...)
(schedule-after delay (lambda () body ...)))
+
+(define-syntax every
+ (syntax-rules ()
+ ((_ (interval n) body ...)
+ (schedule-every interval (lambda () body ...) n))
+ ((_ interval body ...)
+ (schedule-every interval (lambda () body ...)))))
diff --git a/doc/api.texi b/doc/api.texi
index e1f6acc..1a56039 100644
--- a/doc/api.texi
+++ b/doc/api.texi
@@ -955,6 +955,12 @@ Schedule @var{thunk}, a procedure of zero arguments, to be run after
@var{delay}.
@end deffn
+@deffn {Scheme Procedure} schedule-every @var{interval} @var{thunk} [@var{n}]
+Schedule @var{thunk}, a procedure of zero arguments, to be run every
+@var{interval} amount of time. Repeat this @var{n} times, or
+indefinitely if not specified.
+@end deffn
+
@deffn {Scheme Syntax} at @var{time} @var{body} @dots{}
Schedule @var{body} to be evaluated at @var{time}.
@end deffn
@@ -963,6 +969,12 @@ Schedule @var{body} to be evaluated at @var{time}.
Schedule @var{body} to be evaluated after @var{delay}.
@end deffn
+@deffn {Scheme Syntax} every @var{interval} @var{body} @dots{}
+@deffnx {Scheme Syntax} every (@var{interval} @var{n}) @var{body} @dots{}
+Schedule @var{body} to be evaluated every @var{interval} amount of
+time. Repeat this @var{n} times, or indefinitely if not specified.
+@end deffn
+
@node Coroutines
@subsection Coroutines