summary refs log tree commit diff
path: root/implementation
diff options
context:
space:
mode:
authorAdam R. Nelson <adam@nels.onl>2021-03-11 22:42:57 -0500
committerArthur A. Gleckler <srfi@speechcode.com>2021-03-11 21:00:45 -0800
commit368abdee7869e99fa287ec2e6b099ee4584372e5 (patch)
treee32a0fad8d4d2dc4b4b8cf7ec53915e9aad87b19 /implementation
parent983ee1afbab1108c3608ba4228824f6944301d69 (diff)
downloadsrfi-214-368abdee7869e99fa287ec2e6b099ee4584372e5.tar.gz
srfi-214-368abdee7869e99fa287ec2e6b099ee4584372e5.tar.xz
srfi-214-368abdee7869e99fa287ec2e6b099ee4584372e5.zip
last-minute updates: clarify some documentation, add flexvector->generator
Diffstat (limited to 'implementation')
-rw-r--r--implementation/flexvectors-body2.scm10
-rw-r--r--implementation/flexvectors.sld2
-rw-r--r--implementation/tests.scm18
3 files changed, 29 insertions, 1 deletions
diff --git a/implementation/flexvectors-body2.scm b/implementation/flexvectors-body2.scm
index ba3e2f5..6f37efe 100644
--- a/implementation/flexvectors-body2.scm
+++ b/implementation/flexvectors-body2.scm
@@ -389,3 +389,13 @@
 (define (generator->flexvector g)
   (assume (procedure? g))
   (flexvector-unfold eof-object? (lambda (x) x) (lambda (_) (g)) (g)))
+
+(define (flexvector->generator fv)
+  (assume (flexvector? fv))
+  (let ((i 0))
+    (lambda ()
+      (if (< i (flexvector-length fv))
+        (let ((element (flexvector-ref fv i)))
+          (set! i (+ i 1))
+          element)
+        (eof-object)))))
diff --git a/implementation/flexvectors.sld b/implementation/flexvectors.sld
index 25e1543..4bc7da3 100644
--- a/implementation/flexvectors.sld
+++ b/implementation/flexvectors.sld
@@ -38,7 +38,7 @@
           flexvector->vector flexvector->list flexvector->string
           vector->flexvector list->flexvector string->flexvector
           reverse-flexvector->list reverse-list->flexvector
-          generator->flexvector)
+          generator->flexvector flexvector->generator)
 
   (import (scheme base)
           (scheme case-lambda)
diff --git a/implementation/tests.scm b/implementation/tests.scm
index 329a8d6..e41f19c 100644
--- a/implementation/tests.scm
+++ b/implementation/tests.scm
@@ -64,6 +64,24 @@
   (flexvector->vector (string->flexvector "abc")))
 (test-equal "flexvector->string" "abc" (flexvector->string (flexvector #\a #\b #\c)))
 
+(define genlist '(a b c))
+(define (mock-generator)
+  (if (pair? genlist)
+    (let ((value (car genlist)))
+      (set! genlist (cdr genlist))
+      value)
+    (eof-object)))
+
+(test-equal "generator->flexvector" #(a b c)
+  (flexvector->vector (generator->flexvector mock-generator)))
+(test-equal "flexvector->generator" '(a b c #t)
+  (let* ((gen (flexvector->generator (flexvector 'a 'b 'c)))
+         (one (gen))
+         (two (gen))
+         (three (gen))
+         (four (eof-object? (gen))))
+    (list one two three four)))
+
 ; Nondestructive operations on one vector
 (let ((fv (flexvector 10 20 30)))
   (test-equal "flexvector->vector" #(10 20 30) (flexvector->vector fv))