Skip to content

Commit

Permalink
removing unnecessary trailing spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
michalporeba committed Jan 13, 2024
1 parent 4f173ae commit e48a480
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@

(defn leap-year? [year]
(and (divides? year 4)
(or (not (divides? year 100))
(or (not (divides? year 100))
(divides? year 400))))
```

At the core of this approach, three checks are returning three boolean values.
We can use [Boolean logic](https://en.wikipedia.org/wiki/Boolean_algebra) to combine the results. Multiple variations are possible. One is shown above, but the below is also possible.
At the core of this approach, three checks are returning three boolean values.
We can use [Boolean logic](https://en.wikipedia.org/wiki/Boolean_algebra) to combine the results. Multiple variations are possible. One is shown above, but the below is also possible.

```clojure
(defn leap-year? [year]
(defn leap-year? [year]
(or (divides? year 400)
(and (not (divides? year 100))
(and (not (divides? year 100))
(divides? year 4))))
```

## Being explicit

The above examples use a private function `divides?` to be more explicit, to show what logical check is done rather than what operation is performed.
The above examples use a private function `divides?` to be more explicit, to show what logical check is done rather than what operation is performed.
This is common in Clojure code, but it is also possible to do the checks directly.

```clojure
Expand All @@ -32,14 +32,14 @@ This is common in Clojure code, but it is also possible to do the checks directl
(zero? year 400))))
```

In this example, in addition, instead of negating the second check, we check if the reminder from the division is greater than zero.
In this example, in addition, instead of negating the second check, we check if the reminder from the division is greater than zero.

If the concern is that defining `divides?` adds confusion, because it is not known in the only place where it is used, `letfn` can be used. Here is another example.
If the concern is that defining `divides?` adds confusion, because it is not known in the only place where it is used, `letfn` can be used. Here is another example.

```clojure
(defn leap-year? [year]
(letfn [(is-multiple-of? [n] (zero? (mod year n)))]
(and (is-multiple-of? 4)
(or (not (is-multiple-of? 100))
(or (not (is-multiple-of? 100))
(is-multiple-of? 400)))))
```
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defn leap-year? [year]
(and (divides? year 4)
(or (not (divides? year 100))
(or (not (divides? year 100))
(divides? year 400))))
8 changes: 4 additions & 4 deletions exercises/practice/leap/.approaches/data-shapes/content.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
some?))
```

We can define a data structure that describes the properties of a number.
We can define a data structure that describes the properties of a number.
In this case, the number is a year, and the properties are whether it is divisible by 4, 100 and 400. Let's call any instance of such data structure the shape of a number.

While there are many numbers, there are fewer shapes, so we can define all shapes corresponding to all leap years. There are only two.
While there are many numbers, there are fewer shapes, so we can define all shapes corresponding to all leap years. There are only two.

```clojure
(def leap-shapes
#{{:by-4 true :by-100 true :by-400 true}
{:by-4 true :by-100 false :by-400 false}})
```

We now need a function to convert any number to its shape.
We now need a function to convert any number to its shape.

```clojure
(defn- to-shape [year]
Expand All @@ -37,7 +37,7 @@ We now need a function to convert any number to its shape.
:by-400 (zero? (mod year 400))})
```

With the above, we can now take a year number and check if its shape is one of the leap shapes.
With the above, we can now take a year number and check if its shape is one of the leap shapes.

```clojure
(defn leap-year? [year]
Expand Down
30 changes: 15 additions & 15 deletions exercises/practice/leap/.approaches/flow-control/content.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,42 @@
(defn- divides? [number divisor]
(zero? (mod number divisor)))

(defn leap-year? [year]
(if (divides? year 100)
(divides? year 400)
(defn leap-year? [year]
(if (divides? year 100)
(divides? year 400)
(divides? year 4)))
```

## If

Clojure provides `if`, `cond`, `condp` and `case` expressions to control the flow of the program.

The `if` doesn't have an `else if` option, but it is not necessary here.
We can use `if` once to check if the year is divisible by 100.
If it is, then whether it is a leap year or not depends if it is divisible by 400.
If it is not, then whether it is a leap year or not depends if it is divisible by 4.
The `if` doesn't have an `else if` option, but it is not necessary here.
We can use `if` once to check if the year is divisible by 100.
If it is, then whether it is a leap year or not depends if it is divisible by 400.
If it is not, then whether it is a leap year or not depends if it is divisible by 4.

```clojure
(defn leap-year? [year]
(if (divides? year 100)
(divides? year 400)
(if (divides? year 100)
(divides? year 400)
(divides? year 4)))
```

## Cond

Another option is `cond` which allows for evaluating multiple conditions, similar to `else if` in other languages.
Another option is `cond` which allows for evaluating multiple conditions, similar to `else if` in other languages.

```clojure
(defn leap-year? [year]
(cond
(cond
(divides? year 400) true
(divides? year 100) false
(divides? year 4) true
:else false))
```

A very similar alternative is to use the `condp` macro, which takes a predicate and applies it to a series of test value and expected result pairs.
A very similar alternative is to use the `condp` macro, which takes a predicate and applies it to a series of test value and expected result pairs.

```clojure
(defn leap-year? [year]
Expand All @@ -50,18 +50,18 @@ A very similar alternative is to use the `condp` macro, which takes a predicate
false))
```

When using both `cond` and `condp,` the other matters as the first true condition stops evaluating the list and determines the result.
When using both `cond` and `condp,` the other matters as the first true condition stops evaluating the list and determines the result.

## Case

Finally, it is also possible to use `case`, but this solution is not popular.
Finally, it is also possible to use `case`, but this solution is not popular.

```clojure
(defn leap-year? [year]
(case [(divides? year 400)
(divides? year 100)
(divides? year 4)
]
]
[true true true] true
[false true true] false
[false false true] true
Expand Down
6 changes: 3 additions & 3 deletions exercises/practice/leap/.approaches/flow-control/snippet.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defn leap-year? [year]
(if (divides? year 100)
(divides? year 400)
(defn leap-year? [year]
(if (divides? year 100)
(divides? year 400)
(divides? year 4)))

0 comments on commit e48a480

Please sign in to comment.