From 368abdee7869e99fa287ec2e6b099ee4584372e5 Mon Sep 17 00:00:00 2001 From: "Adam R. Nelson" Date: Thu, 11 Mar 2021 22:42:57 -0500 Subject: last-minute updates: clarify some documentation, add flexvector->generator --- implementation/flexvectors-body2.scm | 10 ++++++++++ implementation/flexvectors.sld | 2 +- implementation/tests.scm | 18 ++++++++++++++++++ srfi-214.html | 10 ++++++++-- 4 files changed, 37 insertions(+), 3 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)) diff --git a/srfi-214.html b/srfi-214.html index 93d2fc0..2341c60 100644 --- a/srfi-214.html +++ b/srfi-214.html @@ -153,6 +153,7 @@
  • reverse-list->flexvector
  • flexvector->string
  • string->flexvector
  • +
  • flexvector->generator
  • generator->flexvector
  • @@ -336,18 +337,20 @@ zot
    (flexvector-count even? (flexvector 3 1 4 1 5 9 2 5 6))
    ;=> 3
     
    (flexvector-count < (flexvector 1 3 6 9) (flexvector 2 4 6 8 10 12))
    ;=> 2

    flexvector-cumulate

    (flexvector-cumulate f knil fv)

    -

    Returns a newly-allocated flexvector new with the same length as fv. Each element i of new is set to the result of (f (flexvector-ref new (- i 1)) (flexvector-ref vec i)), except that, for the first call on f, the first argument is knil. The new flexvector is returned.

    +

    Returns a newly-allocated flexvector new with the same length as fv. Each element i of new is set to the result of (f (flexvector-ref new (- i 1)) (flexvector-ref fv i)), except that, for the first call on f, the first argument is knil. The new flexvector is returned.

    (flexvector-cumulate + 0 (flexvector 3 1 4 1 5 9 2 5 6))
    ;=> #<flexvector 3 4 8 9 14 23 25 30 36>

    Searching

    flexvector-index, flexvector-index-right

    (flexvector-index pred? fv1 fv2 ...)

    Finds and returns the index of the first elements in fv1 fv2 ... that satisfy pred?. If no matching element is found by the end of the shortest flexvector, #f is returned.

    flexvector-index-right is similar, but returns the index of the last elements that satisfy pred?, and requires all flexvector arguments to have the same length.

    +

    Given n arguments fv1 fv2..., pred? should be a function that takes n arguments and returns a single value, interpreted as a boolean.

    (flexvector-index even? (flexvector 3 1 4 1 5 9))
    ;=> 2
     
    (flexvector-index < (flexvector 3 1 4 1 5 9 2 5 6) (flexvector 2 7 1 8 2))
    ;=> 1
     
    (flexvector-index = (flexvector 3 1 4 1 5 9 2 5 6) (flexvector 2 7 1 8 2))
    ;=> #f
     
    (flexvector-index-right < (flexvector 3 1 4 1 5) (flexvector 2 7 1 8 2))
    ;=> 3

    flexvector-skip, flexvector-skip-right

    (flexvector-skip pred? fv1 fv2 ...)

    Finds and returns the index of the first elements in fv1 fv2 ... that do not satisfy pred?. If all the values in the flexvectors satisfy pred? until the end of the shortest flexvector, this returns #f.

    flexvector-skip-right is similar, but returns the index of the last elements that do not satisfy pred?, and requires all flexvector arguments to have the same length.

    +

    Given n arguments fv1 fv2..., pred? should be a function that takes n arguments and returns a single value, interpreted as a boolean.

    (flexvector-skip number? (flexvector 1 2 'a 'b 3 4 'c 'd))
    ;=> 2
     
    (flexvector-skip-right number? (flexvector 1 2 'a 'b 3 4 'c 'd))
    ;=> 4

    (flexvector-binary-search fv value cmp [start [end]])

    @@ -388,8 +391,11 @@ zot

    Both start and end are clamped to the range [0, (flexvector-length fv)). It is an error if end is less than start.

    string->flexvector

    (string->flexvector string [start [end]])

    -

    Creates a flexvector containing the elements in string between start, which defaults to -1, and end, which defaults to the length of string.

    +

    Creates a flexvector containing the elements in string between start, which defaults to 0, and end, which defaults to the length of string.

    Both start and end are clamped to the range [0, (string-length string)). It is an error if end is less than start.

    +

    flexvector->generator

    +

    (flexvector->generator fv)

    +

    Returns a SRFI 158 generator that emits the elements of the flexvector fv, in order. If fv is mutated before the generator is exhausted, the generator's remaining return values are undefined.

    generator->flexvector

    (generator->flexvector gen)

    Creates a flexvector containing all elements produced by the SRFI 158 generator gen.

    -- cgit 1.4.1