-
Notifications
You must be signed in to change notification settings - Fork 141
/
utils.ss
214 lines (163 loc) · 4.56 KB
/
utils.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
;; yDiff - a language-aware tool for comparing programs
;; Copyright (C) 2011 Yin Wang ([email protected])
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;-------------------------------------------------------------
; utilities
;-------------------------------------------------------------
(define-syntax letv
(syntax-rules ()
[(_ () body ...)
(begin body ...)]
[(_ ([(e1 e2* ...) e3] bd* ...) body ...)
(let-values ([(e1 e2* ...) e3])
(letv (bd* ...) body ...))]
[(_ ([e1 e2] bd* ...) body ...)
(let ([e1 e2])
(letv (bd* ...) body ...))]))
(define-syntax first-val
(syntax-rules ()
[(_ e)
(letv ([(x y) e]) x)]))
(define-syntax second-val
(syntax-rules ()
[(_ e)
(letv ([(x y) e]) y)]))
(define *debug* #f)
(define-syntax peek
(syntax-rules ()
[(_ name args ...)
(if *debug*
(begin
(printf "~s: ~s = ~s~n" name 'args args)
...)
(void))]))
;; utility for error reporting
(define fatal
(lambda (who . args)
(printf "~s: " who)
(for-each display args)
(display "\n")
(error who "")))
; foldl of Racket has a bug!
; (foldl (lambda (x y) x) 0 '(1 2 3 4))
; => 4
; Don't use it!
(define foldl2
(lambda (f x ls)
(cond
[(null? ls) x]
[else
(foldl2 f (f x (car ls)) (cdr ls))])))
; (foldl2 + 0 '(1 2 3 4 ))
(define orf
(lambda (a b)
(or a b)))
(define char->string
(lambda (c)
(list->string (list c))))
(define read-file
(lambda (filename)
(let ([port (open-input-file filename #:mode 'text)])
(let loop ([line (read-line port)]
[all ""])
(cond
[(eof-object? line) all]
[else
(loop (read-line port)
(string-append all line "\n"))])))))
(define new-progress
(lambda (size)
(let ([counter 0])
(lambda (x)
(cond
[(string? x)
(display x)
(display "\n")
(flush-output)]
[(= 0 (remainder counter size))
(set! counter (+ x counter))
(display ".")
(flush-output)]
[else
(set! counter (+ x counter))])))))
;;----------------- multi dimensional eq hash --------------------
(define hash-put!
(lambda (hash key1 key2 v)
(cond
[(hash-has-key? hash key2)
(let ([inner (hash-ref hash key2)])
(hash-set! inner key1 v))]
[else
(let ([inner (make-hasheq)])
(hash-set! inner key1 v)
(hash-set! hash key2 inner))])))
(define hash-get
(lambda (hash key1 key2)
(cond
[(hash-has-key? hash key2)
(let ([inner (hash-ref hash key2)])
(cond
[(hash-has-key? inner key1)
(hash-ref inner key1)]
[else #f]))]
[else #f])))
(define hash-put2!
(lambda (hash key1 key2 v)
(cond
[(hash-has-key? hash key2)
(let ([inner (hash-ref hash key2)])
(hash-set! inner key1 v))]
[else
(let ([inner (make-hash)])
(hash-set! inner key1 v)
(hash-set! hash key2 inner))])))
(define hash-get2
(lambda (hash key1 key2)
(cond
[(hash-has-key? hash key2)
(let ([inner (hash-ref hash key2)])
(cond
[(hash-has-key? inner key1)
(hash-ref inner key1)]
[else #f]))]
[else #f])))
(define predand
(lambda preds
(lambda (x)
(cond
[(null? preds) #t]
[((car preds) x)
((apply predand (cdr preds)) x)]
[else #f]))))
(define predor
(lambda preds
(lambda (x)
(cond
[(null? preds) #f]
[((car preds) x) #t]
[else
((apply predor (cdr preds)) x)]))))
(define set-
(lambda (s1 s2)
(cond
[(null? s1) '()]
[(memq (car s1) s2)
(set- (cdr s1) s2)]
[else
(cons (car s1) (set- (cdr s1) s2))])))
(define string-join
(lambda (ls sep)
(cond
[(null? ls) ""]
[else
(string-append (car ls) sep (string-join (cdr ls) sep))])))
; (string-join (list "a" "b" "c") ",")