From 79aa98de8bed02a14d5bdb76edc87865c4663053 Mon Sep 17 00:00:00 2001 From: Colin Okay Date: Sat, 30 Jul 2022 12:09:28 -0500 Subject: [refactor] group --- hyperquirks.lisp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/hyperquirks.lisp b/hyperquirks.lisp index dd65b4f..b765e69 100644 --- a/hyperquirks.lisp +++ b/hyperquirks.lisp @@ -208,12 +208,15 @@ E.g. "Group a list XS into consequtive sublists of size N, using the DEFAULT to fill in any remainder in the case the length of XS is not neatly divisible by N." - (loop for l on xs by (lambda (l) (nthcdr n l)) - when (<= n (length l)) - collect (subseq l 0 n) - else - collect (append l (loop repeat (- n (length l)) collect default)))) - + (loop + with len = (length xs) + for start from 0 to len by n + for end = (+ start n) + when (<= end len) + collect (subseq xs start end) + when (and (< start len) (> end len)) + collect (nconc (subseq xs start) + (loop repeat (- end len) collect default)))) ;;; STRING FUNCTIONS -- cgit v1.2.3