Skip to content

Instantly share code, notes, and snippets.

@lysu
Last active December 13, 2015 18:58
Show Gist options
  • Save lysu/4959203 to your computer and use it in GitHub Desktop.
Save lysu/4959203 to your computer and use it in GitHub Desktop.
mark from from plumbing.core
(defn unchunk
"Takes a seqable and returns a lazy sequence that
is maximally lazy and doesn't realize elements due to either
chunking or apply.
Useful when you don't want chunking, for instance,
(first awesome-website? (map slurp +a-bunch-of-urls+))
may slurp up to 31 unneed webpages, wherease
(first awesome-website? (map slurp (unchunk +a-bunch-of-urls+)))
is guaranteed to stop slurping after the first awesome website.
Taken from http://stackoverflow.com/questions/3407876/how-do-i-avoid-clojures-chunking-behavior-for-lazy-seqs-that-i-want-to-short-ci"
[s]
(when (seq s)
(cons (first s)
(lazy-seq (unchunk (rest s))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment