From 91f33fc900e96083aaaf41d1b41d2cd0ada1aa22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Mon, 20 Jul 2020 03:22:50 +0200 Subject: Add split-by. --- module/util.scm | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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) -- cgit v1.2.3