diff options
Diffstat (limited to 'gnu/home-services/utils.scm')
-rw-r--r-- | gnu/home-services/utils.scm | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/gnu/home-services/utils.scm b/gnu/home-services/utils.scm index 3e490a0515..f13133a7ae 100644 --- a/gnu/home-services/utils.scm +++ b/gnu/home-services/utils.scm @@ -24,7 +24,8 @@ #:export (maybe-object->string object->snake-case-string - object->camel-case-string)) + object->camel-case-string + list->human-readable-list)) (define (maybe-object->string object) "Like @code{object->string} but don't do anyting if OBJECT already is @@ -75,3 +76,30 @@ STYLE can be three `@code{lower}', `@code{upper}', defaults to (cons (first splitted-string) (map string-capitalize (cdr splitted-string)))))))))) + +(define* (list->human-readable-list lst + #:key + (cumulative? #f) + (proc identity)) + "Turn a list LST into a sequence of terms readable by humans. +If CUMULATIVE? is @code{#t}, use ``and'', otherwise use ``or'' before +the last term. + +PROC is a procedure to apply to each of the elements of a list before +turning them into a single human readable string. + +@example +(list->human-readable-list '(1 4 9) #:cumulative? #t #:proc sqrt) +@result{} \"1, 2, and 3\" +@end example + +yields:" + (let* ((word (if cumulative? "and " "or ")) + (init (append (drop-right lst 1)))) + (format #f "~a" (string-append + (string-join + (map (compose maybe-object->string proc) init) + ", " 'suffix) + word + (maybe-object->string (proc (last lst))))))) + |