feat: two-arg INTO and continue restart for xfs

This commit is contained in:
Edward Langley
2025-06-28 14:58:32 -07:00
parent d52023f9f4
commit ffe67ab084
3 changed files with 54 additions and 9 deletions

View File

@ -237,6 +237,22 @@
'< '<
:key 'car)))) :key 'car))))
(5am:is (equalp #(1 2 3 4)
(data-lens.transducers:into #() '(1 2 3 4)))
"~s can be used to convert one type into another without a transducer"
'data-lens.transducers:into)
(5am:is (equalp #(1 2 4)
(handler-bind ((simple-error #'continue))
(data-lens.transducers:into #()
(data-lens.transducers:mapping
(lambda (it)
(if (= it 3)
(error "fail")
it)))
'(1 2 3 4))))
"transducers provide a continue restart")
(loop for type in '(vector list) (loop for type in '(vector list)
do (5am:is (equalp #(1 2 3 4 5 6) do (5am:is (equalp #(1 2 3 4 5 6)
(data-lens.transducers:into #(1 2 3) (data-lens.transducers:into #(1 2 3)

View File

@ -16,12 +16,6 @@
seq) seq)
acc))) acc)))
#+(or)
(defun document (&rest strings)
(serapeum:string-join strings #.(format nil "~2%")))
(defgeneric init (client))
(defgeneric stepper (client))
(defmacro transducer-lambda (&body (((two-arg-acc two-arg-next) &body two-arg-body) (defmacro transducer-lambda (&body (((two-arg-acc two-arg-next) &body two-arg-body)
&optional (((one-arg-arg) &body one-arg-body) &optional (((one-arg-arg) &body one-arg-body)
'((it) it)))) '((it) it))))
@ -33,8 +27,29 @@
,@two-arg-body) ,@two-arg-body)
(let ((,one-arg-arg ,arg1)) (let ((,one-arg-arg ,arg1))
,@one-arg-body))))) ,@one-arg-body)))))
(defgeneric init (client)
(:method ((client symbol))
(unless (fboundp client)
(error "client not funcallable"))
(init (fdefinition client)))
(:method ((client function))
(funcall client)))
(defgeneric stepper (client)
(:method ((client function))
(transducer-lambda
((acc a)
(declare (optimize (speed 3)))
(funcall client acc a))))
(:method ((client symbol))
(unless (fboundp client)
(error "client not funcallable"))
(init (fdefinition client))))
(defgeneric unwrap (client obj) (defgeneric unwrap (client obj)
(:method (client obj) obj)) (:method (client obj) obj))
(defgeneric builder-for-input (seq) (defgeneric builder-for-input (seq)
(:documentation (:documentation
"Take a transducible sequence, return a builder and an init value for that builder. "Take a transducible sequence, return a builder and an init value for that builder.
@ -56,7 +71,16 @@ CONSTRAINT: SEQ should be copied, not modified"))
transducer transducer
(init build))))))) (init build)))))))
(defun into (to xf from) (defun into (to xf &optional (from nil from-p))
(if (not from-p)
(let ((from xf))
(data-lens.transducers:into to
(data-lens.transducers:mapping
(lambda (&rest args)
(if (null (cdr args))
(car args)
args)))
from))
(multiple-value-bind (builder init) (builder-for-input to) (multiple-value-bind (builder init) (builder-for-input to)
(let* ((xf (etypecase xf (let* ((xf (etypecase xf
(list (apply 'alexandria:compose xf)) (list (apply 'alexandria:compose xf))
@ -67,7 +91,7 @@ CONSTRAINT: SEQ should be copied, not modified"))
(catch 'done (catch 'done
(reduce-generic from (reduce-generic from
transducer transducer
init))))))) init))))))))
(defmacro defdocumentation (name &body doc-specs) (defmacro defdocumentation (name &body doc-specs)
name doc-specs name doc-specs

View File

@ -23,7 +23,12 @@
(lambda (rf) (lambda (rf)
(transducer-lambda (transducer-lambda
((acc next) ((acc next)
(funcall rf acc (call-function next))) (restart-case
(funcall rf acc (call-function next))
(continue ()
:report (lambda (s)
(format s "skip this item"))
acc)))
((it) (funcall rf it)))))) ((it) (funcall rf it))))))
(defun mv-mapping (function &rest args) (defun mv-mapping (function &rest args)