Commits

Anonymous committed 216ca39

Add optimized lazy-map branch for single seq argument case

  • Participants
  • Parent commits 56cc103

Comments (0)

Files changed (2)

File lazy-seq.scm

                       (+ start step)
                       step))))))
 
-(define (lazy-map proc . seqs)
-  (lazy-seq
-    (if (any lazy-null? seqs)
-        '()
-        (cons (apply proc (map lazy-head seqs))
-              (apply lazy-map proc (map lazy-tail seqs))))))
+(define lazy-map
+  (case-lambda
+   ((proc seq)
+    (let loop ((seq seq))
+      (lazy-seq
+        (if (lazy-null? seq)
+            '()
+            (cons (proc (lazy-head seq))
+                  (loop (lazy-tail seq)))))))
+   ((proc seq . seqs)
+    (let loop ((seqs (cons seq seqs)))
+      (lazy-seq
+        (if (any lazy-null? seqs)
+            '()
+            (cons (apply proc (map lazy-head seqs))
+                  (loop (map lazy-tail seqs)))))))))
 
 (define (lazy-filter pred? seq)
   (let loop ((seq seq))

File tests/run.scm

 
 
 (test-group "lazy-map"
+  (test '(1 2 3 4 5)
+        (lazy-seq->list
+         (lazy-take 5 (lazy-map add1 (lazy-numbers)))))
   (test '(10 12 14)
         (lazy-seq->list
          (lazy-take 3 (lazy-map +