Code Aquarium

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

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

無いっぽいので...

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