Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fare committed Nov 18, 2023
1 parent ca554e8 commit 78b87f7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 14 deletions.
33 changes: 32 additions & 1 deletion doc/reference/std/misc/vector.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ that complement those provided by RnRS, Gambit and `:std/srfi/43`.
:::

## vector-ref-set!

``` scheme
(def vector-ref-set! vector-set!)
(set! (vector-ref v i) x)
Expand All @@ -28,8 +27,40 @@ This binding enables you to use `set!` with `vector-ref`.
:::

### vector-least-index
``` scheme
(vector-least-index pred vector [start: 0] [end: #f])
```

Given a predicate `pred` on the elements of given `vector`, that is “increasing”,
i.e. if true for a given element, true on all subsequent elements, and optionally
a `start` (inclusive, defaults to `0`) and an end (exclusive, defaults to `#f`
which designates the vector length), return the least index of a vector element
in the interval [start, env) that satisfies the predicate, or the end if none does.

::: tip Examples:
``` scheme
> (vector-least-index (cut < 10) #(35 21 16 11 10 9 7 4 1))
5
```
:::

### vector-most-index
``` scheme
(vector-most-index pred vector [start: 0] [end: #f])
```

Given a predicate `pred` on the elements of given `vector`, that is “decreasing”,
i.e. if false for a given element, false on all subsequent elements, and optionally
a `start` (inclusive, defaults to `0`) and an end (exclusive, defaults to `#f`
which designates the vector length), return the most index of a vector element
in the interval [start, env) that satisfies the predicate, or the end if none does.

::: tip Examples:
``` scheme
> (vector-most-index (cut < 10) #(2 3 5 7 11 13 17 19 23))
4
```
:::

### maybe-subvector

Expand Down
4 changes: 2 additions & 2 deletions src/std/misc/number-test.ss
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@
(check (least-integer (cut > <> 13.5) 0 20) => 14)
(check (least-integer true 0 20) => 0)
(check (least-integer false 0 20) => 20)
(check (most-integer true 0 20) => 20)
(check (most-integer false 0 20) => 0)
(check (most-integer true 0 20) => 8)
(check (most-integer false 0 20) => -1)
(check (most-integer (cut < <> 13.5) 0 20) => 13))

(test-case "bezout, invert-mod, div-mod, mult-mod"
Expand Down
16 changes: 10 additions & 6 deletions src/std/misc/vector-test.ss
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
(def foo (vector 1 2 3))
(set! (vector-ref foo 1) 4)
(check foo => #(1 4 3))
(check (vector-least-index (cut < 10) #(35 21 16 11 10 9 7 4 1)) => 5)
(check (vector-most-index (cut < 10) #(2 3 5 7 11 13 17 19 23)) => 4)
(check (vector-least-index (cut < <> 10) #(35 21 16 11 10 9 7 4 1)) => 5)
(check (vector-least-index true #(35 21 16 11 10 9 7 4 1)) => 0)
(check (vector-least-index false #(35 21 16 11 10 9 7 4 1)) => 9)
(check (vector-most-index (cut < <> 10) #(2 3 5 7 11 13 17 19 23)) => 4)
(check (vector-most-index true #(2 3 5 7 11 13 17 19 23)) => 9)
(check (vector-most-index false #(2 3 5 7 11 13 17 19 23)) => 0)
(check (maybe-subvector #(1 3 5 7) 2) => #(5 7))
(check (eq? foo (maybe-subvector foo 0 3)) => #t)
(check (with-list-builder (c)
Expand All @@ -22,20 +26,20 @@
(check (with-list-builder (c)
(subvector-for-each/index
(lambda (x y) (c [x y])) #(a b c d e f g h) start: 5))
=> '((f 5) (g 6) (h 7)))
=> '((5 f) (6 g) (7 h)))
(check (with-list-builder (c)
(subvector-reverse-for-each c #(a b c d e f g h) start: 2 end: 5))
=> '(e d c))
(check (with-list-builder (c)
(subvector-reverse-for-each/index
(lambda (x y) (c [x y])) #(a b c d e f g h) start: 5))
=> '((h 7) (g 6) (f 5)))
=> '((7 h) (6 g) (5 f)))
(check (subvector->list #(a b c d e f g h) start: 5)
=> '(c d e))
=> '(f g h))
(check (cons->vector '(a . b))
=> #(a b))
(check (cons->vector 'foo)
=> #f)
(check (vector-filter odd? #(1 2 3 4 5 6 7 8 9) start: 1 end: 7)
=> #(3 5 7))
))
)))
10 changes: 5 additions & 5 deletions src/std/misc/vector.ss
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
;;; return the most index such that all previous vector elements in the interval [start, env)
;;; satisfy the predicate, or the start if none does.
(def (vector-most-index pred? vector start: (start 0) end: (end (vector-length vector)))
(most-integer (lambda (i) (pred? (vector-ref vector i))) start end))
(least-integer (lambda (i) (not (pred? (vector-ref vector i)))) start end))

;;; Copy a vector if necessary: return the same if no change in start and end requested.
(def (maybe-subvector vector (start 0) (end #f))
Expand All @@ -63,9 +63,9 @@
(function (vector-ref vector i)))))

(def (subvector-reverse-for-each/index function vector start: (start 0) end: (end #f))
(def end (or end (vector-length vector)))
(let (end (or end (vector-length vector)))
(for ((i (in-iota (- end start) (- end 1) -1)))
(function i (vector-ref vector i))))
(function i (vector-ref vector i)))))

(def (subvector->list vector start: (start 0) end: (end #f))
(with-list-builder (c!) (subvector-for-each c! vector start: start end: end)))
Expand All @@ -82,5 +82,5 @@
(list->vector
(with-list-builder (c)
(for (i (in-range start end))
(def e (vector-ref v i))
(when (pred? e) (c e))))))
(let (e (vector-ref v i))
(when (pred? e) (c e)))))))

0 comments on commit 78b87f7

Please sign in to comment.