-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathvalues.ss
49 lines (40 loc) · 1.6 KB
/
values.ss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
;;; -*- Gerbil -*-
;;; © fare, vyzo
;;; Utility functions and accessors for multiple values
(import ./sugar)
(export (rename: first-value% first-value)
(rename: second-value% second-value)
(rename: nth-value% nth-value)
(rename: values->vector% values->vector)
vector->values
list->values ;; NB: values->list is builtin
(rename: values->cons% values->cons)
cons->values)
(def (first-value x . _) x)
(defrules first-value% ()
((_ form) (with ((values x . _) form) x))
((_ . _) (syntax-error "Bad syntax; first-value expects single argument"))
(_ first-value))
(def (second-value _1 x . _) x)
(defrules second-value% ()
((_ form) (with ((values _ x . _) form) x))
((_ . _) (syntax-error "Bad syntax; second-value expects single argument"))
(_ second-value))
(defrules nth-value% ()
((_ n form) (with ((values . x) form) (list-ref x n)))
((_ . _) (syntax-error "Bad syntax; nth-value expects two arguments"))
(_ nth-value_))
(def (nth-value n vals) (nth-value% n vals))
(defrules values->vector% ()
((_ form) (list->vector (values->list form)))
((_ . _) (syntax-error "Bad syntax; values->vector expects one argument"))
(_ values->vector))
(def (values->vector vals) (values->vector% vals))
(def (vector->values v) (list->values (vector->list v)))
(def (list->values l) (apply values l))
(defrules values->cons% ()
((_ form) (with ((values a b) form) (cons a b)))
((_ . _) (syntax-error "Bad syntax; values->cons expects one argument"))
(_ values->cons))
(def (values->cons vals) (values->cons% vals))
(def (cons->values x) (values (car x) (cdr x)))