aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2020-07-20 03:22:50 +0200
committerHugo Hörnquist <hugo@lysator.liu.se>2020-07-20 04:19:03 +0200
commit91f33fc900e96083aaaf41d1b41d2cd0ada1aa22 (patch)
tree18f7d0c5a3cff65caad8f002996ab29fd18c168a
parentRemove invalid export tree-map from (util). (diff)
downloadcalp-91f33fc900e96083aaaf41d1b41d2cd0ada1aa22.tar.gz
calp-91f33fc900e96083aaaf41d1b41d2cd0ada1aa22.tar.xz
Add split-by.
-rw-r--r--module/util.scm18
1 files changed, 18 insertions, 0 deletions
diff --git a/module/util.scm b/module/util.scm
index f09cc315..76877d10 100644
--- a/module/util.scm
+++ b/module/util.scm
@@ -406,6 +406,24 @@
(hash-set! h key (cons value (hash-ref h key '())))))
(hash-map->list list h)))
+;; (group-by '(0 1 2 3 4 2 5 6) 2)
+;; ⇒ ((0 1) (3 4) (5 6))
+(define-public (split-by list item)
+ (let loop ((done '())
+ (current '())
+ (rem list))
+ (cond [(null? rem)
+ (reverse (cons (reverse current) done))]
+ [(eqv? item (car rem))
+ (loop (cons (reverse current) done)
+ '()
+ (cdr rem))]
+ [else
+ (loop done
+ (cons (car rem) current)
+ (cdr rem))])))
+
+
;; Returns the cross product between l1 and l2.
;; each element is a cons cell.
(define (cross-product% l1 l2)