Skip to content
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

trebuchet-intro + lessons #4

Merged
merged 15 commits into from
May 5, 2021
35 changes: 35 additions & 0 deletions .vscode/carpentries.snippet.code-snippets
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
// Place your julia-novice workspace snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
// is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
// used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
// Placeholders with the same ids are connected.
// Example:
// "Print to console": {
// "scope": "javascript,typescript",
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
"julia-codefence": {
"prefix": "~~~jl",
"body": ["~~~",
"$1",
"~~~",
"{: .language-julia}"
]
}

"output-codefence": {
"prefix": "~~~o",
"body": ["~~~",
"$1",
"~~~",
"{: .output}"
]
}
}
60 changes: 56 additions & 4 deletions _episodes/02-REPL.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ questions:
- "How to use the REPL?"
objectives:
- "Explore basic functionality of input."
- "Explain how to interpret the ouput of the REPL."
- "Explain help, shell and pkg mode."
- "Show limitations when using functions and files."
- "Provide solution by using the Revise package."
- "Learn how to declare variables."
- "Learn about REPL modes."
keypoints:
- "The REPL reads the given input, evaluates the given expression and prints the resulting output to the user."
- "Pressing <kbd>?</kbd> enters help mode."
Expand All @@ -20,4 +18,58 @@ keypoints:

---

# Entering the REPL

## Variables

After downloading and executing a julia binary from https://julialang.org Melissa and her classmates face the so called REPL, which stands for <u>r</u>ead-<u>e</u>valuate-<u>p</u>rint-<u>l</u>oop.
The first thing they try is to perform basic arithmetic operations
~~~
1 + 4 * 7.3
~~~
{: .language-julia}
~~~
30.2
~~~
{: .output}

That works as expected.
It is also possible to bind names to values via the assignment operator `=`, which makes it easier to refer to them later on.
These names are called _variables_.

~~~
distance = 30.2
distance_x_2 = 2 * distance
~~~
{: .language-julia}
~~~
60.4
~~~
{: .output}

Melissa notices that assignment also returns the value.

## Unicode

In julia also unicode characters are allowed as variables like `α = 2`.
Where unicode characters can be entered by a backslash followed by their LaTeX-name and then pressing <kbd>tab</kbd> (in this case `\alpha`<kbd>tab</kbd>).
BeastyBlacksmith marked this conversation as resolved.
Show resolved Hide resolved

## REPL-modes

Unfortunately Melissa can't remember the LaTeX name of ∂ so she copies the character, presses <kbd>?</kbd> to enter the help mode, pastes the character and gets
~~~
help?> ∂
"∂" can be typed by \partial<tab>
~~~
{: .output}

Great! This way she can easily look up the names she needs.
She gets back to normal mode by pressing backspace.

Another useful mode is the shell mode that can be entered by pressing <kbd>;</kbd>.
It can be used to use commands of the underlying shell, but don't confuse it with an actual shell: Special shell syntax like piping won't work.

Finally there is the package mode that is enetered with <kbd>]</kbd> which is used for package management, which will be covered later on.

{% include links.md %}

97 changes: 97 additions & 0 deletions _episodes/03-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,101 @@ keypoints:
- "Parametric types are useful for flexible yet performant code."
---

## Structuring variables

Melissa wants to keep the variables corresponding to the trebuchet (`counterweight`, `release_angle`) separate from the variables coming from the environment (`wind`, `target_distance`).
That is why she choses to group them together using `struct`s:

~~~
struct Trebuchet
counterweight::Float64
release_angle::Float64
end

struct Environment
wind::Float64
target_distance::Float64
end
~~~
{: .language-julia}

### Types and hierarchy

Here `::Float64` is a type specification, indicating that this variable should be a 64-bit floating point number.
If Melissa hadn't specified the type, the variables would have the type `Any` by default.

In julia every type can have only one supertype, so lets check how many types are between `Float64` and `Any`

~~~
julia> supertype(Float64)
~~~
{: .language-julia}

~~~
AbstractFloat
~~~
{: .output}

~~~
julia> supertype(AbstractFloat)
~~~
{: .language-julia}

~~~
Real
~~~
{: .output}

~~~
julia> supertype(Real)
~~~
{: .language-julia}

~~~
Number
~~~
{: .output}

~~~
julia> supertype(Number)
~~~
{: .language-julia}

~~~
Any
~~~
{: .output}

So we have the relationship `Float64 <: AbstractFloat <: Real <: Number <: Any`, where `<:` means "subtype of".

`Float64` is a _concrete_ type, which means that you can actually create objects of this type.
For example `1.0` is a object of type `Float64`.
We can check this at the REPL:
~~~
julia> 1.0 isa Float64
~~~
{: .language-julia}

~~~
true
~~~
{: .output}

All the other types are _abstract_ types that are used to adress groups of types.
For example, if we declare a variable as `a::Real` then it can be bound to any value that is a subtype of `Real`.

Let's quickly check what are all the subtypes of `Real`:
~~~
julia> subtypes(Real)
~~~
{: .language-julia}

~~~
4-element Array{Any,1}:
AbstractFloat
AbstractIrrational
Integer
Rational
~~~
{: .output}
{% include links.md %}
14 changes: 0 additions & 14 deletions _episodes/04-dispatch.md

This file was deleted.

56 changes: 56 additions & 0 deletions _episodes/04-pkg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
title: "Using the package manager"
teaching: 30
exercises: 0
questions:
-
objectives:
-
keypoints:
-
-
---

## The package manager

Now it is time for Melissa and their mates to simulate the launch of the trebuchet.
The necessary equations are really complicated, but an investigation on https://juliahub.com/ revealed that someone already implemented these and published it as the julia package `Trebuchet.jl`.
That spares some real work.

Melissa enters the package mode by pressing <kbd>]</kbd>.
The `julia>` prompt becomes a blue prompt that reads the julia version that Melissa is running.
After consulting the [documentation](https://julialang.github.io/Pkg.jl/v1/) she knows that the prompt is showing the currently activated environment and that this is the global environment that is activated by default.

However, she doesn't want to clutter the global environment when working on her project, so she creates a new environment via
~~~
(v1.x) pkg> activate projects/trebuchet
~~~
{: .language-julia}

In this environment she adds the `Trebuchet` package by typing
~~~
(trebuchet) pkg> add Trebuchet
~~~
{: .language-julia}

Melissa quickly recognizes that far more packages are being installed than just `Trebuchet`.
These are the dependencies of `Trebuchet`.

From the output
~~~
[...]
Updating `[...]/projects/trebuchet/Project.toml`
[98b73d46] + Trebuchet v0.2.1
Updating `[...]/projects/trebuchet/Manifest.toml`
[1520ce14] + AbstractTrees v0.3.3
[79e6a3ab] + Adapt v1.1.0
[...]

~~~
{: .output}

she sees that two files were created: `Project.toml` and `Manifest.toml`.

The project file `Project.toml` only contains the packages needed for her project, while the manifest file `Manifest.toml` records the direct and indirect dependencies as well as their current version, thus providing a fully reproducible record of the code that is actually executed.
"That is really handy when I want to share my work with the others." thinks Melissa.
{% include links.md %}
15 changes: 0 additions & 15 deletions _episodes/05-compilation.md

This file was deleted.

Loading