From a8653d4974f105c16bd0a21d8f405b6c3adf7ec3 Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Fri, 30 May 2025 14:46:06 -0400 Subject: [PATCH] start of looking at transducers Signed-off-by: Sean Corfield --- .gitignore | 3 +++ notebooks/clojure_camp/pairing.clj | 42 ++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 notebooks/clojure_camp/pairing.clj diff --git a/.gitignore b/.gitignore index 91650535..df065b8c 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,6 @@ _site/ /checkouts /classes /target +.calva/repl.calva-repl +.calva/mcp-server/port +.portal/vs-code.edn diff --git a/notebooks/clojure_camp/pairing.clj b/notebooks/clojure_camp/pairing.clj new file mode 100644 index 00000000..df9207cd --- /dev/null +++ b/notebooks/clojure_camp/pairing.clj @@ -0,0 +1,42 @@ + +(ns clojure-camp.pairing) + +;; let's work on an algorithm together! + +;; this produces a lazy sequence of strings: +(map #(str "Hello, " % "!") ["Alice" "Bob" "Charlie"]) + +;; here's the non-lazy transducer-based version: +(into [] + (map #(str "Hello, " % "!")) + ["Alice" "Bob" "Charlie"]) + +;; map with a single function argument produces a transducer: +(map #(str "Hello, " % "!")) + +;; we can build up xforms (transformers) using the `comp` function: +(def xf (comp (filter #(> (count %) 3)) + (map #(str "Hello, " % "!")))) + +;; and now use it with `into`: +(into [] + xf ; Bob gets filtered out + ["Alice" "Bob" "Charlie"]) + +;; "traditional" SQL-like rows of columns: +^:kind/table +[{:name "Alice" :age 30 :city "Wonderland"} + {:name "Bob" :age 25 :city "Builderland"} + {:name "Charlie" :age 35 :city "Chocolate Factory"}] + +;; more columnar format: +^:kind/table +{:name ["Alice" "Bob" "Charlie"] + :age [30 25 35] + :city ["Wonderland" "Builderland" "Chocolate Factory"]} + +;; and display it as a portal widget: +^:kind/portal +{:name ["Alice" "Bob" "Charlie"] + :age [30 25 35] + :city ["Wonderland" "Builderland" "Chocolate Factory"]}