git.fiddlerwoaroof.com
Browse code

chore: rearrange optics

Ed Langley authored on 31/10/2020 08:05:20
Showing 1 changed files
... ...
@@ -1,5 +1,42 @@
1 1
 (in-package :data-lens.lenses)
2 2
 
3
+#+fw.dev
4
+(progn
5
+  ;; maybe functor implementation
6
+  (defclass maybe ()
7
+    ())
8
+  (defclass just (maybe)
9
+    ((%v :initarg :value :reader value)))
10
+  (defclass nothing (maybe)
11
+    ())
12
+
13
+  (defun just (value)
14
+    (make-instance 'just :value value))
15
+  (defun nothing (&optional value)
16
+    (declare (ignore value))
17
+    (make-instance 'nothing))
18
+
19
+  (defgeneric maybe (default value)
20
+    (:method (default (value just))
21
+      (value value))
22
+    (:method (default (value nothing))
23
+      default))
24
+
25
+  (defgeneric maybe-apply (function value)
26
+    (:method (function (value just))
27
+      (just (funcall function (value value))))
28
+    (:method (function (value nothing))
29
+      value))
30
+
31
+  (defmethod print-object ((o just) s)
32
+    (format s "#.(~s ~s)"
33
+            'just
34
+            (value o)))
35
+
36
+  (defmethod print-object ((o nothing) s)
37
+    (format s "#.(~s)"
38
+            'nothing)))
39
+
3 40
 ;; identity functor, necessary for set and over
4 41
 (defclass identity- ()
5 42
   ((%v :initarg :value :reader unidentity)))
... ...
@@ -173,47 +210,10 @@ contain the new value at the location focused by the lens."
173 210
   (defun a-lens (cb)
174 211
     (lambda (foo)
175 212
       (fw.lu:prog1-bind (new (clone foo))
176
-        (setf (a new)
177
-              (funcall cb (a foo))))))
213
+                        (setf (a new)
214
+                              (funcall cb (a foo))))))
178 215
   (view 'a-lens
179 216
         (over 'a-lens '1+
180 217
               (set 'a-lens 2
181 218
                    (make-instance 'foo :a 1)))) #|
182 219
   ==> 3 |#)
183
-
184
-#+fw.dev
185
-(progn
186
-  ;; maybe functor implementation
187
-  (defclass maybe ()
188
-    ())
189
-  (defclass just (maybe)
190
-    ((%v :initarg :value :reader value)))
191
-  (defclass nothing (maybe)
192
-    ())
193
-
194
-  (defun just (value)
195
-    (make-instance 'just :value value))
196
-  (defun nothing (&optional value)
197
-    (declare (ignore value))
198
-    (make-instance 'nothing))
199
-
200
-  (defgeneric maybe (default value)
201
-    (:method (default (value just))
202
-      (value value))
203
-    (:method (default (value nothing))
204
-      default))
205
-
206
-  (defgeneric maybe-apply (function value)
207
-    (:method (function (value just))
208
-      (just (funcall function (value value))))
209
-    (:method (function (value nothing))
210
-      value))
211
-
212
-  (defmethod print-object ((o just) s)
213
-    (format s "#.(~s ~s)"
214
-            'just
215
-            (value o)))
216
-
217
-  (defmethod print-object ((o nothing) s)
218
-    (format s "#.(~s)"
219
-            'nothing)))