From 16e68df7cacf367bffa06ec2ca23ee151b2dfa6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Sat, 16 Jul 2022 23:35:50 +0200 Subject: Add break/all. --- module/hnh/util.scm | 10 ++++++++++ tests/test/util.scm | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/module/hnh/util.scm b/module/hnh/util.scm index 9a45704b..096e38c5 100644 --- a/module/hnh/util.scm +++ b/module/hnh/util.scm @@ -36,6 +36,7 @@ split-by split-by-one-of + break/all span-upto cross-product @@ -367,6 +368,15 @@ (lambda (first rest) (cons (cdr first) rest)))))) +(define (break/all predicate lst) + (let loop ((lst lst)) + (if (null? lst) + '(()) + (let ((fst rest (break predicate lst))) + (if (null? rest) + (list fst) + (cons fst (loop (cdr rest)))))))) + ;; Simar to span from srfi-1, but never takes more than ;; @var{count} items. Can however still take less. ;; @example diff --git a/tests/test/util.scm b/tests/test/util.scm index aa37d20c..5e2aab4e 100644 --- a/tests/test/util.scm +++ b/tests/test/util.scm @@ -206,6 +206,15 @@ '((1 + 2) (* 3 + 4)) (split-by-one-of '(1 + 2 * 3 + 4) '(*)))) + +(test-group "break/all" + (test-equal '((a b c)) (break/all (const #f) '(a b c))) + (test-equal '(()) (break/all (const #t) '())) + (test-equal '(() () () ()) (break/all (const #t) '(a b c))) + (test-equal '((a b) (c d)) (break/all number? '(a b 1 c d))) + (test-equal '(() ()) (break/all number? '(1)))) + + (call-with-values (lambda () (span-upto -- cgit v1.2.3