forked from bldl/magnolisp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.rkt
179 lines (142 loc) · 4.92 KB
/
util.rkt
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
#lang racket/base
(require racket/dict racket/match
(for-syntax racket/base syntax/parse))
(require "util/module.rkt")
(provide (all-from-out "util/module.rkt"))
(require* "util/assert.rkt")
(require* "util/print.rkt")
(require* "util/let.rkt")
(define-syntax* fix
(syntax-rules ()
((_ fn arg ...)
(lambda rest (apply fn arg ... rest)))))
;; Like `compose1`, but in reverse order.
(define* (compose1-> . fs)
(apply compose1 (reverse fs)))
;; Threads `v` through functions `fs`.
(define* (thread1-> v . fs)
((apply compose1-> fs) v))
;; A threading macro as suggested by Alex Knauth.
(define-simple-macro* (let-> var:id expr:expr ...+)
(let* ([var expr] ...) var))
;; As in Carl Eastlund's Mischief.
(define-syntax-rule* (values-of e)
(call-with-values
(lambda () (#%expression e))
list))
;; True iff any of the specified predicates matches the specified
;; datum. Implemented using a macro for efficiency.
(define-syntax-rule*
(any-pred-holds p ... x)
(or (p x) ...))
;; True iff all of the specified predicates match the specified
;; datum. Implemented using a macro for efficiency.
(define-syntax-rule*
(every-pred-holds p ... x)
(and (p x) ...))
(define-syntax-rule*
(no-pred-holds p ... x)
(not (or (p x) ...)))
(define-syntax-rule*
(apply-values f-expr gen-expr)
(call-with-values (thunk gen-expr) f-expr))
(define-syntax-rule* (may-fail b ...)
(with-handlers ([exn:fail? (lambda (e) #f)])
b ...))
(define-syntax-rule*
(define-values-from-list (id ...) lst-expr)
(define-values (id ...) (apply values lst-expr)))
(define-syntax-rule*
(define-values-from-cons (id-1 id-2) cons-expr)
(define-values (id-1 id-2)
(let ((x cons-expr))
(values (car x) (cdr x)))))
(define-syntax-rule* (matches? e pat ...)
(match e (pat #t) ... (_ #f)))
(define* (path-basename fn)
(define-values (p f dir?) (split-path fn))
f)
(define* (path-basename-as-string fn)
(path->string (path-basename fn)))
(define* (path-drop-suffix f)
(path-replace-suffix f ""))
(define* (path-basename-only fn)
(path-drop-suffix (path-basename fn)))
(define* (path-basename-only-as-string fn)
(path->string (path-basename-only fn)))
(define* (string-underscorify s)
(regexp-replace* #rx"[-]" s "_"))
(define* (symbolic-identifier=? a b)
(eq? (syntax-e a) (syntax-e b)))
(define* (hash-merge! h . others)
(for ((other others))
(for (([k v] other))
(hash-set! h k v)))
(void))
(define* (hash-merge h . others)
(for ((other others))
(for (([k v] other))
(set! h (hash-set h k v))))
h)
(define* (hash-merge-2 h-1 h-2)
(cond
((hash-empty? h-1) h-2)
((hash-empty? h-2) h-1)
(else (hash-merge h-1 h-2))))
(define* (hash-set/assocs h assocs)
(for ((p assocs))
(set! h (hash-set h (car p) (cdr p))))
h)
(define* (list-map-last f lst)
(when (null? lst)
(error 'list-map-last "expected non-empty list"))
(define r '())
(let loop ((h (car lst))
(t (cdr lst)))
(if (null? t)
(set! r (cons (f h) r))
(begin
(set! r (cons h r))
(loop (car t) (cdr t)))))
(reverse r))
(require (for-syntax syntax/for-body))
;; Different syntax from 'for' in that 'empty-expr' is an extra
;; expression for constructing an empty dictionary. This is required
;; as a dictionary is an abstract concept. Only works for immutable
;; dictionaries.
(define-syntax* (for/dict stx)
(syntax-case stx ()
[(_ empty-expr (clause ...) . body)
(with-syntax ([original stx]
[((pre-body ...) post-body)
(split-for-body stx #'body)])
(syntax/loc stx
(for/fold/derived
original
([d empty-expr])
(clause ...)
pre-body ...
(let-values ([(k v) (begin . post-body)])
(dict-set d k v)))))]))
#|
Copyright 2009 Helsinki Institute for Information Technology (HIIT)
and the authors. All rights reserved.
Authors: Tero Hasu <tero.hasu@hut.fi>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
|#