Created
January 20, 2012 12:25
-
-
Save ponzao/1647159 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
;; 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