💾 Archived View for idiomdrottning.org › as-list captured on 2023-05-24 at 18:31:54. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2021-11-30)

-=-=-=-=-=-=-

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)))

over

define-curry

match-define