-
-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Booleans] new concept #781
Open
colinleach
wants to merge
9
commits into
exercism:main
Choose a base branch
from
colinleach:booleans
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+198
−0
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6cf0ac9
new concept `booleans`
colinleach 47921e7
Merge branch 'exercism:main' into booleans
colinleach 98d337d
Update concepts/booleans/about.md
colinleach e322b73
Update concepts/booleans/about.md
colinleach e08b195
Update concepts/booleans/introduction.md
colinleach 31026c3
parens
colinleach 46be2c5
Merge branch 'exercism:main' into booleans
colinleach 202fac9
rewrite of both `about.md` and `introduction.md`
colinleach 12396fd
Merge branch 'main' into booleans
colinleach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"authors": [ | ||
"colinleach" | ||
], | ||
"contributors": [ | ||
"BNAndras" | ||
], | ||
"blurb": "Julia has boolean values true and false, operators ! (not), && (and), || (or)." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# About | ||
|
||
## Booleans in Julia | ||
|
||
True or false values are represented by the `Bool` type. | ||
It contains only two values: `true` and `false`. | ||
|
||
```julia-repl | ||
julia> true | ||
true | ||
|
||
julia> false | ||
false | ||
|
||
julia> typeof(true) | ||
Bool | ||
``` | ||
|
||
In contrast to several other languages, Julia deliberately has no concept of "truthiness". | ||
Only expressions which evaluate to `true` or `false` will be treated as a `Bool`. | ||
|
||
Specifically, empty arrays or strings will *not* be interpreted as `false`. | ||
There must be an appropriate test such as `isempty()` if you want special handlind for empty values. | ||
|
||
## Boolean operators | ||
|
||
There are three [Boolean operators][boolean-operators] in Julia. | ||
|
||
`&&` is Boolean "and". | ||
It evaluates to `true` if the expressions on *both* sides of `&&` are `true`. | ||
|
||
```julia-repl | ||
julia> true && true | ||
true | ||
|
||
julia> true && false | ||
false | ||
``` | ||
|
||
`||` is Boolean "or". | ||
It evaluates to `true` if an expression on *either* side of `||` is `true`. | ||
|
||
```julia-repl | ||
julia> true || true | ||
true | ||
|
||
julia> false || true | ||
true | ||
``` | ||
|
||
`!` is Boolean "not". | ||
It exchanges (inverts) `true` and `false` values. | ||
|
||
```julia-repl | ||
julia> !true | ||
false | ||
|
||
julia> !false | ||
true | ||
``` | ||
|
||
These operators will be familiar to users of many other languages, though *not* Python. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sentence is confusing. |
||
|
||
## Operator precedence | ||
|
||
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)*. | ||
|
||
Relying on this can be confusing and error-prone. | ||
For clarity, use parentheses to make your intention clear. | ||
|
||
## Short-circuit evaluation | ||
|
||
Does the expression `true || x` depend on the value of `x`, or can the compiler ignore `x`? | ||
|
||
Julia evaluates Boolean expressions from left to right, and stops when it has an unambiguous result. | ||
|
||
For example, `true || x` must be `true`, regardless of `x`, so `x` is not evaluated. | ||
Similarly, `false && y` is `false`, with no need to evaluate `y`. | ||
|
||
Conversely, `true && x` is `true` *only if* `x` is `true`, so `x` must be evaluated. | ||
|
||
Similarly, if we chain multiple operators: | ||
|
||
```julia-repl | ||
julia> true && false && something_else | ||
false | ||
``` | ||
|
||
Because `true && false` must be `false`, the `something_else` is unimportant and is ignored by the compiler. | ||
|
||
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)*. | ||
|
||
Such short-circuit evaluation is quite often used by Julia programmers as a shortcut to trap runtime problems and edge cases: | ||
|
||
```julia | ||
all_ok || do_something() | ||
|
||
is_problem && do_something_else() | ||
``` | ||
|
||
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. | ||
|
||
This is a slight abuse of Boolean syntax, but it can be very convenient. | ||
|
||
## How Bools work internally | ||
|
||
If a Bool is included in an *arithmetic* expression, `true` is interpreted as `1` and `false` as `0`, reflecting how they are stored. | ||
|
||
If you are used to lower-level languages (C and similar), *please* avoid using this often. | ||
It will reduce code readability and make debugging harder. | ||
|
||
You may sometimes see the numerical values used as a quick way to count how many things are `true`. | ||
|
||
```julia-repl | ||
julia> true + false + true | ||
2 | ||
``` | ||
|
||
[operator-precedence]: https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity | ||
[boolean-operators]: https://docs.julialang.org/en/v1/manual/mathematical-operations/#Boolean-Operators |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Introduction | ||
|
||
## Booleans in Julia | ||
|
||
True or false values are represented by the `Bool` type. | ||
It contains only two values: `true` and `false`. | ||
|
||
```julia-repl | ||
julia> true | ||
true | ||
|
||
julia> false | ||
false | ||
``` | ||
|
||
## Boolean Operators | ||
|
||
There are three Boolean operators in Julia. | ||
|
||
`&&` is Boolean "and". | ||
It evaluates to `true` if the expressions on *both* sides of `&&` are `true`. | ||
|
||
```julia-repl | ||
julia> true && true | ||
true | ||
|
||
julia> true && false | ||
false | ||
``` | ||
|
||
`||` is Boolean "or". | ||
It evaluates to `true` if an expression on *either* side of `||` is `true`. | ||
|
||
```julia-repl | ||
julia> true || true | ||
true | ||
|
||
julia> false || true | ||
true | ||
``` | ||
|
||
`!` is Boolean "not". | ||
It exchanges `true` and `false` values. | ||
|
||
```julia-repl | ||
julia> !true | ||
false | ||
|
||
julia> !false | ||
true | ||
``` | ||
|
||
For longer and more complicated expressions, it is best to use parentheses to make your intention clear. | ||
|
||
```julia-repl | ||
julia> (true || false) && (false && true) | ||
false | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[ | ||
{ | ||
"url": "https://docs.julialang.org/en/v1/manual/mathematical-operations/#Boolean-Operators", | ||
"description": "Boolean Operators, section in the manual" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.