Skip to content

Instantly share code, notes, and snippets.

@ponzao
Created January 20, 2012 12:25
Show Gist options
  • Save ponzao/1647159 to your computer and use it in GitHub Desktop.
Save ponzao/1647159 to your computer and use it in GitHub Desktop.
;; Gathers values up to (and including) the one for
;; which the predicate fails.
(defn take-to
[pred coll]
(when-let [s (seq coll)]
(let [[x & xs] s]
(if-not (pred x)
(list x)
(lazy-cat (cons x (take-to pred xs)))))))
;; Partitions the sequence based on a predicate. Includes
;; the first value where the predicate fails in the sequence.
;; Heavily influenced by partition-by from clojure.core.
(defn partition-after
[pred coll]
(lazy-seq
(when-let [s (seq coll)]
(let [fst (first s)
fv (pred fst)
run (cons fst (take-to #(= fv (pred %)) (rest s)))]
(cons run (partition-after pred (drop (count run) s)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment