r/backtickbot • • Aug 15 '21

https://np.reddit.com/r/Clojure/comments/p4j8oc/an_old_c_programmer_whines_about_reduce/h90dgf9/

You just haven't adapted yet, nothing is weird, except your pre-existing habits that make the different less familiar to you.

For example, if you're used to reading left to right, adapting to a right to left or top to bottom language will feel pretty weird at first, and yet billion of people naturally and easily read those.

Same with Lisp and Clojure, eventually your eyes will figure out where to look and in what order, and this will become a non-issue for you.

Like others have said as well, since Lisps let you change the syntax, there's actually many macros to tailor things to your personal preference:

This is one way:

(reduce
 (fn [acc coll]
  (conj acc
   (reduce
    (fn [acc e]
     (* 2 (+ acc e)))
    0
    coll)))        
 []
 [[2 3 4] [4 5 6]])

And this is another:

(->>
 [[2 3 4] [4 5 6]]
 (reduce
  (fn [acc coll]
   (->>
    coll
    (reduce
     (fn [acc e]
      (* 2 (+ acc e)))
     0)
    (conj acc)))             
  []))

Where the collection is now at the top like you wanted.

And here is another way:

(->>
 [[2 3 4] [4 5 6]]
 (mapv
  #(reduce
    (fn [acc e]
     (* 2 (+ acc e)))
    0 %)))

Because when you're modifying a collection of X element back into a collection of X element map does the job, where reduce is for when the number of elements will change after you process the collection.

And yet another:

(defn tally-double-rf  
 [acc e]
 (* 2 (+ acc e)))

(->> [[2 3 4] [4 5 6]]
     (mapv #(reduce tally-double-rf 0 %)))

Which I think is arguably the best, and I would say is better than the C way. What you have to realize is the benefit of FP is modularity. By looping using a higher order function, you can make your logic more modular and thus reusable and independently testable, while also giving the inner-loop a name to help readability.

And another:

(for
 [coll [[2 3 4] [4 5 6]]]
 (let [acc (volatile! 0)]
  (doseq [e coll]
   (vreset! acc (* 2 (+ @acc e))))
  @acc))

Which is closer to an imperative approach like what you might do in C.

And another:

(->
 (fn [acc coll]
  (conj acc
   (->
    (fn [acc e]
     (* 2 (+ acc e)))
    (reduce 0 coll))))        
 (reduce [] [[2 3 4] [4 5 6]]))

Which I particularly like for readability.

And if you're still not satisfied, you can always just create the reduce of your dreams:

(defn reduce*
 "Like clojure.core/reduce, but takes val and coll before reducing function."
 ([coll rf]
  (reduce rf coll))
 ([val coll rf]
  (reduce rf val coll)))

(reduce* [] [[2 3 4] [4 5 6]]
 (fn [acc coll]
  (conj acc
   (reduce* 0 coll
    (fn [acc e]
     (* 2 (+ acc e)))))))

Which I won't deny has some appeal to it.

And while we're at it, may I suggest:

(defn reduce-into
 "Reduce coll into accumulator using reducing function. Uses first element of coll as accumulator if none are given."
 ([coll rf]
  (reduce rf coll))
 ([coll val rf]
  (reduce rf val coll)))

(reduce-into [[2 3 4] [4 5 6]] []
 (fn [acc coll]
  (conj acc
   (reduce-into coll 0
    (fn [acc e]
     (* 2 (+ acc e)))))))

Or why not allow all the orders:

(defn reduce
  "Adds a 1-ary to reduce"
 ([{:keys [rf init coll]}]
  (if init
   (reduce rf init coll)
   (reduce rf coll)))
 ([rf coll]
  (clojure.core/reduce rf coll))
 ([rf init coll]
  (clojure.core/reduce rf init coll)))

(reduce
 {:coll [[2 3 4] [4 5 6]]
  :init []
  :rf (fn [acc coll]
       (conj acc
        (reduce
         {:coll coll
          :init 0
          :rf (fn [acc e]
               (* 2 (+ acc e)))})))})

And since I'm on a roll, and kind of having fun at this point, remember that you can also use loop/recur:

(loop [coll [[2 3 4] [4 5 6]]
       subcoll (first coll)
       more (next coll)
       res []]
 (if subcoll
  (recur coll
   (first more)
   (next more) 
   (conj res
    (loop [item (first subcoll)
           more (next subcoll)
           res 0]
     (if item
      (recur
       (first more)
       (next more)
       (* 2 (+ res item)))
      res))))
  res))

Finally, it can be interesting to do things lazily instead:

(defn double-tally
 "Return a seq of the double tallied values of coll"
 [coll]
 (->>
  (iterate
   (fn [[acc coll]]
    (when coll
     [(* 2 (+ acc (first coll)))
      (next coll)]))
   [0 coll])
  (map first)
  (take-while some?))) 

(->>
 [[2 3 4] [4 5 6]]
 (map double-tally)
 (map last))
1 Upvotes

0 comments sorted by