Created
December 9, 2016 08:55
-
-
Save ray1729/d834ef167c515cbc8fc6e2361cb692f9 to your computer and use it in GitHub Desktop.
Split on pred implemented by reducing with a state machine
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
(defn split-after | |
[pred coll] | |
(let [post-accum (fn [accum x] | |
(update accum :after conj x)) | |
pre-accum (fn [accum x] | |
(cond-> (update accum :before conj x) | |
(pred x) (assoc :f post-accum)))] | |
((juxt :before :after) | |
(reduce (fn [{:keys [f] :as accum} x] (f accum x)) | |
{:before [] :after [] :f pre-accum} | |
coll)))) | |
(defn split-before | |
[pred coll] | |
(let [post-accum (fn [accum x] | |
(update accum :after conj x)) | |
pre-accum (fn [accum x] | |
(if (pred x) | |
(-> (update accum :after conj x) | |
(assoc :f post-accum)) | |
(update accum :before conj x)))] | |
((juxt :before :after) | |
(reduce (fn [{:keys [f] :as accum} x] (f accum x)) | |
{:before [] :after [] :f pre-accum} | |
coll)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment