Browse code
add denest
Ed Langley authored on 07/02/2019 01:56:06
Showing 1 changed files
Showing 1 changed files
... | ... |
@@ -22,7 +22,8 @@ |
22 | 22 |
#:maximizing |
23 | 23 |
#:zipping |
24 | 24 |
#:applying |
25 |
- #:transform-elt)) |
|
25 |
+ #:transform-elt |
|
26 |
+ #:denest)) |
|
26 | 27 |
(in-package :data-lens) |
27 | 28 |
|
28 | 29 |
(declaim |
... | ... |
@@ -155,15 +156,30 @@ |
155 | 156 |
(lambda (it) |
156 | 157 |
(subseq it start end))) |
157 | 158 |
|
159 |
+(defun-ct update (thing fun &rest args) |
|
160 |
+ (apply fun thing args)) |
|
161 |
+ |
|
162 |
+(define-modify-macro updatef (fun &rest args) |
|
163 |
+ update) |
|
164 |
+ |
|
158 | 165 |
(defun-ct transform-head (fun) |
159 | 166 |
(lambda (it) |
160 |
- (list* (funcall fun (car it)) |
|
161 |
- (cdr it)))) |
|
167 |
+ (typecase it |
|
168 |
+ (list (list* (funcall fun (car it)) |
|
169 |
+ (cdr it))) |
|
170 |
+ (vector (let ((result (copy-seq it))) |
|
171 |
+ (prog1 result |
|
172 |
+ (updatef (elt result 0) fun))))))) |
|
162 | 173 |
|
163 | 174 |
(defun-ct transform-tail (fun) |
164 | 175 |
(lambda (it) |
165 |
- (list* (car it) |
|
166 |
- (funcall fun (cdr it))))) |
|
176 |
+ (typecase it |
|
177 |
+ (list (list* (car it) |
|
178 |
+ (funcall fun (cdr it)))) |
|
179 |
+ (vector (let ((result (copy-seq it))) |
|
180 |
+ (prog1 result |
|
181 |
+ (updatef (subseq result 1) |
|
182 |
+ fun))))))) |
|
167 | 183 |
|
168 | 184 |
(defun-ct transform-elt (elt fun) |
169 | 185 |
(lambda (it) |
... | ... |
@@ -177,10 +193,9 @@ |
177 | 193 |
(funcall key-set |
178 | 194 |
(funcall fun key-val))))) |
179 | 195 |
|
180 |
-(defun-ct juxt (fun1 fun2 &rest r) |
|
196 |
+(defun-ct juxt (fun1 &rest r) |
|
181 | 197 |
(lambda (&rest args) |
182 | 198 |
(list* (apply fun1 args) |
183 |
- (apply fun2 args) |
|
184 | 199 |
(when r |
185 | 200 |
(mapcar (lambda (f) |
186 | 201 |
(apply f args)) |
... | ... |
@@ -192,15 +207,25 @@ |
192 | 207 |
(funcall fun2)))) |
193 | 208 |
|
194 | 209 |
(defun-ct derive (diff-fun &key (key #'identity)) |
195 |
- (lambda (list) |
|
196 |
- (cons (cons nil (car list)) |
|
197 |
- (mapcar (lambda (next cur) |
|
198 |
- (cons (funcall diff-fun |
|
199 |
- (funcall key next) |
|
200 |
- (funcall key cur)) |
|
201 |
- next)) |
|
202 |
- (cdr list) |
|
203 |
- list)))) |
|
210 |
+ (lambda (seq) |
|
211 |
+ (typecase seq |
|
212 |
+ (list (cons (cons nil (car seq)) |
|
213 |
+ (mapcar (lambda (next cur) |
|
214 |
+ (cons (funcall diff-fun |
|
215 |
+ (funcall key next) |
|
216 |
+ (funcall key cur)) |
|
217 |
+ next)) |
|
218 |
+ (cdr seq) |
|
219 |
+ seq))) |
|
220 |
+ (vector (coerce (loop for cur = nil then next |
|
221 |
+ for next across seq |
|
222 |
+ if cur |
|
223 |
+ collect (cons (funcall diff-fun |
|
224 |
+ (funcall key next) |
|
225 |
+ (funcall key cur)) |
|
226 |
+ cur) |
|
227 |
+ else collect (cons nil next)) |
|
228 |
+ 'vector))))) |
|
204 | 229 |
|
205 | 230 |
(defun-ct cumsum |
206 | 231 |
(&key (add-fun #'+) (key #'identity) (combine (lambda (x y) y x)) (zero 0)) |
... | ... |
@@ -222,6 +247,11 @@ |
222 | 247 |
(lambda (seq) |
223 | 248 |
(map result-type fun seq))) |
224 | 249 |
|
250 |
+(defun-ct denest (&key (result-type 'list)) |
|
251 |
+ (lambda (seq) |
|
252 |
+ (apply #'concatenate result-type |
|
253 |
+ seq))) |
|
254 |
+ |
|
225 | 255 |
(defmacro applying (fun &rest args) |
226 | 256 |
(alexandria:with-gensyms (seq) |
227 | 257 |
`(lambda (,seq) |