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.

(define-type integer integer?)
(define-type string string?)
(define-type vector vector?)
(define-type hash-table hash-table?)
(define-generic (back-to (integer x)) (o string->number list->string (🍛 map integer->char)))
(define-generic (back-to (string x)) list->string)
(define-generic (back-to (vector x)) list->vector)
(define-generic (back-to (hash-table x)) alist->hash-table)
(define-generic (->list (integer x)) (map char->integer (string->list (number->string x))))
(define-generic (->list (string x)) (string->list x))
(define-generic (->list (vector x)) (vector->list x))
(define-generic (->list (hash-table x)) (hash-table->alist x))

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