Skip to content

Commit 18e171c

Browse files
authored
Merge branch 'main' into elyses-enchantments
2 parents e1212d9 + f2058d8 commit 18e171c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1244
-199
lines changed

.github/workflows/exercise-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ jobs:
5858
run: julia --color=yes --project runtestrunner.jl
5959

6060
- name: Upload reports as artifact
61-
uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08
61+
uses: actions/upload-artifact@4cec3d8aa04e39d1a68397de0c4cd6fb9dce8ec1
6262
with:
6363
name: test-reports
6464
path: ${{ steps.generate-reports.outputs.results-path }}

concepts.wip/conditionals/.meta/config.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

concepts.wip/conditionals/about.md

Lines changed: 0 additions & 20 deletions
This file was deleted.

concepts.wip/conditionals/introduction.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

concepts.wip/conditionals/links.json

Lines changed: 0 additions & 6 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.

concepts.wip/vectors/.meta/config.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

concepts.wip/vectors/about.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

concepts.wip/vectors/introduction.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

concepts.wip/vectors/links.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

concepts/booleans/.meta/config.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"authors": [
3+
"colinleach"
4+
],
5+
"contributors": [
6+
"BNAndras"
7+
],
8+
"blurb": "Julia has boolean values true and false, operators ! (not), && (and), || (or)."
9+
}

concepts/booleans/about.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# About
2+
3+
## Booleans in Julia
4+
5+
True or false values are represented by the `Bool` type.
6+
It contains only two values: `true` and `false`.
7+
8+
```julia-repl
9+
julia> true
10+
true
11+
12+
julia> false
13+
false
14+
15+
julia> typeof(true)
16+
Bool
17+
```
18+
19+
In contrast to several other languages, Julia deliberately has no concept of "truthiness", and only expressions which evaluate to `true` or `false` will be treated as a `Bool`.
20+
21+
Specifically, empty arrays or strings will *not* be interpreted as `false`.
22+
There must be an appropriate test such as `isempty()` if you want special handling for empty values.
23+
24+
## Boolean operators
25+
26+
There are three [Boolean operators][boolean-operators] in Julia.
27+
28+
`&&` is Boolean "and".
29+
It evaluates to `true` if the expressions on *both* sides of `&&` are `true`.
30+
31+
```julia-repl
32+
julia> true && true
33+
true
34+
35+
julia> true && false
36+
false
37+
```
38+
39+
`||` is Boolean "or".
40+
It evaluates to `true` if an expression on *either* side of `||` is `true`.
41+
42+
```julia-repl
43+
julia> true || true
44+
true
45+
46+
julia> false || true
47+
true
48+
```
49+
50+
`!` is Boolean "not".
51+
It exchanges (inverts) `true` and `false` values.
52+
53+
```julia-repl
54+
julia> !true
55+
false
56+
57+
julia> !false
58+
true
59+
```
60+
61+
Similar operators will be conceptually familiar to users of many other languages, though the precise syntax may vary between languages.
62+
63+
## Operator precedence
64+
65+
In more complex expressions, it can be useful to know that `&&` has a slightly higher [precedence][operator-precedence] than `||` *(in the same way that `*` is applied before `+` in arithmetic expressions)*.
66+
67+
Relying on this can be confusing and error-prone.
68+
For clarity, use parentheses to make your intention clear.
69+
70+
## Short-circuit evaluation
71+
72+
Does the expression `true || x` depend on the value of `x`, or can the compiler ignore `x`?
73+
74+
Julia evaluates Boolean expressions from left to right, and stops when it has an unambiguous result.
75+
76+
For example, `true || x` must be `true`, regardless of `x`, so `x` is not evaluated.
77+
Similarly, `false && y` is `false`, with no need to evaluate `y`.
78+
79+
Conversely, `true && x` is `true` *only if* `x` is `true`, so `x` must be evaluated.
80+
81+
Similarly, if we chain multiple operators:
82+
83+
```julia-repl
84+
julia> true && false && something_else
85+
false
86+
```
87+
88+
Because `true && false` must be `false`, the `something_else` is unimportant and is ignored by the compiler.
89+
90+
In this case, `something_else` did not exist as a variable, but including it in this context gave no error *(test this in the REPL if you doubt it)*.
91+
92+
Such short-circuit evaluation is quite often used by Julia programmers as a shortcut to trap runtime problems and edge cases:
93+
94+
```julia
95+
all_ok || do_something()
96+
97+
is_problem && do_something_else()
98+
```
99+
100+
For example, the `do_something` might be an early `return` from the function if `all_ok` is `false`, or assigning a default value to a variable before continuing.
101+
102+
This is a slight abuse of Boolean syntax, but it can be very convenient.
103+
104+
## How Bools work internally
105+
106+
If a Bool is included in an *arithmetic* expression, `true` is interpreted as `1` and `false` as `0`, reflecting how they are stored.
107+
108+
If you are used to lower-level languages (C and similar), *please* avoid using this often.
109+
It will reduce code readability and make debugging harder.
110+
111+
You may sometimes see the numerical values used as a quick way to count how many things are `true`.
112+
113+
```julia-repl
114+
julia> true + false + true
115+
2
116+
```
117+
118+
[operator-precedence]: https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity
119+
[boolean-operators]: https://docs.julialang.org/en/v1/manual/mathematical-operations/#Boolean-Operators

concepts/booleans/introduction.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Introduction
2+
3+
## Booleans in Julia
4+
5+
True or false values are represented by the `Bool` type.
6+
It contains only two values: `true` and `false`.
7+
8+
```julia-repl
9+
julia> true
10+
true
11+
12+
julia> false
13+
false
14+
```
15+
16+
## Boolean Operators
17+
18+
There are three Boolean operators in Julia.
19+
20+
`&&` is Boolean "and".
21+
It evaluates to `true` if the expressions on *both* sides of `&&` are `true`.
22+
23+
```julia-repl
24+
julia> true && true
25+
true
26+
27+
julia> true && false
28+
false
29+
```
30+
31+
`||` is Boolean "or".
32+
It evaluates to `true` if an expression on *either* side of `||` is `true`.
33+
34+
```julia-repl
35+
julia> true || true
36+
true
37+
38+
julia> false || true
39+
true
40+
```
41+
42+
`!` is Boolean "not".
43+
It exchanges `true` and `false` values.
44+
45+
```julia-repl
46+
julia> !true
47+
false
48+
49+
julia> !false
50+
true
51+
```
52+
53+
For longer and more complicated expressions, it is best to use parentheses to make your intention clear.
54+
55+
```julia-repl
56+
julia> (true || false) && (false && true)
57+
false
58+
```

concepts/booleans/links.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"url": "https://docs.julialang.org/en/v1/manual/mathematical-operations/#Boolean-Operators",
4+
"description": "Boolean Operators, section in the manual"
5+
}
6+
]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"authors": [
3+
"colinleach"
4+
],
5+
"contributors": [],
6+
"blurb": "The conditionals 'if', 'elseif' ('else + if'), and 'else' are used to control the flow of execution and make decisions in a program."
7+
}

concepts/conditionals/about.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# About
2+
3+
## Comparison operators
4+
5+
[Comparison operators in Julia][numeric-comparisons] are similar to many other languages, though with some extra options for math-lovers.
6+
7+
For equality, the operators are `==` (equal) and `!=` or `` (not equal).
8+
9+
```julia
10+
txt = "abc"
11+
txt == "abc" # true
12+
txt != "abc" # false
13+
txt "abc" # false (synonym for !=)
14+
```
15+
16+
In addition, we have the various greater/less than operators.
17+
18+
```julia
19+
1 < 3 # true
20+
3 > 3 # false
21+
3 <= 3 # true
22+
3 3 # true (synonym for <=)
23+
4 >= 3 # true
24+
4 3 # true (synonym for >=)
25+
```
26+
27+
As often with Julia, an appropriate editor makes use of the mathematical symbol easy.
28+
Type `\ne`, `\le` or `\ge` then `TAB` to get ``, `` or ``.
29+
30+
The previous example uses only numbers, but we will see in other parts of the syllabus that various additional types have a sense of ordering and can be tested for greater/less than.
31+
32+
Comparison operators can be chained, which allows a clear and concise syntax:
33+
34+
```julia
35+
n = 3
36+
1 n 5 # true (n "between" two limits)
37+
```
38+
39+
The previous example is a synonym for `1 ≤ n && n ≤ 5`.
40+
41+
## Branching with `if`
42+
43+
This is the full form of an [`if` statement][conditional-eval]:
44+
45+
```julia
46+
if conditional1
47+
statements...
48+
elseif conditional2
49+
statements...
50+
else
51+
statements...
52+
end
53+
```
54+
55+
There is no need for parentheses `()` or braces `{}`, and indentation is "only" to improve readability _(but readability is very important!)_.
56+
57+
Both `elseif` and `else` are optional, and there can be multiple `elseif` blocks.
58+
However, the `end` is required.
59+
60+
It is possible to nest `if` statements, though you might want to help readability with the thoughtful use of parentheses, indents and comments.
61+
62+
The shortest form of an `if` statement would be something like this:
63+
64+
```julia
65+
if n < 0
66+
n = 0
67+
end
68+
```
69+
70+
As a reminder: only expressions that evaluate to `true` or `false` can be used as conditionals.
71+
Julia deliberately avoids any concept of "truthiness", so zero values, empty strings and empty arrays are _not_ equivalent to `false`.
72+
73+
## Ternary operator
74+
75+
A simple and common situation is picking one of two values based on a conditional.
76+
77+
Julia, like many languages, has a ternary operator to make this more concise.
78+
79+
The syntax is `conditional ? value_if_true : value_if_false`.
80+
81+
So the previous example could be rewritten:
82+
83+
```julia
84+
n = n < 0 ? 0 : n
85+
```
86+
87+
Parentheses are not required by the compiler, but may improve readability.
88+
89+
## Does Julia have a `match` statement?
90+
91+
No match/case structure is currently part of the base language.
92+
93+
For simple cases, use of `if..elseif..else` is recommended.
94+
95+
For sophisticated pattern matching, similar to Scala, the `Match.jl` package is popular.
96+
However, as with most third-party packages, `Match` is not available in the Exercism test runner.
97+
98+
99+
[numeric-comparisons]: https://docs.julialang.org/en/v1/manual/mathematical-operations/#Numeric-Comparisons
100+
[conditional-eval]: https://docs.julialang.org/en/v1/manual/control-flow/#man-conditional-evaluation

0 commit comments

Comments
 (0)