summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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 ...)))))