diff --git a/utils-tests.rkt b/utils-tests.rkt index 7fd5ddb..d4c83df 100644 --- a/utils-tests.rkt +++ b/utils-tests.rkt @@ -126,7 +126,9 @@ (check-equal? (ht-values/list->set #hash((a . (1 1)))) (hash 'a (set 1))) (check-equal? (hash->list/ordered #hash((b . 1) (a . 1))) - '((a . 1) (b . 1)))) + '((a . 1) (b . 1))) + (let-values ([(l1 l2) (multi-split-at '((1 2 3) (a b c)) 2)]) + (check-equal? l1 '((1 2) (a b))) (check-equal? l2 '((3) (c))))) (test-case "Functions" (check-true (procedure-fixed-arity? not)) diff --git a/utils.rkt b/utils.rkt index f4903db..416ba20 100644 --- a/utils.rkt +++ b/utils.rkt @@ -34,6 +34,8 @@ [ht-values/list->set (-> (hash/c any/c (listof any/c)) (hash/c any/c (set/c any/c)))] [hash->list/ordered (-> hash? (listof (cons/c any/c any/c)))] + [multi-split-at (-> (listof (listof any/c)) number? + (values (listof (listof any/c)) (listof (listof any/c))))] [procedure-fixed-arity? (-> procedure? boolean?)] [in-random (case-> (-> (stream/c (and/c real? inexact? (>/c 0) ( (integer-in 1 4294967087) (stream/c exact-nonnegative-integer?)) @@ -333,6 +335,16 @@ ;;; which the hash table orders them for hash-map and hash-for-each. (define (hash->list/ordered ht) (hash-map ht cons #t)) +;;; Given a list of lists, splits every single list at the given +;;; position, and then returns two lists: one consisting of the first +;;; halves, and the one consisting of the second halves. +(define (multi-split-at lsts pos) + (define (split-1 lst res) + (define-values (l r) (split-at lst pos)) + (match res [(cons left right) + (cons (cons l left) (cons r right))])) + (match (foldr split-1 (cons '() '()) lsts) + [(cons lefts rights) (values lefts rights)])) ;;; ========= ;;; Functions