Last active
August 29, 2015 14:15
-
-
Save pesterhazy/af12a5f85355f5c80358 to your computer and use it in GitHub Desktop.
Constrain a sequence in clojure
This file contains 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 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