From d5ab67000e613c2877e6f008cdb2b322701f5285 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Sun, 3 Oct 2021 12:55:21 +0100 Subject: [PATCH] Add a chunk! untility For splitting a list in to multiple chuncks, satisfying some max length. --- guix-data-service/utils.scm | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/guix-data-service/utils.scm b/guix-data-service/utils.scm index 7741671..c45f518 100644 --- a/guix-data-service/utils.scm +++ b/guix-data-service/utils.scm @@ -16,6 +16,7 @@ ;;; . (define-module (guix-data-service utils) + #:use-module (srfi srfi-1) #:use-module (srfi srfi-11) #:use-module (ice-9 match) #:use-module (ice-9 threads) @@ -28,7 +29,9 @@ parallel-via-thread-pool-channel par-map& - letpar&)) + letpar& + + chunk!)) (define (call-with-time-logging action thunk) (simple-format #t "debug: Starting ~A\n" action) @@ -151,3 +154,13 @@ '()))))) (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)))