;;;; list-utils.comma.scm -*- scheme -*- ;;;; Kon Lovett, Jan '21 (module (list-utils comma) (;export list->comma-string comma-string->list list-comma-join make-comma-string) (import scheme utf8) (import (chicken base)) (import (chicken type)) (import (only (chicken string) string-split)) (import (only (srfi 1) map! make-list remove)) (import (only utf8-srfi-13 string-null? string-concatenate string-trim-both)) (import (only utf8-srfi-14 char-set:whitespace)) (import (check-errors sys)) (: 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)) ;string-utils (: string-trim-whitespace-both (string -> string)) (define (string-trim-whitespace-both str) (string-trim-both str char-set:whitespace) ) ;; (define-constant COMMA-IN ",") (define-constant COMMA-OUT ", ") ;input nulls are removed ;comma : ", " so "a, b" ;allow-empty? : do not remove string-nulls ; (define (*list->comma-string ls allow-empty? comma) (let* ((ls (remove null? ls)) (ls (map ->string ls)) (ls (if allow-empty? ls (remove string-null? ls))) ) (if (null? ls) "" (string-concatenate (intersperse ls comma)) ) ) ) ;input nulls are removed ;comma : ", " so "a, b" ;allow-empty? : do not remove string-nulls ; (define (list->comma-string ls #!optional allow-empty? (comma COMMA-OUT)) (*list->comma-string (check-list 'list->comma-string ls) allow-empty? (check-string 'list->comma-string comma)) ) (define (comma-string->list str #!optional (trim string-trim-whitespace-both)) ;unless trim proc does trim then could be surrounding ws (map! trim (string-split (check-string 'comma-string->list str) COMMA-IN)) ) (define (list-comma-join . rest) (*list->comma-string rest #f COMMA-OUT) ) (define (make-comma-string len #!optional (def "?")) (*list->comma-string (make-list len def) #f COMMA-OUT) ) ) ;module (list-utils comma)