utils: Add multi-split-at.

This commit is contained in:
Sergiu Ivanov 2020-03-23 22:32:39 +01:00
parent 9961dc7c8b
commit df042493d9
2 changed files with 15 additions and 1 deletions

View File

@ -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))

View File

@ -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) (</c 1))))
(-> (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