racket/collects/racket/string.rkt
Eli Barzilay da1c334f70 Use `apply apply' which is slightly faster for most inputs than using
`list*'.  (And add specialized cases for small inputs.)
2011-06-14 19:02:30 -04:00

23 lines
899 B
Racket

#lang scheme/base
(provide string-append* string-join)
(define string-append*
(case-lambda [(strs) (apply string-append strs)] ; optimize common case
[(s1 strs) (apply string-append s1 strs)]
[(s1 s2 strs) (apply string-append s1 s2 strs)]
[(s1 s2 s3 strs) (apply string-append s1 s2 s3 strs)]
[(s1 s2 s3 s4 strs) (apply string-append s1 s2 s3 s4 strs)]
[(str . strss) (apply apply string-append str strss)]))
(require (only-in scheme/list add-between))
(define (string-join strs sep)
(cond [(not (and (list? strs) (andmap string? strs)))
(raise-type-error 'string-join "list-of-strings" strs)]
[(not (string? sep))
(raise-type-error 'string-join "string" sep)]
[(null? strs) ""]
[(null? (cdr strs)) (car strs)]
[else (apply string-append (add-between strs sep))]))