-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtins.lisp
executable file
·218 lines (191 loc) · 6.53 KB
/
builtins.lisp
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
214
215
216
217
218
(in-package :pipeline)
;; (defun tee (&rest streams)
;; (lambda ()
;; (uiop:copy-stream-to-stream *standard-input*
;; (apply #'make-broadcast-stream
;; *standard-output*
;; streams)
;; :linewise t)))
(defun tee/error ()
(tee *error-output*))
;; (defmacro broadcast (&rest stream-forms)
;; "Build a function that broadcasts *STANDARD-INPUT* to all STREAM-FORMS.
;; STREAM-FORMS is a list of forms evaluating to streams, at
;; function call time (not at function creation time).
;; In STREAM-FORMS, some symbols are treated specially:
;; - :OUTPUT and T designate *STANDARD-OUTPUT*
;; - :ERROR designates *ERROR-OUTPUT*"
;; (let ((stream-forms
;; (remove nil
;; (sublis '((t . *standard-output*)
;; (:output . *standard-output*)
;; (:error . *error-output*))
;; stream-forms))))
;; (with-gensyms (stream rebind)
;; `(bricabrac.sdl2.event-loop:with-captured-bindings
;; (,rebind
;; ,@(set-difference
;; (remove-if-not #'symbolp
;; stream-forms)
;; '(*standard-output* *error-output*)))
;; (lambda ()
;; (,rebind
;; (let ((,stream (make-broadcast-stream ,@stream-forms)))
;; (unwind-protect (uiop:copy-stream-to-stream *standard-input*
;; ,stream
;; :linewise t)
;; (close ,stream)))))))))
(export
(defun map-lines (function)
(loop
for line = (read-line *standard-input* nil)
while line
do (funcall function line))))
(defmacro with-read-loop ((value read-fn) &body body)
(alexandria:with-gensyms (sentinel)
`(loop
named ,(gensym)
with ,sentinel = (gensym)
for ,value = (,(ecase read-fn ((read read-line read-char) read-fn))
*standard-input*
nil
,sentinel)
until (eq ,value ,sentinel)
do (progn ,@body))))
(defmacro lambda-line ((line) &body body)
`(lambda ()
(with-read-loop (,line read-line)
(locally ,@body))))
(defmacro lambda-line-print ((line) &body body)
(alexandria:with-gensyms (transform)
`(lambda ()
(with-read-loop (,line read-line)
(let ((,transform (locally ,@body)))
(when ,transform
(princ ,transform)
(terpri)))))))
(defmacro do-lines ((line) &body body)
`(with-read-loop (,line read-line)
(locally ,@body)))
(export
(defun each-line (function)
(lambda-line (line)
(write-line (funcall function line))
(finish-output))))
(export
(defmacro lambda-form ((form) &body body)
`(lambda ()
(with-read-loop (,form read)
(locally ,@body)))))
(export
(defmacro lambda-form-print ((form) &body body)
(alexandria:with-gensyms (value)
`(lambda ()
(with-read-loop (,form read)
(let ((,value (progn ,@body)))
(write ,value :escape t)
(terpri)))))))
(export
(defun each-form (function)
(lambda-form (form)
(funcall function form))))
(export
(defun feed (sequence &optional (separator #\newline))
(lambda ()
(map ()
(lambda (u)
(write u :escape nil)
(write separator :escape nil))
sequence))))
(export
(defun read-form ()
(lambda-form (form) (throw :pipeline form))))
(export
(defun from-file (file)
(lambda ()
(with-open-file (*standard-input* file)
(with-read-loop (line read-line)
(write-sequence line *standard-output*)
(terpri))))))
(export
(defun to-file (file)
(lambda ()
(with-open-file (*standard-output*
file
:direction :output
:if-exists :supersede)
(with-read-loop (line read-line)
(write-sequence line *standard-output*)
(terpri))
(pathname *standard-output*)))))
(export
(defun line-collector (&optional (type 'simple-vector))
(lambda ()
(let ((vector (make-array 256 :fill-pointer 0 :adjustable t)))
(with-read-loop (line read-line)
(vector-push-extend line vector (array-total-size vector)))
(coerce vector type)))))
(defun keep-regex (regex)
(let ((scanner (ppcre:create-scanner regex)))
(lambda-line (line)
(when (ppcre:scan scanner line)
(write-line line)))))
(export
(defun reservoir-sampling (n)
(let ((sample (make-array n :initial-element nil)) (i 0))
(lambda (item)
(unless (eq item sample)
(if (<= (incf i) n)
(setf (aref sample (1- i)) item)
(when (< (random i) n)
(setf (aref sample (random n)) item))))
sample))))
(export
(defun reservoir-sampling-pipe (size &optional (on-result #'print))
(check-type size (integer 1))
(check-type on-result (or function symbol))
(let ((sampler (reservoir-sampling size))
(callback (etypecase on-result
(symbol (symbol-function on-result))
(function on-result))))
(check-type callback function)
(lambda ()
(with-read-loop (line read-line)
(funcall sampler line))
(funcall callback (funcall sampler sampler))))))
(export
(defun print-sequence (sequence)
(map () #'print sequence)))
(export
(defun sequence-emitter (&optional (separator #\newline))
(lambda (&aux (first t))
(let ((sequence (read)))
(map
()
(lambda (v)
(cond
(first (setq first nil))
(t (princ separator)))
(princ v))
sequence)))))
(export
(defun signal-each-line ()
(with-read-loop (line read-line)
(signal-result line))))
(export
(defun uniq (&key (key #'identity) (test #'equalp) (stop-after 10) (on-result #'print))
(let ((hash (make-hash-table :test test)))
(lambda (&aux (counter stop-after))
(block nil
(with-read-loop (line read-line)
(let ((value (funcall key line)))
(multiple-value-bind (_ e) (gethash value hash)
(declare (ignore _))
(cond
(e
(when (<= (decf counter) 0)
(return)))
(t
(setf counter stop-after)
(setf (gethash value hash) line)))))))
(funcall on-result (alexandria:hash-table-values hash))))))