aboutsummaryrefslogtreecommitdiff
path: root/dump/utils.scm
blob: 1368a67a214bb9072726328fd981fd44f94c3f47 (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
(define-module (dump utils)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:use-module (ice-9 match)
  #:export (translate-forms
            collect-forms
            map-alist))

(define (translate-forms from translator x)
  "Recursively pass (FROM ...) forms in source X to TRANSLATOR, and
replace them with the return value."
  (syntax-case x ()
    ;; Handle translation base case.
    ((head tail ...) (eq? (syntax->datum #'head)
                          from)
     (translator x))
    ;; Recurse.
    ((head tail ...)
     (cons (translate-forms from translator #'head)
           (map (cut translate-forms from translator <>)
                #'(tail ...))))
    ;; Handle leaf base case.
    (leaf #'leaf)))

(define (key->assoc-ref alist x)
  "Recursively translate (key k) forms in source X to (assoc-ref ALIST
k) forms."
  (translate-forms 'key
                   ;; (syntax-rules (key)
                   ;;   ((key k) (assoc-ref alist k)))
                   (lambda (x)
                     (syntax-case x (key)
                       ((key k) #`(assoc-ref #,alist 'k))))
                   x))

(define (collect-forms target x)
  "Recursively collect (TARGET ...) forms in source X and return them
as a list."
  (syntax-case x ()
    ;; Handle collection base case.
    ((head tail ...) (eq? (syntax->datum #'head)
                          target)
     (list x))
    ;; Recurse.
    ((head tail ...)
     (append (collect-forms target #'head)
             (append-map (cut collect-forms target <>) #'(tail ...))))
    ;; Handle leaf base case.
    (leaf (list))))

(define (collect-keys x)
  "Recursively collect (key k) forms from source X and return as a
list of all K."
  (map (syntax-rules (key)
         ((key k) 'k))
       (collect-forms 'key x)))

(define (alist-delete* alist keys)
  "Delete entries from ALIST whose key is equal (in the sense of
equal?) to any in KEYS, a list."
  (remove (match-lambda
            ((key . value)
             (member key keys))
            (x (error "malformed alist element" x)))
          alist))

(define-syntax map-alist
  (lambda (x)
    "Transform (aka map) ALIST, an association list, into another
association list. The returned association list may contain multiple
associations for the same key. equal? is used in all association list
key comparisons.

Syntax:

(map-alist alist
  (verb key expression) ...
  (else=> proc))

VERB must be one of set, filter-set, multiset and remove.

For the set VERB, KEY is set to the result of evaluating
EXPRESSION. Multiple set verbs on the same key will result in multiple
associations for that key.

For the filter-set VERB, KEY is set to the result of evaluating
EXPRESSION only if that result is not #f.

For the multiset VERB, EXPRESSION must return a list and KEY is
associated multiple times once with each element of the returned list.

For the remove VERB, KEY is discarded.

EXPRESSIONs must reference elements of ALIST using (key k) forms where
K is the key to be referenced from ALIST. K must not be quoted. That
is, if K is the symbol 'bar, it must be referenced as (key bar), not
as (key 'bar).

The else=> clause is optional.

If the else=> clause is present, PROC is passed all pairs of ALIST
that are not set by an earlier (verb key expression) action. PROC must
return #f or a pair to replace its input pair. If PROC returns #f,
that pair is discarded.

If the else=> clause is absent, all unset pairs are discarded.

Example:

(map-alist '((\"foo\" . 1)
             (bar . 2)
             (foobar . 5)
             (fubar . 3))
  (set spam (1+ (key \"foo\")))
  (set ham (* 2 (key bar)))
  (set eggs (* 3 (key bar)))
  (set aal (+ (key \"foo\")
              (key bar)))
  (multiset vel (iota (* 2 (key bar))))
  (remove foobar)
  (else=> (match-lambda
            ((key . value)
             (cons key (expt 2 value))))))
=> ((spam . 2) (ham . 4) (eggs . 6) (aal . 3)
    (vel . 0) (vel . 1) (vel . 2) (vel . 3) (fubar . 8))"
    (syntax-case x ()
      ((_ alist actions ...)
       ;; TODO: Check that all actions are valid.
       #`(let ((evaluated-alist alist))
           (append (remove (match-lambda
                             ;; Filter out results of filter-set actions.
                             ((key . #f)
                              (member key '#,(filter-map (lambda (action)
                                                           (syntax-case action (filter-set)
                                                             ((filter-set key expression) #'key)
                                                             (_ #f)))
                                                         #'(actions ...))))
                             (_ #f))
                           ;; Do set and filter-set.
                           `#,(filter-map (lambda (action)
                                            (syntax-case action (set filter-set)
                                              ((set key expression)
                                               #`(key . ,#,(key->assoc-ref #'evaluated-alist #'expression)))
                                              ((filter-set key expression)
                                               #`(key . ,#,(key->assoc-ref #'evaluated-alist #'expression)))
                                              (_ #f)))
                                          #'(actions ...)))
                   ;; Do multiset.
                   #,@(filter-map (lambda (action)
                                    (syntax-case action (multiset)
                                      ((multiset key expression)
                                       #`(map (cut cons 'key <>)
                                              #,(key->assoc-ref #'evaluated-alist #'expression)))
                                      (_ #f)))
                                  #'(actions ...))
                   ;; Apply else=> procedure on unspecified keys. If
                   ;; no else=> procedure is specified, delete
                   ;; unspecified keys.
                   (filter-map #,(or (any (lambda (action)
                                            (syntax-case action (else=>)
                                              ((else=> proc) #'proc)
                                              (_ #f)))
                                          #'(actions ...))
                                     #'(const #f))
                               ;; The unspecified part of the input
                               ;; alist
                               (alist-delete* evaluated-alist
                                              (list
                                               ;; Keys that were referenced
                                               #,@(append-map (lambda (action)
                                                                (syntax-case action ()
                                                                  ((_ key expression)
                                                                   (collect-keys #'expression))
                                                                  (_ '())))
                                                              #'(actions ...))
                                               ;; Keys that were deleted
                                               #,@(filter-map (lambda (action)
                                                                (syntax-case action (remove)
                                                                  ((remove key) #''key)
                                                                  (_ #f)))
                                                              #'(actions ...))
                                               ;; Keys that were set
                                               #,@(filter-map (lambda (action)
                                                                (syntax-case action ()
                                                                  ((_ key expression) #''key)
                                                                  (_ #f)))
                                                              #'(actions ...)))))))))))