Idiomdrottning’s homepage

as-list

Update: Now part of brev-separate.


Here is a functional combinator for Scheme that lets its arguments treat their arguments as if they were lists.

((as-list (c filter odd?)) 130752)

⇒ 1375

((as-list cdr reverse) 23311358)

⇒ 5311332

((as-list delete-duplicates) 23311358)

⇒ 23158

((as-list append) 11 22)

⇒ 1122

(define (vowel? l) ((as-list (c member l)) "aeiou"))
((as-list (c filter vowel?)) "magnetic mountaintop")

⇒ “aeiouaio”

Together with over:

((as-list (over (if (vowel? x) x (char-upcase x)))) "fleet foxes")

⇒ “FLeeT FoXeS”

Here is the implementation for Chicken. It uses c from define-curry, and match-define.

(define (record? x)
  (and
   (not (##sys#immediate? x))
   (##sys#structure? x (##sys#slot x 0))))

(match-define
 ((list-> (? integer? x))
   (o string->number list->string
      (c map (o (cut string-ref <> 0) number->string))))
 ((list-> (? string? x)) list->string)
 ((list-> (? vector? x)) list->vector)
 ((list-> (? hash-table? x)) alist->hash-table)
 ((list-> (? record? r)) (fn (apply ##sys#make-structure (##sys#slot r 0) x)))
 ((list-> (? list? x)) identity))

(match-define
 ((->list (? integer? x))
   (map (o string->number string) (string->list (number->string x))))
 ((->list (? string? x)) (string->list x))
 ((->list (? vector? x)) (vector->list x))
 ((->list (? hash-table? x)) (hash-table->alist x))
 ((->list (? record? x)) (map (cut ##sys#slot x <>) (iota (sub1 (##sys#size x)) 1)))
 ((->list (? list? x)) x))

(define ((as-list . procs) . xs)
  (let ((ret (apply (apply compose procs) (map ->list xs))))
    (if (list? ret)
	 ((list-> (car xs)) ret)
	 ret)))