Created
March 25, 2014 15:49
-
-
Save favila/9764736 to your computer and use it in GitHub Desktop.
Lazy Clojure(Script) function to return row-oriented slices of sequential data suitable for display in a table. E.g. [0 1 2 3 4] -> ((0 3)(1 4)(2 nil))
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn zip-partition | |
"Return a sequence of length n of sequences of every nth item, each offset | |
by one. Suitable for building the rows of a table with n columns where the | |
sequence items flow down columns then rows. Example: | |
(zip-partition [0 1 2 3 4] 2) -> ((0 4) | |
(1 nil) | |
(2 nil) | |
(3 nil))" | |
[s n] | |
(->> (repeat s) | |
(map drop (range n)) | |
(apply #(partition n n (repeat nil) %)) | |
(#(if (next %) | |
(->> % (apply interleave) (partition n)) | |
%)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment