Last active
August 5, 2024 15:27
-
-
Save chase-lambert/da9a0867db3f9c9441730a722ba963a4 to your computer and use it in GitHub Desktop.
rendezvous with cassidoo challenge: 24.08.05
This file contains 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
(ns squares | |
(:require [clojure.test :refer [deftest is]] | |
[criterium.core :as cc])) | |
(defn squares [n] | |
(let [nums (range 1 (inc n))] | |
(->> nums | |
(map #(* % %)) | |
(reduce +)))) | |
(deftest squares-test | |
(is (= 55 (squares 5))) | |
(is (= 385 (squares 10))) | |
(is (= 5525 (squares 25))) | |
(is (= 338350 (squares 100)))) | |
(defn squares-tranduce [n] | |
(let [nums (range 1 (inc n))] | |
(transduce (map #(* % %)) + nums))) | |
(deftest squares-tranduce-test | |
(is (= 55 (squares-tranduce 5))) | |
(is (= 385 (squares-tranduce 10))) | |
(is (= 5525 (squares-tranduce 25))) | |
(is (= 338350 (squares-tranduce 100)))) | |
(comment | |
(cc/quick-bench (squares 100000)) ;; => ~2.85 ms | |
(cc/quick-bench (squares-tranduce 100000)) ;; => ~1.85 ms | |
(/ 2.85 1.85) ;; transducer approach is 1.54x faster | |
,) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment