git.fiddlerwoaroof.com
Browse code

Fix example a bit

Ed Langley authored on 18/08/2018 20:06:28
Showing 2 changed files
... ...
@@ -6,16 +6,17 @@ the composition of more primitive operations.
6 6
 #+BEGIN_SRC lisp
7 7
   DATA-LENS> (funcall (on (compress-runs :collector 'combine-matching-lists)
8 8
                           (alexandria:compose 
9
-                           (over (juxt (element 0)
10
-                                       'identity))
11
-                           (sorted 'string<)))
12
-                      '("January"   "February" "March"    "April"
13
-                        "May"       "June"     "July"     "August"
14
-                        "September" "October"  "November" "December"))
15
-  #| ==> ((#\A "April" "August") 
9
+                           (over (data-lens:juxt
10
+                                  (data-lens:element 0)
11
+                                  'identity))
12
+                           (sorted 'char< :key (element 0))))
13
+                      '("January" "February" "March" "April"
14
+                        "May" "June" "July" "August"
15
+                        "September" "October" "November" "December"))
16
+  #| ==> ((#\A "April" "August")
16 17
           (#\D "December")
17 18
           (#\F "February")
18
-          (#\J "January" "July" "June")
19
+          (#\J "January" "June" "July")
19 20
           (#\M "March" "May")
20 21
           (#\N "November")
21 22
           (#\O "October")
... ...
@@ -52,15 +52,18 @@
52 52
         acc
53 53
         (cons next acc))))
54 54
 
55
+(defun matching-list-reducer (test acc next)
56
+  (if (and acc
57
+           (funcall test (caar acc) (car next)))
58
+      (cons (cons (caar acc)
59
+                  (append (cdar acc)
60
+                          (cdr next)))
61
+            (cdr acc))
62
+      (cons next acc)))
63
+
55 64
 (defun combine-matching-lists (&key (test 'eql) &allow-other-keys)
56 65
   (lambda (acc next)
57
-    (if (and acc
58
-             (funcall test (caar acc) (car next)))
59
-        (cons (cons (caar acc)
60
-                    (append (cdar acc)
61
-                            (cdr next)))
62
-              (cdr acc))
63
-        (cons next acc))))
66
+    (matching-list-reducer test acc next)))
64 67
 
65 68
 (defun-ct compress-runs (&key (collector 'cons-new) (test 'eql) (key 'identity))
66 69
   (lambda (it)
... ...
@@ -72,7 +75,7 @@
72 75
 (defun-ct sorted (comparator &rest r &key key)
73 76
   (declare (ignore key))
74 77
   (lambda (it)
75
-    (apply #'sort (copy-seq it) comparator r)))
78
+    (apply #'stable-sort (copy-seq it) comparator r)))
76 79
 
77 80
 (defun-ct element (num)
78 81
   (lambda (it)