guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

new proc for string-fun.scm: `mapconcat'


From: thi
Subject: new proc for string-fun.scm: `mapconcat'
Date: Wed, 7 Feb 2001 15:15:14 -0800

please see below for `mapconcat'.  i've re-implemented this several
times (although less generally), and thought it might be nice to add to
ice-9/string-fun.scm.  some example usage:

guile> (mapconcat id '(1 2 3) "x")
"1x2x3"
guile> (mapconcat id '(a b c) "x")
"axbxc"
guile> (mapconcat id '((nested) (funky) (stuff)) "x")
"(nested)x(funky)x(stuff)"
guile> (mapconcat id #(1 2 3) "x")
"1x2x3"
guile> (mapconcat id "123" "x")
"1x2x3"
guile> (mapconcat (lambda (n) (- n 42)) '(1 2 3) "x")
"-41x-40x-39"

if "mapconcat" is not a sufficiently guilish name, maybe
"string-map-append" (or some permutation) would work....

thi


________________________________________
;;; {String Fun: mapconcat}

;;; Snarfed from Emacs Lisp function by same name.

(define-public (mapconcat procedure sequence separator)
  "Apply PROCEDURE to each element of SEQUENCE, and concat the results as
strings.  In between each pair of results, stick in SEPARATOR.  Thus, \" \" as
SEPARATOR results in spaces between the values returned by FUNCTION.  SEQUENCE
may be a list, a vector, a bool-vector, or a string."
  (let ((rev (reverse
              (map (lambda (item)
                     (let ((val (procedure item)))
                       (cond ((string? val) val)
                             ((char? val) (make-string 1 val))
                             (else (with-output-to-string
                                     (lambda () (write val)))))))
                   (cond ((list? sequence) sequence)
                         ((vector? sequence) (vector->list sequence))
                         ((string? sequence) (string->list sequence))
                         (else
                          ;; fixme:ttn -- what is guile's error convention?
                          (error "bad sequence type")))))))
    (apply string-append
           (let loop ((s (cdr rev))
                      (acc (list (car rev))))
             (if (null? s)
                 acc
                 (loop (cdr s)
                       (cons (car s)
                             (cons separator acc))))))))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]