Commits

Moritz Heidkamp  committed 45af739

Implement lazy-length

  • Participants
  • Parent commits cb38f0f

Comments (0)

Files changed (2)

File lazy-seq.scm

 (lazy-seq make-lazy-seq
  lazy-seq? lazy-seq-realized? lazy-null? 
  lazy-seq->list list->lazy-seq
- lazy-head lazy-tail lazy-append
+ lazy-head lazy-tail
+ lazy-length lazy-append
  lazy-take lazy-drop lazy-ref
  lazy-take-while lazy-drop-while
  lazy-map lazy-filter lazy-each
        #f (cons (car list)
                 (list->lazy-seq (cdr list))))))
 
+(define (lazy-length seq)
+  (let loop ((count 0) (seq seq))
+    (if (lazy-null? seq)
+        count
+        (loop (+ count 1) (lazy-tail seq)))))
+
 (define (lazy-take n seq)
   (lazy-seq
     (if (or (zero? n) (lazy-null? seq))

File tests/run.scm

   (test "ok" (lazy-head (lazy-seq (list->lazy-seq '("ok"))))))
 
 
+(test-group "lazy-length"
+  (test 10 (lazy-length (lazy-numbers count: 10))))
+
 (test-group "lazy-map"
   (test '(1 2 3 4 5)
         (lazy-seq->list