Adjust the keyword names for string-join' following the change to add-between'.

This commit is contained in:
Eli Barzilay 2012-06-26 13:45:10 -04:00
parent 826a5f360f
commit 0c78194267
3 changed files with 22 additions and 19 deletions

View File

@ -20,17 +20,18 @@
(define none (gensym))
(define (string-join strs [sep " "]
#:first [first none] #:last [last none]
#:before-last [before-last none])
#:before-first [before-first none]
#:before-last [before-last sep]
#:after-last [after-last none])
(unless (and (list? strs) (andmap string? strs))
(raise-argument-error 'string-join "(listof string?)" strs))
(unless (string? sep)
(raise-argument-error 'string-join "string?" sep))
(let* ([r (cond [(or (null? strs) (null? (cdr strs))) strs]
[(eq? before-last none) (add-between strs sep)]
[else (add-between strs sep #:before-last before-last)])]
[r (if (eq? last none) r (append r (list last)))]
[r (if (eq? first none) r (cons first r))])
(let* ([r (if (or (null? strs) (null? (cdr strs)))
strs
(add-between strs sep #:before-last before-last))]
[r (if (eq? after-last none) r (append r (list after-last)))]
[r (if (eq? before-first none) r (cons before-first r))])
(apply string-append r)))
;; Utility for the functions below: get a string or a regexp and return a list

View File

@ -391,24 +391,26 @@ one between @racket[list] and @racket[list*].
@defproc[(string-join [strs (listof string?)] [sep string? " "]
[#:before-last before-last string? sep]
[#:first first string? ....]
[#:last last string? ....])
[#:before-first before-first string? ""]
[#:before-last before-last string? sep]
[#:after-last after-last string? ""])
string?]{
Appends the strings in @racket[strs], inserting @racket[sep] between
each pair of strings in @racket[strs]. @racket[before-last],
@racket[first], and @racket[last] are analogous to the inputs of
@racket[add-between]: they specify an alternate separator between the
last two strings, a prefix string, and a suffix string respectively.
@racket[before-first], and @racket[after-last] are analogous to the
inputs of @racket[add-between]: they specify an alternate separator
between the last two strings, a prefix string, and a suffix string
respectively.
@mz-examples[#:eval string-eval
(string-join '("one" "two" "three" "four"))
(string-join '("one" "two" "three" "four") ", ")
(string-join '("one" "two" "three" "four") " potato ")
(string-join #:first "Todo: "
'("x" "y" "z") ", " #:before-last " and "
#:last ".")
(string-join '("x" "y" "z") ", "
#:before-first "Todo: "
#:before-last " and "
#:after-last ".")
]}

View File

@ -398,13 +398,13 @@
", " #:before-last " and ")
(test (string-append "Todo: " (cadr strs+res))
string-join (car strs+res)
#:first "Todo: " ", " #:before-last " and ")
#:before-first "Todo: " ", " #:before-last " and ")
(test (string-append (cadr strs+res) ".")
string-join (car strs+res)
", " #:before-last " and " #:last ".")
", " #:before-last " and " #:after-last ".")
(test (string-append "Todo: " (cadr strs+res) ".")
string-join (car strs+res)
#:first "Todo: " ", " #:before-last " and " #:last ".")))
#:before-first "Todo: " ", " #:before-last " and " #:after-last ".")))
;; ---------- string-trim & string-normalize-spaces ----------
(let ()