Lars Yencken avatar Lars Yencken committed 33a3bdb

Avoid needless reduce calls in Clojure.

Comments (0)

Files changed (8)

clojure/01-sum-numbers.clj

   (:use clojure.test))
 
 (defn sum [xs]
-  (reduce + 0 xs))
+  (apply + xs))
 
 (defn m35 [x]
   (or (= 0 (mod x 3)) (= 0 (mod x 5))))

clojure/02-sum-fibonnaci.clj

   (cons 1 (cons 2 (fibonacci-acc 1 2 t))))
 
 (defn fibonacci-sum [n]
-  (reduce + 0 (filter (fn [x] (= 0 (mod x 2)))
+  (apply + (filter (fn [x] (= 0 (mod x 2)))
                              (fibonacci n))))
 
 (deftest test-fibonacci-sum-1

clojure/03-prime-factors.clj

          (is (= [2 2 2 2 2 3] (get-factors 96))))
 
 (deftest get-factors-solution
-         (is (= 6857 (reduce max (get-factors 600851475143)))))
+         (is (= 6857 (apply max (get-factors 600851475143)))))
 
 (run-tests)
 
 (println)
-(println "Answer:" (reduce max (get-factors 600851475143)))
+(println "Answer:" (apply max (get-factors 600851475143)))

clojure/04-palindrome.clj

 (println)
 (println
   "Answer:"
-  (reduce max
+  (apply max
     (filter #(is-palindrome? %)
             (map #(* (first %) (second %))
                  (combinations (range 100 1000) 2)))))

clojure/05-divisible.clj

       (map #(max (get xs % 0) (get ys % 0)) keys))))
 
 (defn pow [n i]
-  (reduce * (repeat i n)))
+  (apply * (repeat i n)))
 
 (deftest test-pow-1
          (is (= 8 (pow 2 3))))
 
 (defn prod-dist [d]
   "Calculate the product of the factor distribution."
-  (reduce * (map #(pow (% 0) (% 1)) (seq d))))
+  (apply * (map #(pow (% 0) (% 1)) (seq d))))
 
 (deftest test-prod-dist
          (is (= 96 (prod-dist {2 5, 3 1}))))

clojure/06-sum-squares.clj

   (:use clojure.test))
 
 (defn sum-sq [xs]
-  (reduce + (map #(* % %) xs)))
+  (apply + (map #(* % %) xs)))
 
 (deftest sum-sq-1
   (is (= (sum-sq (range 1 4)) 14)))
 
 (defn sq-sum [xs]
-  (let [sum (reduce + xs)]
+  (let [sum (apply + xs)]
     (* sum sum)))
 
 (deftest sq-sum-1

clojure/08-consecutive-digits.clj

 (def xs
   (map #(read-string (str %)) (str x)))
 
-(println
-  "Answers:"
-  (reduce max
-    (map #(reduce * %) 
+(defn consecutive-digits []
+  (apply max
+    (map #(apply * %)
       (map vector
         xs
         (drop 1 xs)
         (drop 2 xs)
         (drop 3 xs)
         (drop 4 xs)))))
+
+(deftest consecutive-digits-test
+  (is (= (consecutive-digits) 40824)))
+
+(run-tests)
+(println)
+(println "Answers:" (consecutive-digits))

clojure/10-sum-primes.clj

 (ns euler
   (:use clojure.contrib.lazy-seqs))
 
-(println (reduce + (take-while #(< % 2000000) primes)))
+(println (apply + (take-while #(< % 2000000) primes)))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.