mirror of
https://github.com/fiddlerwoaroof/data-lens.git
synced 2025-11-08 18:26:32 +00:00
chore(transducers): extract a common pattern into a macro
This commit is contained in:
@ -3,22 +3,36 @@
|
|||||||
exit-early taking dropping transduce
|
exit-early taking dropping transduce
|
||||||
hash-table-builder vector-builder list-builder))
|
hash-table-builder vector-builder list-builder))
|
||||||
|
|
||||||
(defun mapping (function)
|
(defmacro define-functional-transducer (name () &body body)
|
||||||
|
`(defun ,name (function &rest args)
|
||||||
|
(flet ((call-function (it) (apply function it args)))
|
||||||
(lambda (rf)
|
(lambda (rf)
|
||||||
(lambda (acc next)
|
(lambda (acc next)
|
||||||
(funcall rf acc (funcall function next)))))
|
,@body)))))
|
||||||
|
|
||||||
(defun mv-mapping (function)
|
(define-functional-transducer mapping ()
|
||||||
(lambda (rf)
|
(funcall rf acc (call-function next)))
|
||||||
(lambda (acc next)
|
|
||||||
(funcall rf acc (multiple-value-list (funcall function next))))))
|
|
||||||
|
|
||||||
(defun filtering (predicate)
|
(define-functional-transducer mv-mapping ()
|
||||||
(lambda (rf)
|
(funcall rf acc (multiple-value-list (call-function next))))
|
||||||
(lambda (acc next)
|
|
||||||
(if (funcall predicate next)
|
(define-functional-transducer mv-selecting ()
|
||||||
|
(multiple-value-bind (value use-p) (call-function next)
|
||||||
|
(if use-p
|
||||||
|
(funcall rf acc value)
|
||||||
|
acc)))
|
||||||
|
|
||||||
|
(defun hash-table-select (hash-table)
|
||||||
|
(mv-selecting #'gethash hash-table))
|
||||||
|
|
||||||
|
(define-functional-transducer filtering ()
|
||||||
|
(if (call-function next)
|
||||||
(funcall rf acc next)
|
(funcall rf acc next)
|
||||||
acc))))
|
acc))
|
||||||
|
|
||||||
|
(defun mv-filtering (function &rest args)
|
||||||
|
(filtering (lambda (it)
|
||||||
|
(nth-value 1 (apply function it args)))))
|
||||||
|
|
||||||
(defun deduping (&optional (test 'eql))
|
(defun deduping (&optional (test 'eql))
|
||||||
(lambda (rf)
|
(lambda (rf)
|
||||||
|
|||||||
Reference in New Issue
Block a user