Skip to content

Instantly share code, notes, and snippets.

@edtsech
Created February 5, 2014 13:34
Show Gist options
  • Save edtsech/8823692 to your computer and use it in GitHub Desktop.
Save edtsech/8823692 to your computer and use it in GitHub Desktop.
(defn split-by
"Create from sequence (l) sequence of sequences with specified number of elemenets (c)
Example:
(split-by 2 [1 2 3 4 5 6 7])
=> '((1 2) (3 4) (5 6) (7))"
[c l]
(if (seq l)
(cons (take c l) (split-by c (drop c l)))))
(defn split-into-chunks
"Split sequence (l) into specified number of chunks (c)
Example:
(split-into-chunks 3 [1 2 3 4 5 6 7])
=> '((1 2 3) (4 5 6) (7))"
[c l]
(split-by (inc (int (/ (count l) c))) l))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment