From 09a8a96664fadbc2933024910b95526f7d66843f Mon Sep 17 00:00:00 2001 From: Louis Pilfold Date: Thu, 4 Jan 2024 20:06:40 +0000 Subject: More content! --- .../chapter0_basics/lesson12_blocks/code.gleam | 13 ------------ .../chapter0_basics/lesson12_blocks/text.html | 23 ---------------------- .../lesson12_type_aliases/code.gleam | 12 +++++++++++ .../lesson12_type_aliases/text.html | 8 ++++++++ .../chapter0_basics/lesson13_blocks/code.gleam | 13 ++++++++++++ .../chapter0_basics/lesson13_blocks/text.html | 23 ++++++++++++++++++++++ .../chapter0_basics/lesson13_lists/code.gleam | 16 --------------- .../chapter0_basics/lesson13_lists/text.html | 19 ------------------ .../chapter0_basics/lesson14_lists/code.gleam | 16 +++++++++++++++ .../chapter0_basics/lesson14_lists/text.html | 19 ++++++++++++++++++ .../chapter0_basics/lesson15_constants/code.gleam | 13 ++++++++++++ .../chapter0_basics/lesson15_constants/text.html | 18 +++++++++++++++++ .../lesson00_todo/code.gleam | 7 +++++++ .../lesson00_todo/text.html | 14 +++++++++++++ .../lesson01_panic/code.gleam | 15 ++++++++++++++ .../lesson01_panic/text.html | 11 +++++++++++ 16 files changed, 169 insertions(+), 71 deletions(-) delete mode 100644 src/content/chapter0_basics/lesson12_blocks/code.gleam delete mode 100644 src/content/chapter0_basics/lesson12_blocks/text.html create mode 100644 src/content/chapter0_basics/lesson12_type_aliases/code.gleam create mode 100644 src/content/chapter0_basics/lesson12_type_aliases/text.html create mode 100644 src/content/chapter0_basics/lesson13_blocks/code.gleam create mode 100644 src/content/chapter0_basics/lesson13_blocks/text.html delete mode 100644 src/content/chapter0_basics/lesson13_lists/code.gleam delete mode 100644 src/content/chapter0_basics/lesson13_lists/text.html create mode 100644 src/content/chapter0_basics/lesson14_lists/code.gleam create mode 100644 src/content/chapter0_basics/lesson14_lists/text.html create mode 100644 src/content/chapter0_basics/lesson15_constants/code.gleam create mode 100644 src/content/chapter0_basics/lesson15_constants/text.html create mode 100644 src/content/chapter5_advance_features/lesson00_todo/code.gleam create mode 100644 src/content/chapter5_advance_features/lesson00_todo/text.html create mode 100644 src/content/chapter5_advance_features/lesson01_panic/code.gleam create mode 100644 src/content/chapter5_advance_features/lesson01_panic/text.html (limited to 'src') diff --git a/src/content/chapter0_basics/lesson12_blocks/code.gleam b/src/content/chapter0_basics/lesson12_blocks/code.gleam deleted file mode 100644 index 31e4729..0000000 --- a/src/content/chapter0_basics/lesson12_blocks/code.gleam +++ /dev/null @@ -1,13 +0,0 @@ -import gleam/io - -pub fn main() { - let fahrenheit = { - let degrees = 64 - degrees - } - // io.debug(degrees) // <- This will not compile - - // Changing order of evaluation - let celsius = { fahrenheit - 32 } * 5 / 9 - io.debug(celsius) -} diff --git a/src/content/chapter0_basics/lesson12_blocks/text.html b/src/content/chapter0_basics/lesson12_blocks/text.html deleted file mode 100644 index bc82e39..0000000 --- a/src/content/chapter0_basics/lesson12_blocks/text.html +++ /dev/null @@ -1,23 +0,0 @@ -

- Blocks are one or more expressions grouped together with curly braces. Each - expression is evaluated in order and the value of the last expression is - returned. -

-

- Any variables assigned within the block can only be used within the block. -

-

- Try uncommenting io.debug(degrees) to see the compile error from - trying to use a variable that is not in scope. -

-

- Blocks can also be used to change the order of evaluation of binary operators - expressions. -

-

- * binds more tightly than + so the expression - 1 + 2 * 3 evaluates to 7. If the 1 + 2 should be - evaluated first to make the expression evaluate to 9 then the expression can be - wrapped in a block: { 1 + 2 } * 3. This is similar to grouping - with parentheses in some other languages. -

diff --git a/src/content/chapter0_basics/lesson12_type_aliases/code.gleam b/src/content/chapter0_basics/lesson12_type_aliases/code.gleam new file mode 100644 index 0000000..6125ffe --- /dev/null +++ b/src/content/chapter0_basics/lesson12_type_aliases/code.gleam @@ -0,0 +1,12 @@ +import gleam/io + +pub type UserId = + Int + +pub fn main() { + let one: UserId = 1 + let two: Int = 2 + + // UserId and Int are the same type + io.debug(one == two) +} diff --git a/src/content/chapter0_basics/lesson12_type_aliases/text.html b/src/content/chapter0_basics/lesson12_type_aliases/text.html new file mode 100644 index 0000000..9935582 --- /dev/null +++ b/src/content/chapter0_basics/lesson12_type_aliases/text.html @@ -0,0 +1,8 @@ +

+ A type alias can be used to refer to a type by a different name. Giving a type + an alias doesn't make a new type, it is still the same type. +

+

+ When the pub keyword is used the type alias is public and can be + referred to by other module. +

diff --git a/src/content/chapter0_basics/lesson13_blocks/code.gleam b/src/content/chapter0_basics/lesson13_blocks/code.gleam new file mode 100644 index 0000000..31e4729 --- /dev/null +++ b/src/content/chapter0_basics/lesson13_blocks/code.gleam @@ -0,0 +1,13 @@ +import gleam/io + +pub fn main() { + let fahrenheit = { + let degrees = 64 + degrees + } + // io.debug(degrees) // <- This will not compile + + // Changing order of evaluation + let celsius = { fahrenheit - 32 } * 5 / 9 + io.debug(celsius) +} diff --git a/src/content/chapter0_basics/lesson13_blocks/text.html b/src/content/chapter0_basics/lesson13_blocks/text.html new file mode 100644 index 0000000..bc82e39 --- /dev/null +++ b/src/content/chapter0_basics/lesson13_blocks/text.html @@ -0,0 +1,23 @@ +

+ Blocks are one or more expressions grouped together with curly braces. Each + expression is evaluated in order and the value of the last expression is + returned. +

+

+ Any variables assigned within the block can only be used within the block. +

+

+ Try uncommenting io.debug(degrees) to see the compile error from + trying to use a variable that is not in scope. +

+

+ Blocks can also be used to change the order of evaluation of binary operators + expressions. +

+

+ * binds more tightly than + so the expression + 1 + 2 * 3 evaluates to 7. If the 1 + 2 should be + evaluated first to make the expression evaluate to 9 then the expression can be + wrapped in a block: { 1 + 2 } * 3. This is similar to grouping + with parentheses in some other languages. +

diff --git a/src/content/chapter0_basics/lesson13_lists/code.gleam b/src/content/chapter0_basics/lesson13_lists/code.gleam deleted file mode 100644 index 646ad6e..0000000 --- a/src/content/chapter0_basics/lesson13_lists/code.gleam +++ /dev/null @@ -1,16 +0,0 @@ -import gleam/io - -pub fn main() { - let ints = [1, 2, 3] - - io.debug(ints) - - // Immutably prepend - io.debug([-1, 0, ..ints]) - - // Uncomment this to see the error - // io.debug(["zero", ..ints]) - - // The original lists are unchanged - io.debug(ints) -} diff --git a/src/content/chapter0_basics/lesson13_lists/text.html b/src/content/chapter0_basics/lesson13_lists/text.html deleted file mode 100644 index dd07cd7..0000000 --- a/src/content/chapter0_basics/lesson13_lists/text.html +++ /dev/null @@ -1,19 +0,0 @@ -

- Lists are ordered collections of values. -

-

- List is a generic type, having a type parameter - for the type of values it contains. A list of ints has the type - List(Int), and a list of strings has the type - List(String). -

-

- Lists are immutable single-linked lists, meaning they are very efficient to - add and remove elements from the front of the list. -

-

- Counting the length of a list or getting elements from other positions in the - list is expensive and rarely done. It is rare to write algorithms that index - into sequences in Gleam, but but when they are written a list is not the right - choice of data structure. -

diff --git a/src/content/chapter0_basics/lesson14_lists/code.gleam b/src/content/chapter0_basics/lesson14_lists/code.gleam new file mode 100644 index 0000000..646ad6e --- /dev/null +++ b/src/content/chapter0_basics/lesson14_lists/code.gleam @@ -0,0 +1,16 @@ +import gleam/io + +pub fn main() { + let ints = [1, 2, 3] + + io.debug(ints) + + // Immutably prepend + io.debug([-1, 0, ..ints]) + + // Uncomment this to see the error + // io.debug(["zero", ..ints]) + + // The original lists are unchanged + io.debug(ints) +} diff --git a/src/content/chapter0_basics/lesson14_lists/text.html b/src/content/chapter0_basics/lesson14_lists/text.html new file mode 100644 index 0000000..dd07cd7 --- /dev/null +++ b/src/content/chapter0_basics/lesson14_lists/text.html @@ -0,0 +1,19 @@ +

+ Lists are ordered collections of values. +

+

+ List is a generic type, having a type parameter + for the type of values it contains. A list of ints has the type + List(Int), and a list of strings has the type + List(String). +

+

+ Lists are immutable single-linked lists, meaning they are very efficient to + add and remove elements from the front of the list. +

+

+ Counting the length of a list or getting elements from other positions in the + list is expensive and rarely done. It is rare to write algorithms that index + into sequences in Gleam, but but when they are written a list is not the right + choice of data structure. +

diff --git a/src/content/chapter0_basics/lesson15_constants/code.gleam b/src/content/chapter0_basics/lesson15_constants/code.gleam new file mode 100644 index 0000000..aed6fb0 --- /dev/null +++ b/src/content/chapter0_basics/lesson15_constants/code.gleam @@ -0,0 +1,13 @@ +import gleam/io + +const ints: List(Int) = [1, 2, 3] + +const floats = [1.0, 2.0, 3.0] + +pub fn main() { + io.debug(ints) + io.debug(ints == [1, 2, 3]) + + io.debug(floats) + io.debug(floats == [1.0, 2.0, 3.0]) +} diff --git a/src/content/chapter0_basics/lesson15_constants/text.html b/src/content/chapter0_basics/lesson15_constants/text.html new file mode 100644 index 0000000..f610dcd --- /dev/null +++ b/src/content/chapter0_basics/lesson15_constants/text.html @@ -0,0 +1,18 @@ +

+ As well as let assignments Gleam also has constants, which are defined at the + top level of a module. +

+

+ Constants must be literal values, functions cannot be used in their + definitions. +

+

+ Constants may be useful for values that are used throughout your program, + permitting them to be named and to ensure there are no differences in the + definition between each use. +

+

+ Using a constant may be more efficient than creating the same value in + multiple functions, though the exact performance characteristics will depend + on the runtime and whether compiling to Erlang or JavaScript. +

diff --git a/src/content/chapter5_advance_features/lesson00_todo/code.gleam b/src/content/chapter5_advance_features/lesson00_todo/code.gleam new file mode 100644 index 0000000..d5abe8f --- /dev/null +++ b/src/content/chapter5_advance_features/lesson00_todo/code.gleam @@ -0,0 +1,7 @@ +pub fn main() { + todo as "I haven't written this code yet!" +} + +pub fn todo_without_reason() { + todo +} diff --git a/src/content/chapter5_advance_features/lesson00_todo/text.html b/src/content/chapter5_advance_features/lesson00_todo/text.html new file mode 100644 index 0000000..4a2c433 --- /dev/null +++ b/src/content/chapter5_advance_features/lesson00_todo/text.html @@ -0,0 +1,14 @@ +

+ The todo keyword is used to specify that some code is not yet + implemented. +

+

+ The as "some string" is optional, though you may wish to include + the message if you have more than one code block marked as + todo in your code. +

+

+ When used the Gleam compiler will print a warning to remind you the code is + unfinished, and if the code is run then the program will crash with the given + message. +

diff --git a/src/content/chapter5_advance_features/lesson01_panic/code.gleam b/src/content/chapter5_advance_features/lesson01_panic/code.gleam new file mode 100644 index 0000000..fce9d66 --- /dev/null +++ b/src/content/chapter5_advance_features/lesson01_panic/code.gleam @@ -0,0 +1,15 @@ +import gleam/io + +pub fn main() { + print_score(10) + print_score(100_000) + print_score(-1) +} + +pub fn print_score(score: Int) { + case score { + score if score > 1000 -> io.println("High score!") + score if score > 0 -> io.println("Still working on it") + _ -> panic as "Scores should never be negative!" + } +} diff --git a/src/content/chapter5_advance_features/lesson01_panic/text.html b/src/content/chapter5_advance_features/lesson01_panic/text.html new file mode 100644 index 0000000..843a65b --- /dev/null +++ b/src/content/chapter5_advance_features/lesson01_panic/text.html @@ -0,0 +1,11 @@ +

+ The panic keyword is similar to todo keyword, but it + is used to crash the program when the program has reached a point that should + never be reached. +

+

+ This keyword should almost never be used! It may be useful in initial + prototypes and scripts, but its use in a library or production application is + a sign that the design could be improved. With well designed types the type + system can typically be used to make these invalid states unrepresentable. +

-- cgit v1.2.3