Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try reducers #1

Open
miner opened this issue Dec 13, 2017 · 1 comment
Open

Try reducers #1

miner opened this issue Dec 13, 2017 · 1 comment

Comments

@miner
Copy link

miner commented Dec 13, 2017

I found that reducers can be pretty fast with large collections. You could try something like this. I pulled the predicate out as an argument. It would probably make sense to try some slow predicates as well to see how much time is overhead.

(require '[clojure.core.reducers :as r])

(defn tally-by [pred xs]
  (let [total (count xs)
        cnt (r/fold + (r/map (fn [_] 1) (r/filter pred xs)))]
    [cnt (- total cnt)]))

 (quick-bench (tally-by odd? xs))
@rauhs
Copy link
Owner

rauhs commented Dec 13, 2017

I have just tried it out. Seems to be a little bit quicker (like 25%).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants