Hello Ivan,

You are listed as a maintainer of srfi-127 egg. Can you apply this patch from
upstream, please? It amends the implementation of lseq-for-each to be actually
lazy.

I have only removed the redundant no-arguments check from the original patch,
which would otherwise require extra imports in order to compile.

---8<---
From 7976fce401da8ac368ada1914c5fbaba715aa246 Mon Sep 17 00:00:00 2001
From: Wolfgang Corcoran-Mathe <[email protected]>
Date: Sat, 11 Feb 2023 14:07:04 -0500
Subject: [PATCH] Implement lseq-for-each directly.

Fully forcing lseq-for-each's lseq arguments wastes memory and
causes side effects to occur in an unexpected order.

See the following SRFI 127 mailing list message for a discussion
of these issues.

https://srfi-email.schemers.org/srfi-127/msg/21875262/
---
 lseqs/lseqs-impl.scm | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/lseqs/lseqs-impl.scm b/lseqs/lseqs-impl.scm
index a30b2b1..31aab6e 100644
--- a/lseqs/lseqs-impl.scm
+++ b/lseqs/lseqs-impl.scm
@@ -146,9 +146,22 @@
 (define (lseq-zip . lseqs) (apply lseq-map list lseqs))
 
 ;; Eagerly apply a proc to the elements of lseqs
-;; Included because it's a common operation, even though it is trivial
-(define (lseq-for-each proc . lseqs)
-  (apply for-each proc (map lseq-realize lseqs)))
+(define lseq-for-each
+  (case-lambda
+    ((proc lseq)              ; 1-sequence fast path
+     (let loop ((lseq lseq))
+       (if (null? lseq)
+         (if #f #f)
+         (begin
+          (proc (lseq-car lseq))
+          (loop (lseq-cdr lseq))))))
+    ((proc . lseqs)           ; variadic path
+     (let loop ((lseqs lseqs))
+       (if (any-null? lseqs)
+         (if #f #f)
+         (begin
+          (apply proc (map lseq-car lseqs))
+          (loop (map lseq-cdr lseqs))))))))
 
 ;; Filter an lseq lazily to include only elements that satisfy pred
 (define (lseq-filter pred lseq)
-- 
2.53.0
---8<---

—
Stan

Reply via email to