Commits

Moritz Heidkamp committed 2aa018e

Implement lazy-cycle

Comments (0)

Files changed (2)

  lazy-take-while lazy-drop-while
  lazy-map lazy-filter lazy-each
  lazy-iterate lazy-repeat lazy-repeatedly
- lazy-numbers input-port->lazy-seq)
+ lazy-numbers input-port->lazy-seq
+ lazy-cycle)
 
 (import chicken scheme)
 (use srfi-1 extras)
           (loop (lazy-tail seq)
                 (%make-lazy-seq #f (cons (lazy-head seq) rev-seq)))))))
 
+(define (lazy-cycle seq)
+  (lazy-seq
+    (if (lazy-null? seq)
+        '()
+        (let loop ((rest seq))
+          (lazy-seq
+            (if (lazy-null? rest)
+                (loop seq)
+                (cons (lazy-head rest)
+                      (loop (lazy-tail rest)))))))))
+
 )
          (lazy-reverse
           (lazy-numbers count: 3 start: 1)))))
 
+(test-group "lazy-cycle"
+  (test '(1 2 1 2 1 2)
+        (lazy-seq->list
+         (lazy-take 6 (lazy-cycle
+                       (list->lazy-seq '(1 2))))))
+  (test '() (lazy-seq->list (lazy-cycle (list->lazy-seq '())))))
+
 (test-exit)