Commits

Moritz Heidkamp committed d269524

Implement lazy-append-map

  • Participants
  • Parent commits ae04c1b

Comments (0)

Files changed (2)

  lazy-map lazy-filter lazy-each
  lazy-iterate lazy-repeat lazy-repeatedly
  lazy-numbers input-port->lazy-seq
- lazy-cycle)
+ lazy-cycle lazy-append-map)
 
 (import chicken scheme)
 (use srfi-1 extras)
                       (+ start step)
                       step))))))
 
-(define lazy-map
+(define (lazy-append . seqs)
+  (let loop ((seqs seqs))
+    (lazy-seq
+      (if (null? seqs)
+          '()
+          (let loop2 ((seq (car seqs)))
+            (lazy-seq
+              (if (lazy-null? seq)
+                  (loop (cdr seqs))
+                  (cons (lazy-head seq)
+                        (loop2 (lazy-tail seq))))))))))
+
+(define (make-lazy-mapping-proc append-result)
   (case-lambda
    ((proc seq)
     (let loop ((seq seq))
       (lazy-seq
         (if (lazy-null? seq)
             '()
-            (cons (proc (lazy-head seq))
-                  (loop (lazy-tail seq)))))))
+            (append-result
+             (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)))))))))
+            (append-result
+             (apply proc (map lazy-head seqs))
+             (loop (map lazy-tail seqs)))))))))
+
+(define lazy-map
+  (make-lazy-mapping-proc cons))
+
+(define lazy-append-map
+  (make-lazy-mapping-proc lazy-append))
 
 (define (lazy-filter pred? seq)
   (let loop ((seq seq))
 (define (lazy-repeatedly f)
   (lazy-seq (cons (f) (lazy-repeatedly f))))
 
-(define (lazy-append . seqs)
-  (let loop ((seqs seqs))
-    (lazy-seq
-      (if (null? seqs)
-          '()
-          (let loop2 ((seq (car seqs)))
-            (lazy-seq
-              (if (lazy-null? seq)
-                  (loop (cdr seqs))
-                  (cons (lazy-head seq)
-                        (loop2 (lazy-tail seq))))))))))
-
 (define (lazy-iterate f x)
   (lazy-seq
     (cons x (lazy-iterate f (f x)))))
                                 (lazy-numbers start: 7)
                                 (lazy-numbers start: 3))))))
 
+(test-group "lazy-append-map"
+  (test '(1 2 3 4 5 6 7)
+        (lazy-seq->list
+         (lazy-append-map
+          list->lazy-seq 
+          (list->lazy-seq '(1 3 5))
+          (list->lazy-seq '(2 4 6))))))
+
 
 (test-group "lazy-filter"
   (test '(2 8 14 20 26)