Skip to content

Commit

Permalink
Merge pull request #885 from gambiteer/vector-fold
Browse files Browse the repository at this point in the history
Add vector-fold{-right}, remove string-{any|every}
  • Loading branch information
feeley committed Jan 31, 2024
2 parents 024ac23 + e6f2495 commit 6cd4fe4
Show file tree
Hide file tree
Showing 8 changed files with 484 additions and 314 deletions.
77 changes: 33 additions & 44 deletions doc/gambit.txi
Original file line number Diff line number Diff line change
Expand Up @@ -4112,83 +4112,72 @@ For example:

@end deffn

@deffn procedure string-set @var{string} @var{k} @var{char}
@deffn procedure vector-fold @var{kons} @var{knil} @var{vec1} @var{vec2} @dots{}

The procedure @code{string-set} returns a new copy of the string
@var{string} with the character at index @var{k} replaced with
@var{char}.
The procedure @var{kons} is iterated over each value in all of the vectors, stopping at the end of the shortest; @var{kons} is applied as @code{(@var{kons} state (vector-ref @var{vec1} i) (vector-ref @var{vec2} i) @dots{})} where @code{state} is the current state value---the current state value begins with @var{knil}, and becomes whatever @var{kons} returned on the previous iteration---and @code{i} is the current index.
The iteration is strictly increasing in the index @code{i}, starting at 0.

For example:

@smallexample
> @b{(define s1 (string #\a #\b #\c #\d))}
> @b{(define s2 (string-set s1 2 #\.))}
> @b{s2}
"ab.d"
> @b{(eq? s1 s2)}
#f
> @b{(vector-fold (lambda (len str) (max (string-length str) len)) 0 '#("a b c" "ab" "" "cde"))}
5
> @b{(vector-fold xcons '() '#(a b c d))}
(d c b a)
> @b{(vector-fold (lambda (counter n) (if (even? n) (+ counter 1) counter)) 0 '#(0 1 2 3 4))}
3
@end smallexample

@end deffn

@deffn procedure string-swap! @var{string} @var{i} @var{j}
@deffn procedure vector-fold-right @var{kons} @var{knil} @var{vec1} @var{vec2} @dots{}

The procedure @code{string-swap!} swaps the characters at index @var{i}
and index @var{j} of the string @var{string}.
Similar to @code{vector-fold}, but strictly decreasing in the index.

For example:

@smallexample
> @b{(define s1 (string #\a #\b #\c #\d #\e))}
> @b{s1}
"abcde"
> @b{(string-swap! s1 0 4)}
> @b{s1}
"ebcda"
> @b{(vector-fold-right xcons '() '#(a b c d))}
(a b c d)
@end smallexample

@end deffn

@deffn procedure string-any @var{pred} @var{s1} @var{s2} @dots{}

The procedure @code{string-any} returns the first non-false value that
the predicate @var{pred} returns when applied to the elements of the strings
@var{s1} @var{s2} @dots{} in order, beginning with index 0 and
stopping at the length of the shortest
string argument. It returns @code{#f} if there is no such non-false value.

@deffn procedure string-set @var{string} @var{k} @var{char}

The procedure @code{string-set} returns a new copy of the string
@var{string} with the character at index @var{k} replaced with
@var{char}.

For example:

@smallexample
> @b{(string-any char-ci=? "abc" "1B")}
#t
> @b{(string-any char=? "abc" "1B")}
#f
> @b{(string-any char-alphabetic? "")}
> @b{(define s1 (string #\a #\b #\c #\d))}
> @b{(define s2 (string-set s1 2 #\.))}
> @b{s2}
"ab.d"
> @b{(eq? s1 s2)}
#f
@end smallexample

@end deffn

@deffn procedure string-every @var{pred} @var{s1} @var{s2} @dots{}
@deffn procedure string-swap! @var{string} @var{i} @var{j}

The procedure @code{string-every} applies
the predicate @var{pred} to the elements of the strings
@var{s1} @var{s2} @dots{} in order, beginning with index 0 and
stopping at the length of the shortest
string argument. If the result of @var{pred} is ever @code{#f}, that is
returned; if the smallest string is empty, then @code{#t} is returned;
otherwise, the last value of @var{pred} is returned.
The procedure @code{string-swap!} swaps the characters at index @var{i}
and index @var{j} of the string @var{string}.

For example:

@smallexample
> @b{(string-every char-ci=? "abc" "aB")}
#t
> @b{(string-every char=? "abc" "aB")}
#f
> @b{(string-every char-alphabetic? "")}
#t
> @b{(define s1 (string #\a #\b #\c #\d #\e))}
> @b{s1}
"abcde"
> @b{(string-swap! s1 0 4)}
> @b{s1}
"ebcda"
@end smallexample

@end deffn
Expand Down
615 changes: 407 additions & 208 deletions lib/_std#.scm

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/gambit#.scm
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,6 @@ started-thread-exception-procedure
started-thread-exception?
step
step-level-set!
string-any
string->char-set
string->char-set!
string->keyword
Expand All @@ -914,7 +913,6 @@ string-ci=?-hash
string-concatenate
string-contains
string-contains-ci
string-every
string-prefix-ci?
string-prefix-length
string-prefix-length-ci
Expand Down Expand Up @@ -1176,6 +1174,8 @@ vector-any
vector-cas!
vector-concatenate
vector-every
vector-fold
vector-fold-right
vector-inc!
vector-set
vector-shrink!
Expand Down
4 changes: 2 additions & 2 deletions lib/gambit/gambit.sld
Original file line number Diff line number Diff line change
Expand Up @@ -1258,12 +1258,10 @@ string->char-set!
string->keyword
string->uninterned-keyword
string->uninterned-symbol
string-any
string-ci=?-hash
string-concatenate
string-contains
string-contains-ci
string-every
string-prefix-ci?
string-prefix-length
string-prefix-length-ci
Expand Down Expand Up @@ -1525,6 +1523,8 @@ vector-any
vector-cas!
vector-concatenate
vector-every
vector-fold
vector-fold-right
vector-inc!
vector-set
vector-shrink!
Expand Down
29 changes: 0 additions & 29 deletions tests/unit-tests/07-vector/string_any.scm

This file was deleted.

29 changes: 0 additions & 29 deletions tests/unit-tests/07-vector/string_every.scm

This file was deleted.

26 changes: 26 additions & 0 deletions tests/unit-tests/07-vector/vector_fold.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(include "#.scm")


(check-equal? (vector-fold (lambda (len str)
(max (string-length str) len))
0
'#("a b c" "ab" "" "cde"))
5)

(check-equal? (vector-fold xcons
'()
'#(a b c d))
'(d c b a))
(check-equal? (vector-fold (lambda (counter n)
(if (even? n) (+ counter 1) counter))
0
'#(0 1 2 3 4))
3)

(check-tail-exn type-exception? (lambda () (vector-fold 4 4 '#())))
(check-tail-exn type-exception? (lambda () (vector-fold xcons 4 '())))
(check-tail-exn type-exception? (lambda () (vector-fold xcons 4 '#()'())))

(check-tail-exn wrong-number-of-arguments-exception? (lambda () (vector-fold)))
(check-tail-exn wrong-number-of-arguments-exception? (lambda () (vector-fold xcons)))
(check-tail-exn wrong-number-of-arguments-exception? (lambda () (vector-fold xcons 0)))
14 changes: 14 additions & 0 deletions tests/unit-tests/07-vector/vector_fold_right.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(include "#.scm")

(check-equal? (vector-fold-right xcons
'()
'#(a b c d))
'(a b c d))

(check-tail-exn type-exception? (lambda () (vector-fold-right 4 4 '#())))
(check-tail-exn type-exception? (lambda () (vector-fold-right xcons 4 '())))
(check-tail-exn type-exception? (lambda () (vector-fold-right xcons 4 '#()'())))

(check-tail-exn wrong-number-of-arguments-exception? (lambda () (vector-fold-right)))
(check-tail-exn wrong-number-of-arguments-exception? (lambda () (vector-fold-right xcons)))
(check-tail-exn wrong-number-of-arguments-exception? (lambda () (vector-fold-right xcons 0)))

0 comments on commit 6cd4fe4

Please sign in to comment.