Commits

Anonymous committed a4adf4f

Implement lazy-each (side-effect centric eager iteration)

Comments (0)

Files changed (2)

  lazy-seq->list list->lazy-seq
  lazy-head lazy-tail
  lazy-take lazy-drop lazy-ref
- lazy-map lazy-filter
+ lazy-map lazy-filter lazy-each
  lazy-numbers
  realized-lazy-seq
  input-port->lazy-seq)
       (lazy-head seq)
       (lazy-ref (- n 1) (lazy-tail seq))))
 
+(define (lazy-each proc . seqs)
+  (unless (any lazy-null? seqs)
+    (apply proc (map lazy-head seqs))
+    (apply lazy-each proc (map lazy-tail seqs))))
+
 (define (input-port->lazy-seq port read)
   (let loop ()
     (lazy-seq
      (open-input-string "")
      read))))
 
+(test-group "lazy-each"
+  (define sum 0)
+  (lazy-each (lambda (i)
+               (set! sum (+ i sum)))
+             (lazy-numbers count: 5))
+  (test sum (fold + 0 (iota 5))))
+
 (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.