about summary refs log tree commit diff
path: root/users/wpcarro/emacs/pkgs/list/list.el
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2022-08-01T20·43-0700
committerclbot <clbot@tvl.fyi>2022-08-02T20·29+0000
commit5417b512e77a7434f7b1fc9d55035921725a8df1 (patch)
tree81b28a81def8eba77449717287c7026c4c952cdb /users/wpcarro/emacs/pkgs/list/list.el
parent852cc4a9c8dac3d2a6afd139fee69a81f838db41 (diff)
feat(wpcarro/emacs): Support list-chunk function r/4365
Would be nice to remove the top-level `if` statement, but I can't be bothered to
debug the first unit test without that workaround.

Change-Id: I4ba576bda915eeb11f9fbfeb5d95d8e5668fd0bd
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6022
Reviewed-by: wpcarro <wpcarro@gmail.com>
Autosubmit: wpcarro <wpcarro@gmail.com>
Tested-by: BuildkiteCI
Diffstat (limited to 'users/wpcarro/emacs/pkgs/list/list.el')
-rw-r--r--users/wpcarro/emacs/pkgs/list/list.el18
1 files changed, 18 insertions, 0 deletions
diff --git a/users/wpcarro/emacs/pkgs/list/list.el b/users/wpcarro/emacs/pkgs/list/list.el
index 03382991e035..a1822e77ff60 100644
--- a/users/wpcarro/emacs/pkgs/list/list.el
+++ b/users/wpcarro/emacs/pkgs/list/list.el
@@ -160,6 +160,24 @@
                     (list-cons x acc)))
                 xs)))
 
+(defun list-chunk (n xs)
+  "Chunk XS into lists of size N."
+  (if (> n (length xs))
+      (list xs)
+    (->> xs
+         (list-reduce '(:curr () :result ())
+                      (lambda (x acc)
+                        (let ((curr (plist-get acc :curr))
+                              (result (plist-get acc :result)))
+                          (if (= (- n 1) (length curr))
+                              `(:curr () :result ,(list-cons (list-reverse (list-cons x curr)) result))
+                            `(:curr ,(list-cons x curr) :result ,result)))))
+         (funcall (lambda (xs)
+                    (let ((curr (plist-get xs :curr))
+                          (result (plist-get xs :result)))
+                      (if curr (list-cons curr result)) result)))
+         list-reverse)))
+
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Predicates
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;