From db5c7af18f027ce250f3b9871693a18425de3565 Mon Sep 17 00:00:00 2001 From: PEZ Date: Sat, 7 Dec 2024 22:40:36 +0100 Subject: [PATCH 1/9] Use bb directory for Babashka instead of sharing the Clojure code Copies of the Clojure code for now --- fibonacci/bb/code.clj | 20 ++++++++++++++++++++ loops/bb/code.clj | 18 ++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 fibonacci/bb/code.clj create mode 100644 loops/bb/code.clj diff --git a/fibonacci/bb/code.clj b/fibonacci/bb/code.clj new file mode 100644 index 00000000..6fe0f204 --- /dev/null +++ b/fibonacci/bb/code.clj @@ -0,0 +1,20 @@ +(ns code + (:gen-class)) + +(set! *unchecked-math* :warn-on-boxed) + +(defn- fibonacci ^long [^long n] + ((fn fib ^long [^long n] + (if (<= n 1) + n + (+ (long (fib (- n 1))) + (long (fib (- n 2)))))) n)) + +(defn -main [& args] + (let [u (long (parse-long (first args))) + r (loop [i 1 + sum 0] + (if (< i u) + (recur (inc i) (+ sum (long (fibonacci i)))) + sum))] + (println r))) diff --git a/loops/bb/code.clj b/loops/bb/code.clj new file mode 100644 index 00000000..f1f48f62 --- /dev/null +++ b/loops/bb/code.clj @@ -0,0 +1,18 @@ +(ns code + (:gen-class)) + +(set! *unchecked-math* :warn-on-boxed) + +(defn -main [& args] + (let [u (long (parse-long (first args))) ; Get an input number from the command line + r (long (rand-int 10000)) ; Get a random number 0 <= r < 10k + a (long-array 10000)] ; Array of 10k elements initialized to 0 + (loop [i 0] + (when (< i 10000) ; 10k outer loop iterations + (loop [j 0] + (when (< j 100000) ; 100k inner loop iterations, per outer loop iteration + (aset a i (unchecked-add (aget a i) (rem j u))) ; Simple sum + (recur (unchecked-inc j)))) + (aset a i (unchecked-add (aget a i) r)) ; Add a random value to each element in array + (recur (unchecked-inc i)))) + (println (aget a r)))) ; Print out a single element from the array From f99a5720996912427a7292e79dc5ae525b3ac6dd Mon Sep 17 00:00:00 2001 From: PEZ Date: Sat, 7 Dec 2024 22:52:45 +0100 Subject: [PATCH 2/9] Babashka: Use a functional loops implementation (reduce) --- loops/bb/code.clj | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/loops/bb/code.clj b/loops/bb/code.clj index f1f48f62..eec14f30 100644 --- a/loops/bb/code.clj +++ b/loops/bb/code.clj @@ -1,18 +1,15 @@ -(ns code - (:gen-class)) +(defn main [u] + (let [r (rand-int 10000) ; Get a random number 0 <= r < 10k + v' (vec (repeat 10000 0)) ; Vector of 10k elements initialized to 0 + v (reduce (fn [v i] + (let [inner-sum (reduce + (fn [sum j] + (+ sum (rem j u))) ; Simple sum + (v i) + (range 100000))] ; 100k inner loop iterations, per outer loop iteration + (assoc v i (+ inner-sum r)))) ; Add a random value to each element in array + v' + (range 10000))] ; 10k outer loop iterations + (println (nth v r)))) ; Print out a single element from the array -(set! *unchecked-math* :warn-on-boxed) - -(defn -main [& args] - (let [u (long (parse-long (first args))) ; Get an input number from the command line - r (long (rand-int 10000)) ; Get a random number 0 <= r < 10k - a (long-array 10000)] ; Array of 10k elements initialized to 0 - (loop [i 0] - (when (< i 10000) ; 10k outer loop iterations - (loop [j 0] - (when (< j 100000) ; 100k inner loop iterations, per outer loop iteration - (aset a i (unchecked-add (aget a i) (rem j u))) ; Simple sum - (recur (unchecked-inc j)))) - (aset a i (unchecked-add (aget a i) r)) ; Add a random value to each element in array - (recur (unchecked-inc i)))) - (println (aget a r)))) ; Print out a single element from the array +(main (-> *command-line-args* first parse-long)) ; Get an input number from the command line \ No newline at end of file From 21f11f63b6dea6edc1496c3e82467d641972ebf4 Mon Sep 17 00:00:00 2001 From: PEZ Date: Sat, 7 Dec 2024 23:24:35 +0100 Subject: [PATCH 3/9] Babashka: Use a fully functional fibonacci (reduce) --- fibonacci/bb/code.clj | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/fibonacci/bb/code.clj b/fibonacci/bb/code.clj index 6fe0f204..0a04c43b 100644 --- a/fibonacci/bb/code.clj +++ b/fibonacci/bb/code.clj @@ -1,20 +1,14 @@ -(ns code - (:gen-class)) +(defn- fibonacci [n] + (if (<= n 1) + n + (+ (fibonacci (- n 1)) + (fibonacci (- n 2))))) -(set! *unchecked-math* :warn-on-boxed) - -(defn- fibonacci ^long [^long n] - ((fn fib ^long [^long n] - (if (<= n 1) - n - (+ (long (fib (- n 1))) - (long (fib (- n 2)))))) n)) - -(defn -main [& args] - (let [u (long (parse-long (first args))) - r (loop [i 1 - sum 0] - (if (< i u) - (recur (inc i) (+ sum (long (fibonacci i)))) - sum))] +(defn main [u] + (let [r (reduce (fn [sum i] + (+ sum (fibonacci i))) + 0 + (range 1 u))] (println r))) + +(main (-> *command-line-args* first parse-long)) \ No newline at end of file From 2c40202682ae95e32e400fed9772af9a56dd3102 Mon Sep 17 00:00:00 2001 From: PEZ Date: Sat, 7 Dec 2024 23:56:52 +0100 Subject: [PATCH 4/9] Babashka: Use mapv instead of reduce for outer loop --- loops/bb/code.clj | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/loops/bb/code.clj b/loops/bb/code.clj index eec14f30..12dc4f5f 100644 --- a/loops/bb/code.clj +++ b/loops/bb/code.clj @@ -1,15 +1,13 @@ (defn main [u] (let [r (rand-int 10000) ; Get a random number 0 <= r < 10k v' (vec (repeat 10000 0)) ; Vector of 10k elements initialized to 0 - v (reduce (fn [v i] - (let [inner-sum (reduce - (fn [sum j] - (+ sum (rem j u))) ; Simple sum - (v i) - (range 100000))] ; 100k inner loop iterations, per outer loop iteration - (assoc v i (+ inner-sum r)))) ; Add a random value to each element in array - v' - (range 10000))] ; 10k outer loop iterations + v (mapv (fn [initial-value] + (let [inner-sum (reduce (fn [sum j] + (+ sum (rem j u))) ; Simple sum + initial-value + (range 100000))] ; 100k inner loop iterations, per outer loop iteration + (+ inner-sum r))) ; Add a random value to each element in array + v')] ; 10k outer loop iterations (println (nth v r)))) ; Print out a single element from the array -(main (-> *command-line-args* first parse-long)) ; Get an input number from the command line \ No newline at end of file +(main (-> *command-line-args* first parse-long)) ; Get an input number from the command line From 69c5e5422f49e5793e20e9f04c20a7cc6f246641 Mon Sep 17 00:00:00 2001 From: PEZ Date: Sat, 7 Dec 2024 23:58:35 +0100 Subject: [PATCH 5/9] Babashka: Run as a plain bb script --- run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/run.sh b/run.sh index d5bd10ba..f5cc7cce 100755 --- a/run.sh +++ b/run.sh @@ -54,7 +54,7 @@ run "Chez Scheme" "chez --program" "./chez/code.so" run "AWK" "awk -f" "./awk/code.awk" run "MAWK" "mawk -f" "./awk/code.awk" run "Clojure" "java -cp clojure/classes:$(clojure -Spath)" "./clojure/code" -run "Babashka" "bb -cp clojure -m" "./babashka/code" +run "Babashka" "bb" "./bb/code.clj" run "COBOL" "" "./cobol/main" run "Octave" "octave ./octave/code.m 40" #run "F# AOT" "./fsharp/code-aot/code" From d4f401fffd75feb12eea781b5b63823db5c67c83 Mon Sep 17 00:00:00 2001 From: PEZ Date: Sat, 14 Dec 2024 11:18:11 +0100 Subject: [PATCH 6/9] fibonacci: Babashka: test for 0 and 1 explicitly And clarify summation of fibs --- fibonacci/bb/code.clj | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/fibonacci/bb/code.clj b/fibonacci/bb/code.clj index 0a04c43b..7ebcb6c0 100644 --- a/fibonacci/bb/code.clj +++ b/fibonacci/bb/code.clj @@ -1,14 +1,11 @@ (defn- fibonacci [n] - (if (<= n 1) - n + (case n + 0 0 + 1 1 (+ (fibonacci (- n 1)) (fibonacci (- n 2))))) (defn main [u] - (let [r (reduce (fn [sum i] - (+ sum (fibonacci i))) - 0 - (range 1 u))] - (println r))) + (println (reduce + (map fibonacci (range u))))) (main (-> *command-line-args* first parse-long)) \ No newline at end of file From 7791dafd40a6ee44a6dc9b1a43ba8deffb75b29e Mon Sep 17 00:00:00 2001 From: PEZ Date: Sat, 14 Dec 2024 16:17:24 +0100 Subject: [PATCH 7/9] fibonacci: Clojure make summation of fibs clearer --- fibonacci/clojure/code.clj | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/fibonacci/clojure/code.clj b/fibonacci/clojure/code.clj index 6fe0f204..9e7e8122 100644 --- a/fibonacci/clojure/code.clj +++ b/fibonacci/clojure/code.clj @@ -12,9 +12,5 @@ (defn -main [& args] (let [u (long (parse-long (first args))) - r (loop [i 1 - sum 0] - (if (< i u) - (recur (inc i) (+ sum (long (fibonacci i)))) - sum))] + r (transduce (map fibonacci) + (range u))] (println r))) From 46b4949c007d91945c2173cfe040066d106ea592 Mon Sep 17 00:00:00 2001 From: PEZ Date: Sat, 14 Dec 2024 16:57:58 +0100 Subject: [PATCH 8/9] hello-world: Add Babashka --- hello-world/bb/code.clj | 1 + 1 file changed, 1 insertion(+) create mode 100644 hello-world/bb/code.clj diff --git a/hello-world/bb/code.clj b/hello-world/bb/code.clj new file mode 100644 index 00000000..2cb98fb9 --- /dev/null +++ b/hello-world/bb/code.clj @@ -0,0 +1 @@ +(println "Hello, World!") \ No newline at end of file From 65c9164092f33c212c71fd1b9af9c5e89bc11538 Mon Sep 17 00:00:00 2001 From: PEZ Date: Sun, 15 Dec 2024 22:27:45 +0100 Subject: [PATCH 9/9] fibonacci: Clojure revert make summation of fibs clearer --- fibonacci/clojure/code.clj | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/fibonacci/clojure/code.clj b/fibonacci/clojure/code.clj index 9e7e8122..6fe0f204 100644 --- a/fibonacci/clojure/code.clj +++ b/fibonacci/clojure/code.clj @@ -12,5 +12,9 @@ (defn -main [& args] (let [u (long (parse-long (first args))) - r (transduce (map fibonacci) + (range u))] + r (loop [i 1 + sum 0] + (if (< i u) + (recur (inc i) (+ sum (long (fibonacci i)))) + sum))] (println r)))