1
0
Fork 0

- fixed misc:array-slice;

- changed misc:split-into-sublist to misc:split-into-chunks.
This commit is contained in:
cage 2022-05-27 19:16:29 +02:00
parent 950b669455
commit bf23d728b4
2 changed files with 22 additions and 9 deletions

View File

@ -615,7 +615,7 @@ to the array"
(defun array-slice (array start &optional (end nil)) (defun array-slice (array start &optional (end nil))
(let* ((new-size (if end (let* ((new-size (if end
(- end start) (- end start)
(length array))) (- (length array) start)))
(new-fill-pointer (cond (new-fill-pointer (cond
((array-has-fill-pointer-p array) ((array-has-fill-pointer-p array)
(if end (if end
@ -670,13 +670,26 @@ to the array"
(swap (elt sequence rnd) (elt sequence i)))) (swap (elt sequence rnd) (elt sequence i))))
sequence) sequence)
(defun split-into-sublist (lst len) (defun %split-into-chunks (sequence subseq-fn chunk-length &optional (accum ()))
(if (or (= len 0) (assert (> chunk-length 0))
(< (length lst) len)) (cond
(if (null lst) ((null sequence)
lst (reverse accum))
(list lst)) ((< (length sequence) chunk-length)
(append (list (subseq lst 0 len)) (split-into-sublist (subseq lst len) len)))) (%split-into-chunks nil subseq-fn chunk-length (push sequence accum)))
(t
(%split-into-chunks (funcall subseq-fn sequence chunk-length)
subseq-fn
chunk-length
(push (funcall subseq-fn sequence 0 chunk-length) accum)))))
(defgeneric split-into-chunks (object chunk-length))
(defmethod split-into-chunks ((object list) chunk-length)
(%split-into-chunks object #'subseq chunk-length))
(defmethod split-into-chunks ((object vector) chunk-length)
(%split-into-chunks object #'array-slice chunk-length))
(defun group-by (sequence &key (test #'=)) (defun group-by (sequence &key (test #'=))
(let ((distinct '())) (let ((distinct '()))

View File

@ -133,7 +133,7 @@
:dump-hash-table :dump-hash-table
:with-messages-start-end :with-messages-start-end
:safe-random :safe-random
:split-into-sublist :split-into-chunks
:group-by :group-by
:delete@ :delete@
:return-whole :return-whole