summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2021-03-05 07:47:57 -0500
committerDavid Thompson <dthompson2@worcester.edu>2021-03-05 07:47:57 -0500
commit48df59c37e5fb1a2f5061589bfba4e3c155e2f08 (patch)
tree004e8bb17b499b353fb5ba6abd4d4d5679c1fd11
parent764155b7422301c497012989658fa6423bf1fd99 (diff)
utils: Add for-range macro.
-rw-r--r--.dir-locals.el3
-rw-r--r--chickadee/utils.scm24
2 files changed, 25 insertions, 2 deletions
diff --git a/.dir-locals.el b/.dir-locals.el
index f255f96..9eb5b8e 100644
--- a/.dir-locals.el
+++ b/.dir-locals.el
@@ -28,4 +28,5 @@
(eval . (put 'with-graphics-state! 'scheme-indent-function 1))
(eval . (put 'translate 'scheme-indent-function 1))
(eval . (put 'rotate 'scheme-indent-function 1))
- (eval . (put 'scale 'scheme-indent-function 1)))))
+ (eval . (put 'scale 'scheme-indent-function 1))
+ (eval . (put 'for-range 'scheme-indent-function 1)))))
diff --git a/chickadee/utils.scm b/chickadee/utils.scm
index 4f989ac..e2a20c1 100644
--- a/chickadee/utils.scm
+++ b/chickadee/utils.scm
@@ -17,7 +17,8 @@
;;; <http://www.gnu.org/licenses/>.
(define-module (chickadee utils)
- #:export (memoize))
+ #:export (memoize
+ for-range))
;; Written by Ludovic Courtès. Taken from GNU Guix.
(define (memoize proc)
@@ -31,3 +32,24 @@
list)))
(hash-set! cache args results)
(apply values results)))))))
+
+(define-syntax for-range
+ (syntax-rules ()
+ ((_ ((var end start inc)) body ...)
+ (let* ((s start) ; evaluate start/end only once
+ (e end)
+ (reverse? (< e s))
+ (start* (if reverse? e s))
+ (end* (if reverse? s e)))
+ (let loop ((var start*))
+ (when (< var end*)
+ body ...
+ (loop (+ var inc))))))
+ ((_ ((var end start)) body ...)
+ (for-range ((var end start 1)) body ...))
+ ((_ ((var end)) body ...)
+ (for-range ((var end 0 1)) body ...))
+ ((_ ((var args ...) rest ...) body ...)
+ (for-range ((var args ...))
+ (for-range (rest ...)
+ body ...)))))