Skip to content

Commit 27cf056

Browse files
Merge pull request #6 from seancorfield/main
start of looking at transducers
2 parents 3aba2b9 + a8653d4 commit 27cf056

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ _site/
2929
/checkouts
3030
/classes
3131
/target
32+
.calva/repl.calva-repl
33+
.calva/mcp-server/port
34+
.portal/vs-code.edn

notebooks/clojure_camp/pairing.clj

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
(ns clojure-camp.pairing)
3+
4+
;; let's work on an algorithm together!
5+
6+
;; this produces a lazy sequence of strings:
7+
(map #(str "Hello, " % "!") ["Alice" "Bob" "Charlie"])
8+
9+
;; here's the non-lazy transducer-based version:
10+
(into []
11+
(map #(str "Hello, " % "!"))
12+
["Alice" "Bob" "Charlie"])
13+
14+
;; map with a single function argument produces a transducer:
15+
(map #(str "Hello, " % "!"))
16+
17+
;; we can build up xforms (transformers) using the `comp` function:
18+
(def xf (comp (filter #(> (count %) 3))
19+
(map #(str "Hello, " % "!"))))
20+
21+
;; and now use it with `into`:
22+
(into []
23+
xf ; Bob gets filtered out
24+
["Alice" "Bob" "Charlie"])
25+
26+
;; "traditional" SQL-like rows of columns:
27+
^:kind/table
28+
[{:name "Alice" :age 30 :city "Wonderland"}
29+
{:name "Bob" :age 25 :city "Builderland"}
30+
{:name "Charlie" :age 35 :city "Chocolate Factory"}]
31+
32+
;; more columnar format:
33+
^:kind/table
34+
{:name ["Alice" "Bob" "Charlie"]
35+
:age [30 25 35]
36+
:city ["Wonderland" "Builderland" "Chocolate Factory"]}
37+
38+
;; and display it as a portal widget:
39+
^:kind/portal
40+
{:name ["Alice" "Bob" "Charlie"]
41+
:age [30 25 35]
42+
:city ["Wonderland" "Builderland" "Chocolate Factory"]}

0 commit comments

Comments
 (0)