Moritz Heidkamp avatar Moritz Heidkamp committed bca6059

Implement lazy-take-while and lazy-drop-while

Comments (0)

Files changed (2)

  lazy-seq->list list->lazy-seq
  lazy-head lazy-tail lazy-append
  lazy-take lazy-drop lazy-ref
+ lazy-take-while lazy-drop-while
  lazy-map lazy-filter lazy-each
  lazy-iterate lazy-repeat lazy-repeatedly
  lazy-numbers input-port->lazy-seq)
         seq
         (lazy-drop (- n 1) (lazy-tail seq)))))
 
+(define (lazy-take-while pred? seq)
+  (let loop ((seq seq))
+    (lazy-seq
+      (cond ((lazy-null? seq) '())
+            ((pred? (lazy-head seq))
+             (cons (lazy-head seq)
+                   (loop (lazy-tail seq))))
+            (else '())))))
+
+(define (lazy-drop-while pred? seq)
+  (let loop ((seq seq))
+    (lazy-seq
+      (cond ((lazy-null? seq) '())
+            ((pred? (lazy-head seq))
+             (loop (lazy-tail seq)))
+            (else seq)))))
+
 (define (lazy-numbers #!key (step 1) (start 0) count)
   (let loop ((count count) (start start) (step step))
     (lazy-seq
         (lazy-seq->list
          (lazy-take 4 (lazy-iterate (lambda (x) (* x 2)) 2)))))
 
+(test-group "lazy-take-while"
+  (test '(2 2 2 4)
+        (lazy-seq->list
+         (lazy-take-while even? (list->lazy-seq '(2 2 2 4 5 6))))))
+
+(test-group "lazy-drop-while"
+  (test '(end)
+        (lazy-seq->list
+         (lazy-drop-while number? (list->lazy-seq '(1 2 3 4 end))))))
+
 (test-exit)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.