;;;; comma-list-utils.scm -*- scheme -*- ;;;; Kon Lovett, Jan '21 (module comma-list-utils (;export list->comma-string comma-string->list list-comma-join make-comma-string) (import scheme utf8 (chicken base) (chicken type) (only (chicken string) string-split) (srfi 1) (only utf8-srfi-13 string-null? string-concatenate string-trim-both) (only utf8-srfi-14 char-set:whitespace) (only list-utils list-flatten)) ;string-utils (: string-trim-whitespace-both (string -> string)) (define (string-trim-whitespace-both str) (string-trim-both str char-set:whitespace) ) ;;; (: list->comma-string (list #!optional boolean string -> string)) (: comma-string->list (string -> list)) (: list-comma-join (#!rest -> string)) (: make-comma-string (fixnum #!optional string -> string)) ;; (define not-string-null? (o not string-null?)) ;the input list is flattened, so nulls are removed ;comma : ", " so "a, b" ;allow-empty? : do not remove string-nulls ; (define (list->comma-string ls #!optional allow-empty? (comma ", ")) ;flatten possibly mixed list of string | list (let* ( (ls (map ->string (list-flatten ls))) (ls (if allow-empty? ls (filter not-string-null? ls))) ) (if (null? ls) "" (string-concatenate (intersperse ls comma)) ) ) ) (define (comma-string->list str #!optional (proc string-trim-whitespace-both)) (map! proc (string-split str ",")) ) (define (list-comma-join . rest) (list->comma-string rest) ) (define (make-comma-string len #!optional (str "?")) (list->comma-string (make-list len str)) ) ) ;module comma-list-utils