Skip to content

Instantly share code, notes, and snippets.

@favila
Created March 25, 2014 15:49
Show Gist options
  • Save favila/9764736 to your computer and use it in GitHub Desktop.
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))
(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