Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitba141d0

Browse files
the-freytrptcolin
authored andcommitted
Add some examples for threading macros and a couple of simple transducer koans
1 parent9481906 commitba141d0

File tree

3 files changed

+112
-6
lines changed

3 files changed

+112
-6
lines changed

‎resources/koans.clj‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,24 @@
259259
"Giants"]}]
260260

261261
["24_macros" {"__" [~(first form)
262-
~(nth form2)
263-
form
264-
(drop2 form)
265-
"Hello, Macros!"
266-
10
267-
'(+91)]}]
262+
~(nth form2)
263+
form
264+
(drop2 form)
265+
"Hello, Macros!"
266+
10
267+
'(+91)]}]
268+
269+
["25_threading_macros" {"__" [{:a1}
270+
"Hello world, and moon, and stars"
271+
"String with a trailing space"
272+
6
273+
1
274+
[234]
275+
12
276+
[123]]}]
268277

278+
["26_transducers" {"__" [[24]
279+
[24]
280+
[24]
281+
6]}]
269282
]

‎src/koans/25_threading_macros.clj‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
(nskoans.25-threading-macros
2+
(:require [koan-engine.core:refer:all]))
3+
4+
(defa-list
5+
'(12345))
6+
7+
(defa-list-with-maps
8+
'({:a1} {:a2} {:a3}))
9+
10+
(defnfunction-that-takes-a-map [m a b]
11+
(do
12+
(println (str"Other unused arguments:" a"" b))
13+
(get m:a)))
14+
15+
(defnfunction-that-takes-a-coll [a b coll]
16+
(do
17+
(println (str"Other unused arguments:" a"" b))
18+
(map:a coll)))
19+
20+
(meditations
21+
"We can use thread first for more readable sequential operations"
22+
(= __
23+
(-> {}
24+
(assoc:a1)))
25+
26+
"Consider also the case of strings"
27+
(= __
28+
(->"Hello world"
29+
(str", and moon")
30+
(str", and stars")))
31+
32+
"When a function has no arguments to partially apply, just reference it"
33+
(= __
34+
(->"String with a trailing space"
35+
clojure.string/trim))
36+
37+
"Most operations that take a scalar value as an argument can be threaded-first"
38+
(= __
39+
(-> {}
40+
(assoc:a1)
41+
(assoc:b2)
42+
(assoc:c {:d4
43+
:e5})
44+
(update-in [:c:e] inc)
45+
(get-in [:c:e])))
46+
47+
"We can use functions we have written ourselves that follow this pattern"
48+
(= __
49+
(-> {}
50+
(assoc:a1)
51+
(function-that-takes-a-map"hello""there")))
52+
53+
"We can also thread last using ->>"
54+
(= __
55+
(->> [123]
56+
(map inc)))
57+
58+
"Most operations that take a collection can be threaded-last"
59+
(= __
60+
(->> a-list
61+
(map inc)
62+
(filter even?)
63+
(into [])
64+
(reduce +)))
65+
66+
"We can use funtions we have written ourselves that follow this pattern"
67+
(= __
68+
(->> a-list-with-maps
69+
(function-that-takes-a-coll"hello""there")
70+
(into []))))

‎src/koans/26_transducers.clj‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
(nskoans.26-transducers
2+
(:require [koan-engine.core:refer:all]))
3+
4+
(defxfms
5+
(comp (map inc)
6+
(filter even?)))
7+
8+
(meditations
9+
"Consider that sequence operations can be used as transducers"
10+
(= __
11+
(transduce xfms conj [123]))
12+
13+
"We can do this eagerly"
14+
(= __
15+
(into [] xfms [123]))
16+
17+
"Or lazily"
18+
(= __
19+
(sequence xfms [123]))
20+
21+
"The transduce function can combine mapping and reduction"
22+
(= __
23+
(transduce xfms + [123])))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp