You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/introduction/clojure-in-15-minutes.md
+22-21Lines changed: 22 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@
3
3
A quick tour of the Clojure syntax and common functions, which is so terse you can read through this page in around 15 minutes and have a basic understanding of the language.
4
4
5
5
!!! HINT "Try the code out in the REPL"
6
-
[Start a Clojure REPL]() or use a [Clojure aware editor](/clojure/clojure-editors/) connected to a REPL and experiment with these code examples.
6
+
[:fontawesome-solid-book-open: Start a Clojure REPL](/clojure/clojure-cli/repl/) or use a [:fontawesome-solid-book-open: Clojure aware editor](/clojure/clojure-editors/) connected to a REPL and experiment with these code examples.
7
7
8
8
Using the REPL provides instant feedback on each expression as they are evaluated, greatly increasing your understanding.
9
9
@@ -14,7 +14,7 @@ A quick tour of the Clojure syntax and common functions, which is so terse you c
14
14
15
15
`#_` comment reader macro to comment out the next form
16
16
17
-
`(comment )` form to comment all the containing forms, useful to [:fontawesome-solid-book-open: separate experimental and established code](https://practical.li/clojure/introduction/repl-workflow/#rich-comment-blocks-living-documentation) in a namespace.
17
+
`(comment ,,,)` form to comment all the containing forms, useful to [:fontawesome-solid-book-open: separate experimental and established code](/clojure/introduction/repl-workflow/#rich-comment-blocks-living-documentation) in a namespace.
18
18
19
19
20
20
## Clojure expressions
@@ -25,12 +25,12 @@ Clojure evaluates the first element in an expression as a function call. Additi
25
25
26
26
!!! EXAMPLE "Function call with value and expression as arguments"
27
27
```clojure
28
-
(+ 2007 (* 1 16))
28
+
(+ 2007 (* 1 16))
29
29
```
30
30
31
31
!!! EXAMPLE "Functions can be passed as an argument"
32
32
```clojure
33
-
(map inc (range 0 99))
33
+
(map inc (range 0 99))
34
34
```
35
35
36
36
## Organising Clojure
@@ -51,7 +51,7 @@ A company name or community repository name is often used making the namespace u
??? WARNING "Namespaces use dash, directory and file names use underscore"
@@ -92,15 +92,15 @@ Nesting forms defined a very precise calculation
92
92
```clojure
93
93
(* 1 2 (- 24 (* 7 3)))
94
94
```
95
-
96
-
`6` is returned as the value. Nested expressions are typically read inside out. `(* 7 3)` is `21`, giving `(- 24 21)` expression resulting in `3`. Finally the expression becomes `(* 1 2 3)`, resulting in a value of `6`
95
+
96
+
`6` is returned as the value. Nested expressions are typically read inside out. `(* 7 3)` is `21`, giving `(- 24 21)` expression resulting in `3`. Finally the expression becomes `(* 1 2 3)`, resulting in a value of `6`
97
97
98
98
99
99
Maintain precision for calculations using a Ratio type in Clojure
100
100
101
101
!!! EXAMPLE "Clojure Ratio value"
102
102
```clojure
103
-
(/ 27 7)
103
+
(/ 27 7) ; => 27/7
104
104
```
105
105
106
106
`22/7` is returned as the value, rather than a floating point value (double) which may loose some precision due to rounding.
@@ -145,7 +145,7 @@ A predicate is a function that returns a boolean `true` or `false` value and by
145
145
All other values are consider true.
146
146
147
147
148
-
[:fontawesome-solid-book-open: Clojure Standard Library Predicate Functions](https://practical.li/clojure/reference/standard-library/predicate-functions/){.md-button}
148
+
[:fontawesome-solid-book-open: Clojure Standard Library Predicate Functions](https://practical.li/clojure/reference/standard-library/predicate-functions/){.md-button}
149
149
150
150
151
151
## Collections & Sequences
@@ -182,10 +182,11 @@ Sequences are an interface for logical lists, which can be lazy. "Lazy" means th
182
182
183
183
A lazy sequence enables the use of large or even an infinite series, like so:
184
184
185
-
```clojure
186
-
(range) ; => (0 1 2 3 4 ...) - an infinite series
187
-
(take4 (range)) ; (0 1 2 3) - lazyily evaluate range and stop when enough values are taken
188
-
```
185
+
!!! EXAMPLE "Lazy sequences"
186
+
```clojure
187
+
(range) ; => (0 1 2 3 4 ...) - an infinite series
188
+
(take 4 (range)) ; (0 1 2 3) - lazyily evaluate range and stop when enough values are taken
189
+
```
189
190
190
191
Use cons to add an item to the beginning of a list or vector
Use `fn` to create new functions that defines some behaviour. `fn` is referred to as an anonymous fuction as it has no external name to be referenced by and must be called within a list form.
238
239
239
240
```clojure
240
-
(fn hello [] "Hello World")
241
+
(fn hello [] "Hello World")
241
242
```
242
243
243
-
Wrap a `(fn ,,,)` form in parens to call it and return the result.
244
+
Wrap a `(fn ,,,)` form in parens to call it and return the result.
244
245
245
246
!!! EXAMPLE "Call an anonymous function"
246
247
```clojure
@@ -254,16 +255,16 @@ Normally the anonymous function is used inline with other code
254
255
(map (fn [x] (* x 2)) [1 2 3 4 [1 2 3 4 5]5])
255
256
```
256
257
257
-
Make the anonymous function reusable by binding it to a shared name (`var`) using `def`.
258
+
Make the anonymous function reusable by binding it to a shared name (`var`) using `def`.
258
259
259
260
The `var` name bound to the function can now be called anywhere in the namespace.
260
261
261
262
> As `def` creates a `var` (variable) name, the developer can changed the expression the name is bound to and re-evaluated to use the changed behaviour.
262
263
263
264
!!! EXAMPLE "Bind a name to the anonymous function"
264
265
```clojure
265
-
(def hello-world
266
-
(fn hello [] "Hello World"))
266
+
(def hello-world
267
+
(fn hello [] "Hello World"))
267
268
```
268
269
269
270
!!! EXAMPLE "Evaluate annonymous function by evaluating its name"
@@ -282,7 +283,7 @@ It is more common to use the `defn` macro to define a function. This is the sam
282
283
```clojure
283
284
(defn hello-world
284
285
"I am a humble doc-string, please describe the function purpose"
285
-
[]
286
+
[]
286
287
"Hello World")
287
288
```
288
289
@@ -296,7 +297,7 @@ It is more common to use the `defn` macro to define a function. This is the sam
296
297
(hello-world)
297
298
```
298
299
299
-
The `[]` vector is used to define the argument names for the function. There can be zero or more arguments.
300
+
The `[]` vector is used to define the argument names for the function. There can be zero or more arguments.
300
301
301
302
!!! EXAMPLE "Call function with arguments"
302
303
```clojure
@@ -574,7 +575,7 @@ Vectors and Lists are java classes too!
574
575
575
576
!!! INFO "Type hints"
576
577
Type hints can be used to avoid reflection look-ups where performace critical issues have been identified. Type hints are not required in general.
577
-
[Clojure Type Hints](https://clojure.org/reference/java_interop#typehints){target=_blank .md-button}
578
+
[Clojure Type Hints](https://clojure.org/reference/java_interop#typehints){target=_blank .md-button}
0 commit comments