c276ee07daac

Add in-whatever
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Sat, 06 Aug 2016 00:01:39 +0000
parents b31619a5b09c
children cb1e9c623738
branches/tags (none)
files losh.lisp package.lisp

Changes

--- a/losh.lisp	Fri Aug 05 23:35:35 2016 +0000
+++ b/losh.lisp	Sat Aug 06 00:01:39 2016 +0000
@@ -365,6 +365,28 @@
              (pop ,seq))))))))
 
 
+(defmacro-driver (FOR var IN-WHATEVER seq)
+  "Iterate over items in the given sequence.
+
+  Unlike iterate's own `in-sequence` this won't use the horrifically inefficient
+  `elt`/`length` functions on a list.
+
+  "
+  (let ((kwd (if generate 'generate 'for)))
+    (with-gensyms (is-list source i len)
+      `(progn
+        (with ,source = ,seq)
+        (with ,is-list = (typep ,source 'list))
+        (with ,len = (if ,is-list -1 (length ,source)))
+        (for ,i :from 0)
+        (,kwd ,var next (if ,is-list
+                          (if ,source
+                            (pop ,source)
+                            (terminate))
+                          (if (< ,i ,len)
+                            (elt ,source ,i)
+                            (terminate))))))))
+
 ;;;; Hash Sets
 (defclass hash-set ()
   ((data :initarg :data)))
--- a/package.lisp	Fri Aug 05 23:35:35 2016 +0000
+++ b/package.lisp	Sat Aug 06 00:01:39 2016 +0000
@@ -53,6 +53,7 @@
     #:run-time
     #:in-lists
     #:in-sequences
+    #:in-whatever
 
     #:hash-set
     #:make-set