Skip to content

Commit

Permalink
intro: additional updates to clojure in 15
Browse files Browse the repository at this point in the history
  • Loading branch information
practicalli-johnny committed Sep 22, 2023
1 parent bf2d00c commit 53b715f
Showing 1 changed file with 49 additions and 29 deletions.
78 changes: 49 additions & 29 deletions docs/introduction/clojure-in-15-minutes.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ A company name or community repository name is often used making the namespace u
The `str` function creates a new string from all the arguments passed

!!! EXAMPLE "Combine strings into a single string value"
```clojure
(str "Hello" " " "World")
```
`"Hello World"` is returned from evaluating the expression.
```clojure
(str "Hello" " " "World")
```

`"Hello World"` is returned from evaluating the expression.

!!! HINT "clojure.string library for manipulating strings"
`clojure.string` library functions manipulate values and return string values (other clojure.core functions my return characters as results, e.g. `map`)
Expand Down Expand Up @@ -106,28 +106,49 @@ Maintain precision for calculations using a Ratio type in Clojure
`22/7` is returned as the value, rather than a floating point value (double) which may loose some precision due to rounding.


```clojure
(+ 1 (- 3 2)) ; = 1 + (3 - 2) => 2
```
### Equality

Equality is =
Equality is `=`

```clojure
(= 1 1) ; => true
(= 2 1) ; => false
```
!!! EXAMPLE "Equal values return a boolean true"
```clojure
(= 1 1) ; => true
```

!!! EXAMPLE "Unequals values return a boolean false"
```clojure
(= 2 1) ; => false
```

`true` and `false` are Boolean values
`true` and `false` are Boolean values and can be used literally in Clojure.

```clojure
(true? true) ; => true
(not true) ; => false
(not= true false) ; => true
(true? (complement true?)) ; => false
```

### Predicates

### Collections & Sequences
A predicate is a function that returns a boolean `true` or `false` value and by convention the function name ends in `?`, e.g. `true?`, `false?`, `seq?`, `even?`, `uuid?`.

`and` & `or` functions can be used to chain the results of predicates together for more interesting conditional tests.

!!! EXAMPLE "All predicates are true, returning true"
```clojure
(and (true? true) (not false)) ; => true
```

!!! EXAMPLE "One of the predicates or values is true"
```clojure
(or nil (not= true false) (true? (complement true?)) ) ; => true
```

!!! HINT "Truthy and Falsy values in Clojure"
`false` boolean value and `nil` value are considered false in Clojure.

All other values are consider true.


[:fontawesome-solid-book-open: Clojure Standard Library Predicate Functions](https://practical.li/clojure/reference/standard-library/predicate-functions/){.md-button}


## Collections & Sequences

The most common data collections in Clojure:

Expand All @@ -140,16 +161,15 @@ A list `()` is evaluated as a function call. The first element of the list the n

The `'` quote function informs the Clojure reader to treat the list as data only.

```clojure
'(1 2 3)
```

Lists and vectors are collections
!!! EXAMPLE "A quoted list is treated as data"
```clojure
'(1 2 3) ; => (1 2 3)
```

```clojure
(coll? '(1 2 3)) ; => true
(coll? [1 2 3]) ; => true
```
!!! EXAMPLE "Lists and vectors are collections"
```clojure
(and (coll? '(1 2 3)) (coll? [1 2 3])) ; => true
```

Only lists are sequences

Expand Down

0 comments on commit 53b715f

Please sign in to comment.