0
votes

Is there a function that exists in clojure similar to reduce, but that can pass in a variable?

For example, say I wanted to implement my own filter function and pass in a predicate to check the entire collection against and retain the values that satisfy the predicate, is there an idiomatic way of doing this while using reduce?

My guess would be to do the following:

(->> coll 
    (map #(:val % :pred pred))
    (reduce my-filter-fn {:val 0 :pred pred}))

Essentially just creating a collection of maps that have the value I need to pass into the reduce function.

I've found the use case of needing to pass in values to the reduce function somewhat frequently, but it may because I'm not thinking in the appropriate functional way. Is there a more idiomatic solution?

1
I don't really understand what you are trying to do. Can you add an example of before/after with data?Alan Thompson

1 Answers

1
votes

Let my-filter-fn be a three arity fun, and use partial

   (reduce (partial my-filter-fn pred) 0)

But it also sounds like you could use filter instead of reduce

   (->> coll
           (filter pred)
           (reduce my-fn)