git.fiddlerwoaroof.com
Browse code

chore(transducers): move functions around

Edward authored on 05/01/2021 04:19:19
Showing 3 changed files
... ...
@@ -22,12 +22,14 @@
22 22
 
23 23
 (defpackage :data-lens.transducers.internals
24 24
   (:use :cl)
25
-  (:export #:unwrap #:init #:reduce-generic #:stepper))
25
+  (:export #:unwrap #:init #:reduce-generic #:stepper #:transduce
26
+           #:exit-early))
26 27
 
27 28
 (defpackage :data-lens.transducers
28 29
   (:use :cl)
29 30
   (:import-from :data-lens.transducers.internals
30
-                #:unwrap #:init #:reduce-generic #:stepper)
31
+                #:unwrap #:init #:reduce-generic #:stepper #:transduce
32
+                #:exit-early)
31 33
   (:export #:mapping :filtering :deduping :catting :splitting
32 34
            #:exit-early :taking :dropping :transduce
33 35
            #:hash-table-builder :vector-builder :list-builder
... ...
@@ -1,10 +1,5 @@
1 1
 (in-package :data-lens.transducers.internals)
2 2
 
3
-(defgeneric unwrap (it obj)
4
-  (:method (it obj) obj))
5
-(defgeneric init (it))
6
-(defgeneric stepper (it))
7
-
8 3
 (defgeneric reduce-generic (seq func init)
9 4
   (:method ((seq sequence) (func function) init)
10 5
     (reduce func seq :initial-value init))
... ...
@@ -20,3 +15,34 @@
20 15
                  (setf acc (funcall func acc (list k v))))
21 16
                seq)
22 17
       acc)))
18
+
19
+(defgeneric init (client))
20
+(defgeneric stepper (client))
21
+(defgeneric unwrap (client obj)
22
+  (:method (client obj) obj))
23
+
24
+(defun exit-early (acc)
25
+  (throw 'done acc))
26
+
27
+(defun transduce (xf build seq)
28
+  (let* ((xf (etypecase xf
29
+               (list (apply 'alexandria:compose xf))
30
+               ((or function symbol) xf)))
31
+         (transducer (funcall xf (stepper build))))
32
+    (unwrap build
33
+            (funcall transducer
34
+                     (catch 'done
35
+                       (reduce-generic seq
36
+                                       transducer
37
+                                       (init build)))))))
38
+
39
+#+(or)
40
+(defdocumentation transducer-protocol
41
+    (:function transduce (xf build seq)
42
+               )
43
+  (:generic-function unwrap (client obj)
44
+                     )
45
+  (:generic-function unwrap (client obj)
46
+                     )
47
+  (:generic-function unwrap (client obj)
48
+                     ))
... ...
@@ -116,9 +116,6 @@
116 116
   (let ((splitter (apply #'data-lens:juxt functions)))
117 117
     (mapping splitter)))
118 118
 
119
-(defun exit-early (acc)
120
-  (throw 'done acc))
121
-
122 119
 (defun taking (n)
123 120
   (lambda (rf)
124 121
     (let ((taken 0))
... ...
@@ -141,17 +138,6 @@
141 138
              (funcall rf acc next)))
142 139
         ((it) (funcall rf it))))))
143 140
 
144
-(defun transduce (xf build seq)
145
-  (let* ((xf (etypecase xf
146
-               (list (apply 'alexandria:compose xf))
147
-               ((or function symbol) xf)))
148
-         (transducer (funcall xf (stepper build))))
149
-    (unwrap build
150
-            (funcall transducer
151
-                     (catch 'done
152
-                       (reduce-generic seq
153
-                                       transducer
154
-                                       (init build)))))))
155 141
 (defun eduction (xf seq)
156 142
   (lambda (build)
157 143
     (unwrap