Add a chunk! untility

For splitting a list in to multiple chuncks, satisfying some max length.
This commit is contained in:
Christopher Baines 2021-10-03 12:55:21 +01:00
parent 211da6868f
commit d5ab67000e

View file

@ -16,6 +16,7 @@
;;; <http://www.gnu.org/licenses/>. ;;; <http://www.gnu.org/licenses/>.
(define-module (guix-data-service utils) (define-module (guix-data-service utils)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-11) #:use-module (srfi srfi-11)
#:use-module (ice-9 match) #:use-module (ice-9 match)
#:use-module (ice-9 threads) #:use-module (ice-9 threads)
@ -28,7 +29,9 @@
parallel-via-thread-pool-channel parallel-via-thread-pool-channel
par-map& par-map&
letpar&)) letpar&
chunk!))
(define (call-with-time-logging action thunk) (define (call-with-time-logging action thunk)
(simple-format #t "debug: Starting ~A\n" action) (simple-format #t "debug: Starting ~A\n" action)
@ -151,3 +154,13 @@
'()))))) '())))))
(define par-map& (par-mapper' map cons)) (define par-map& (par-mapper' map cons))
(define (chunk! lst max-length)
(if (> (length lst)
max-length)
(call-with-values (lambda ()
(split-at! lst max-length))
(lambda (first-lst rest)
(cons first-lst
(chunk! rest max-length))))
(list lst)))