Skip to content

Instantly share code, notes, and snippets.

@pesterhazy
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pesterhazy/af12a5f85355f5c80358 to your computer and use it in GitHub Desktop.
Save pesterhazy/af12a5f85355f5c80358 to your computer and use it in GitHub Desktop.
Constrain a sequence in clojure
(defn constrain
"Truncate a sequence
Returns a two-element vector to signal whether of not the seq has
been truncated"
[max coll]
(let [[head tail] (split-at max coll)]
(if (seq tail)
[nil (take max coll)]
[coll])))
;; usage
user=> (let [[a b] (constrain 5 (range 5))] (or a (conj (vec b) :...)))
(0 1 2 3 4)
user=> (let [[a b] (constrain 5 (range 6))] (or a (conj (vec b) :...)))
[0 1 2 3 4 :...]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment