読者です 読者をやめる 読者になる 読者になる

Code Aquarium

minazoko's blog -*- 水底のブログ -*-

(Racket) values->list マクロとか

Racket Scheme Lisp

無いっぽいので...

(define-syntax values->list
  (syntax-rules ()
    ((_ vs) (call-with-values (thunk vs) list))))

(define-syntax values->vector
  (syntax-rules ()
    ((_ vs) (call-with-values (thunk vs) vector))))

(define-syntax values->hash
  (syntax-rules ()
    ((_ vs) (call-with-values (thunk vs) hash))))

(define-syntax values->struct
  (syntax-rules ()
    ((_ struct-type vs)
     (apply struct-type
            (call-with-values (thunk vs) list)))))

つかう

(values->list (values 10 20)) ;=> (10 20)

(values->vector (values 10 20)) ;=> #(10 20)

(values->hash (values 'a 10 'b 20)) ;=> #hash((b . 20) (a . 10))

(struct person (name age email) #:transparent)
(values->struct person (values "Yamada" 42 "hoge@foo.bar"))
 ;;=> #(person "Yamada" 42 "hoge@foo.bar")