utils: Add lists-transpose.

This commit is contained in:
Sergiu Ivanov 2020-03-23 22:47:52 +01:00
parent df042493d9
commit f1514fffe3
2 changed files with 16 additions and 1 deletions

View File

@ -128,7 +128,8 @@
(check-equal? (hash->list/ordered #hash((b . 1) (a . 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)))))
(check-equal? l1 '((1 2) (a b))) (check-equal? l2 '((3) (c))))
(check-equal? (lists-transpose '((1 2) (a b))) '((1 a) (2 b))))
(test-case "Functions"
(check-true (procedure-fixed-arity? not))

View File

@ -36,6 +36,7 @@
[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))))]
[lists-transpose (-> (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?))
@ -346,6 +347,19 @@
(match (foldr split-1 (cons '() '()) lsts)
[(cons lefts rights) (values lefts rights)]))
;;; Given a list of lists of the same length, transposes them.
;;;
;;; > (lists-transpose '((1 2) (a b)))
;;; '((1 a) (2 b))
;;;
;;; This function is essentially in-parallel, wrapped in a couple
;;; conversions.
(define lists-transpose
(compose sequence->list
in-values-sequence
((curry apply) in-parallel)))
;;; =========
;;; Functions
;;; =========