mirror of
https://github.com/fiddlerwoaroof/data-lens.git
synced 2025-11-08 10:16:32 +00:00
feat: allow over to accept multiple functions and compose them
This commit is contained in:
27
lens.lisp
27
lens.lisp
@ -291,10 +291,29 @@
|
|||||||
seq
|
seq
|
||||||
:initial-value ()))))
|
:initial-value ()))))
|
||||||
|
|
||||||
(defun over (fun &key (result-type 'list))
|
(defun over (fun &rest funs)
|
||||||
(let ((fun (functionalize fun)))
|
(let* ((fun (functionalize fun))
|
||||||
(lambda (seq)
|
(rt-pos (position :result-type funs))
|
||||||
(map result-type fun seq))))
|
(result-type (cond
|
||||||
|
((null rt-pos) 'list)
|
||||||
|
((>= (1+ rt-pos)
|
||||||
|
(length funs))
|
||||||
|
(error "invalid result-type"))
|
||||||
|
(t
|
||||||
|
(elt funs (1+ rt-pos)))))
|
||||||
|
(funs (if rt-pos
|
||||||
|
(append (mapcar #'functionalize
|
||||||
|
(subseq funs 0 rt-pos))
|
||||||
|
(mapcar #'functionalize
|
||||||
|
(subseq funs (+ rt-pos 2))))
|
||||||
|
(mapcar #'functionalize funs)))
|
||||||
|
(combined-fun (if funs
|
||||||
|
(apply #'alexandria:compose fun funs)
|
||||||
|
fun)))
|
||||||
|
(lambda (seq &rest seqs)
|
||||||
|
(if seqs
|
||||||
|
(apply #'map result-type combined-fun seq seqs)
|
||||||
|
(map result-type combined-fun seq)))))
|
||||||
|
|
||||||
(defun denest (&key (result-type 'list))
|
(defun denest (&key (result-type 'list))
|
||||||
(lambda (seq)
|
(lambda (seq)
|
||||||
|
|||||||
@ -250,6 +250,12 @@
|
|||||||
(5am:is (equalp #(1 2 3)
|
(5am:is (equalp #(1 2 3)
|
||||||
(funcall (data-lens:over '1+ :result-type 'vector)
|
(funcall (data-lens:over '1+ :result-type 'vector)
|
||||||
'(0 1 2))))
|
'(0 1 2))))
|
||||||
|
(5am:is (equalp #(2 3 4)
|
||||||
|
(funcall (data-lens:over '1+ :result-type 'vector '1+)
|
||||||
|
'(0 1 2))))
|
||||||
|
(5am:is (equalp #(2 3 4)
|
||||||
|
(funcall (data-lens:over '1+ '1+ :result-type 'vector)
|
||||||
|
'(0 1 2))))
|
||||||
(5am:is (equalp #(1 2 3)
|
(5am:is (equalp #(1 2 3)
|
||||||
(funcall (data-lens:over '1+ :result-type 'vector)
|
(funcall (data-lens:over '1+ :result-type 'vector)
|
||||||
#(0 1 2)))))
|
#(0 1 2)))))
|
||||||
|
|||||||
Reference in New Issue
Block a user