From f6addcc0ed9ed41c496d839b46ae98473d49cbc9 Mon Sep 17 00:00:00 2001 From: xxKeefer Date: Wed, 24 Apr 2024 20:54:18 +1000 Subject: refactor: :truck: make space for new record destructuring chapter --- .../lesson04_record_updates/code.gleam | 15 -------- .../lesson04_record_updates/en.html | 8 ---- .../lesson05_generic_custom_types/code.gleam | 10 ----- .../lesson05_generic_custom_types/en.html | 12 ------ .../lesson05_record_updates/code.gleam | 15 ++++++++ .../lesson05_record_updates/en.html | 8 ++++ .../lesson06_generic_custom_types/code.gleam | 10 +++++ .../lesson06_generic_custom_types/en.html | 12 ++++++ .../chapter3_data_types/lesson06_nil/code.gleam | 11 ------ .../chapter3_data_types/lesson06_nil/en.html | 15 -------- .../chapter3_data_types/lesson07_nil/code.gleam | 11 ++++++ .../chapter3_data_types/lesson07_nil/en.html | 15 ++++++++ .../lesson07_results/code.gleam | 25 ------------ .../chapter3_data_types/lesson07_results/en.html | 39 ------------------- .../lesson08_bit_arrays/code.gleam | 18 --------- .../lesson08_bit_arrays/en.html | 44 ---------------------- .../lesson08_results/code.gleam | 25 ++++++++++++ .../chapter3_data_types/lesson08_results/en.html | 39 +++++++++++++++++++ .../lesson09_bit_arrays/code.gleam | 18 +++++++++ .../lesson09_bit_arrays/en.html | 44 ++++++++++++++++++++++ 20 files changed, 197 insertions(+), 197 deletions(-) delete mode 100644 src/content/chapter3_data_types/lesson04_record_updates/code.gleam delete mode 100644 src/content/chapter3_data_types/lesson04_record_updates/en.html delete mode 100644 src/content/chapter3_data_types/lesson05_generic_custom_types/code.gleam delete mode 100644 src/content/chapter3_data_types/lesson05_generic_custom_types/en.html create mode 100644 src/content/chapter3_data_types/lesson05_record_updates/code.gleam create mode 100644 src/content/chapter3_data_types/lesson05_record_updates/en.html create mode 100644 src/content/chapter3_data_types/lesson06_generic_custom_types/code.gleam create mode 100644 src/content/chapter3_data_types/lesson06_generic_custom_types/en.html delete mode 100644 src/content/chapter3_data_types/lesson06_nil/code.gleam delete mode 100644 src/content/chapter3_data_types/lesson06_nil/en.html create mode 100644 src/content/chapter3_data_types/lesson07_nil/code.gleam create mode 100644 src/content/chapter3_data_types/lesson07_nil/en.html delete mode 100644 src/content/chapter3_data_types/lesson07_results/code.gleam delete mode 100644 src/content/chapter3_data_types/lesson07_results/en.html delete mode 100644 src/content/chapter3_data_types/lesson08_bit_arrays/code.gleam delete mode 100644 src/content/chapter3_data_types/lesson08_bit_arrays/en.html create mode 100644 src/content/chapter3_data_types/lesson08_results/code.gleam create mode 100644 src/content/chapter3_data_types/lesson08_results/en.html create mode 100644 src/content/chapter3_data_types/lesson09_bit_arrays/code.gleam create mode 100644 src/content/chapter3_data_types/lesson09_bit_arrays/en.html diff --git a/src/content/chapter3_data_types/lesson04_record_updates/code.gleam b/src/content/chapter3_data_types/lesson04_record_updates/code.gleam deleted file mode 100644 index ed7b45b..0000000 --- a/src/content/chapter3_data_types/lesson04_record_updates/code.gleam +++ /dev/null @@ -1,15 +0,0 @@ -import gleam/io - -pub type SchoolPerson { - Teacher(name: String, subject: String, floor: Int, room: Int) -} - -pub fn main() { - let teacher1 = Teacher(name: "Mr Dodd", subject: "ICT", floor: 2, room: 2) - - // Use the update syntax - let teacher2 = Teacher(..teacher1, subject: "PE", room: 6) - - io.debug(teacher1) - io.debug(teacher2) -} diff --git a/src/content/chapter3_data_types/lesson04_record_updates/en.html b/src/content/chapter3_data_types/lesson04_record_updates/en.html deleted file mode 100644 index 4a5cd3a..0000000 --- a/src/content/chapter3_data_types/lesson04_record_updates/en.html +++ /dev/null @@ -1,8 +0,0 @@ -

- The record update syntax can be used to create a new record from an existing - one of the same type, but with some fields changed. -

-

- Gleam is an immutable language, so using the record update syntax does not - mutate or otherwise change the original record. -

diff --git a/src/content/chapter3_data_types/lesson05_generic_custom_types/code.gleam b/src/content/chapter3_data_types/lesson05_generic_custom_types/code.gleam deleted file mode 100644 index 7e34e99..0000000 --- a/src/content/chapter3_data_types/lesson05_generic_custom_types/code.gleam +++ /dev/null @@ -1,10 +0,0 @@ -pub type Option(inner) { - Some(inner) - None -} - -// An option of string -pub const name: Option(String) = Some("Annah") - -// An option of int -pub const level: Option(Int) = Some(10) diff --git a/src/content/chapter3_data_types/lesson05_generic_custom_types/en.html b/src/content/chapter3_data_types/lesson05_generic_custom_types/en.html deleted file mode 100644 index 3d003d7..0000000 --- a/src/content/chapter3_data_types/lesson05_generic_custom_types/en.html +++ /dev/null @@ -1,12 +0,0 @@ -

- Like functions, custom types can also be generic, taking contained types as - parameters. -

-

- Here a generic Option type is defined, which is used to represent - a value that is either present or absent. This type is quite useful! The - - gleam/option - - module defines it so you can use it in your Gleam projects. -

diff --git a/src/content/chapter3_data_types/lesson05_record_updates/code.gleam b/src/content/chapter3_data_types/lesson05_record_updates/code.gleam new file mode 100644 index 0000000..ed7b45b --- /dev/null +++ b/src/content/chapter3_data_types/lesson05_record_updates/code.gleam @@ -0,0 +1,15 @@ +import gleam/io + +pub type SchoolPerson { + Teacher(name: String, subject: String, floor: Int, room: Int) +} + +pub fn main() { + let teacher1 = Teacher(name: "Mr Dodd", subject: "ICT", floor: 2, room: 2) + + // Use the update syntax + let teacher2 = Teacher(..teacher1, subject: "PE", room: 6) + + io.debug(teacher1) + io.debug(teacher2) +} diff --git a/src/content/chapter3_data_types/lesson05_record_updates/en.html b/src/content/chapter3_data_types/lesson05_record_updates/en.html new file mode 100644 index 0000000..4a5cd3a --- /dev/null +++ b/src/content/chapter3_data_types/lesson05_record_updates/en.html @@ -0,0 +1,8 @@ +

+ The record update syntax can be used to create a new record from an existing + one of the same type, but with some fields changed. +

+

+ Gleam is an immutable language, so using the record update syntax does not + mutate or otherwise change the original record. +

diff --git a/src/content/chapter3_data_types/lesson06_generic_custom_types/code.gleam b/src/content/chapter3_data_types/lesson06_generic_custom_types/code.gleam new file mode 100644 index 0000000..7e34e99 --- /dev/null +++ b/src/content/chapter3_data_types/lesson06_generic_custom_types/code.gleam @@ -0,0 +1,10 @@ +pub type Option(inner) { + Some(inner) + None +} + +// An option of string +pub const name: Option(String) = Some("Annah") + +// An option of int +pub const level: Option(Int) = Some(10) diff --git a/src/content/chapter3_data_types/lesson06_generic_custom_types/en.html b/src/content/chapter3_data_types/lesson06_generic_custom_types/en.html new file mode 100644 index 0000000..3d003d7 --- /dev/null +++ b/src/content/chapter3_data_types/lesson06_generic_custom_types/en.html @@ -0,0 +1,12 @@ +

+ Like functions, custom types can also be generic, taking contained types as + parameters. +

+

+ Here a generic Option type is defined, which is used to represent + a value that is either present or absent. This type is quite useful! The + + gleam/option + + module defines it so you can use it in your Gleam projects. +

diff --git a/src/content/chapter3_data_types/lesson06_nil/code.gleam b/src/content/chapter3_data_types/lesson06_nil/code.gleam deleted file mode 100644 index c28080b..0000000 --- a/src/content/chapter3_data_types/lesson06_nil/code.gleam +++ /dev/null @@ -1,11 +0,0 @@ -import gleam/io - -pub fn main() { - let x = Nil - io.debug(x) - - // let y: List(String) = Nil - - let result = io.println("Hello!") - io.debug(result == Nil) -} diff --git a/src/content/chapter3_data_types/lesson06_nil/en.html b/src/content/chapter3_data_types/lesson06_nil/en.html deleted file mode 100644 index 9749180..0000000 --- a/src/content/chapter3_data_types/lesson06_nil/en.html +++ /dev/null @@ -1,15 +0,0 @@ -

- Nil is Gleam's unit type. It is a value that is returned by - functions that have nothing else to return, as all functions must return - something. -

-

- Nil is not a valid value of any other types. Therefore, values in - Gleam are not nullable. If the type of a value is Nil then it is - the value Nil. If it is some other type then the value is not - Nil. -

-

- Uncomment the line that assigns Nil to a variable with an - incompatible type annotation to see the compile time error it produces. -

diff --git a/src/content/chapter3_data_types/lesson07_nil/code.gleam b/src/content/chapter3_data_types/lesson07_nil/code.gleam new file mode 100644 index 0000000..c28080b --- /dev/null +++ b/src/content/chapter3_data_types/lesson07_nil/code.gleam @@ -0,0 +1,11 @@ +import gleam/io + +pub fn main() { + let x = Nil + io.debug(x) + + // let y: List(String) = Nil + + let result = io.println("Hello!") + io.debug(result == Nil) +} diff --git a/src/content/chapter3_data_types/lesson07_nil/en.html b/src/content/chapter3_data_types/lesson07_nil/en.html new file mode 100644 index 0000000..9749180 --- /dev/null +++ b/src/content/chapter3_data_types/lesson07_nil/en.html @@ -0,0 +1,15 @@ +

+ Nil is Gleam's unit type. It is a value that is returned by + functions that have nothing else to return, as all functions must return + something. +

+

+ Nil is not a valid value of any other types. Therefore, values in + Gleam are not nullable. If the type of a value is Nil then it is + the value Nil. If it is some other type then the value is not + Nil. +

+

+ Uncomment the line that assigns Nil to a variable with an + incompatible type annotation to see the compile time error it produces. +

diff --git a/src/content/chapter3_data_types/lesson07_results/code.gleam b/src/content/chapter3_data_types/lesson07_results/code.gleam deleted file mode 100644 index 67a2559..0000000 --- a/src/content/chapter3_data_types/lesson07_results/code.gleam +++ /dev/null @@ -1,25 +0,0 @@ -import gleam/int -import gleam/io - -pub fn main() { - let _ = io.debug(buy_pastry(10)) - let _ = io.debug(buy_pastry(8)) - let _ = io.debug(buy_pastry(5)) - let _ = io.debug(buy_pastry(3)) -} - -pub type PurchaseError { - NotEnoughMoney(required: Int) - NotLuckyEnough -} - -fn buy_pastry(money: Int) -> Result(Int, PurchaseError) { - case money >= 5 { - True -> - case int.random(4) == 0 { - True -> Error(NotLuckyEnough) - False -> Ok(money - 5) - } - False -> Error(NotEnoughMoney(required: 5)) - } -} diff --git a/src/content/chapter3_data_types/lesson07_results/en.html b/src/content/chapter3_data_types/lesson07_results/en.html deleted file mode 100644 index c43a0b7..0000000 --- a/src/content/chapter3_data_types/lesson07_results/en.html +++ /dev/null @@ -1,39 +0,0 @@ -

- Gleam doesn't use exceptions, instead computations that can either succeed or - fail return a value of the built-in Result(value, error) type. It - has two variants: -

- -

- The type is generic with two type parameters, one for the success value and - one for the error. With these the result can hold any type for success and - failure. -

-

- Commonly a Gleam program or library will define a custom type with a variant - for each possible problem that can arise, along with any error information - that would be useful to the programmer. -

-

- This is advantageous over exceptions as you can immediately see what if any - errors a function can return, and the compiler will ensure they are handled. - No nasty surprises with unexpected exceptions! -

-

- A result value can be handled by pattern matching with a - case expression, but given how frequently results are returned - this can become unwieldy. Gleam code commonly uses the - - gleam/result - - standard library module and use expressions when working with results, - both of which will be covered in later chapters. -

diff --git a/src/content/chapter3_data_types/lesson08_bit_arrays/code.gleam b/src/content/chapter3_data_types/lesson08_bit_arrays/code.gleam deleted file mode 100644 index 52d7b8c..0000000 --- a/src/content/chapter3_data_types/lesson08_bit_arrays/code.gleam +++ /dev/null @@ -1,18 +0,0 @@ -import gleam/io - -pub fn main() { - // 8 bit int. In binary: 00000011 - io.debug(<<3>>) - io.debug(<<3>> == <<3:size(8)>>) - - // 16 bit int. In binary: 0001100000000011 - io.debug(<<6147:size(16)>>) - - // A bit array of UTF8 data - io.debug(<<"Hello, Joe!":utf8>>) - - // Concatenation - let first = <<4>> - let second = <<2>> - io.debug(<>) -} diff --git a/src/content/chapter3_data_types/lesson08_bit_arrays/en.html b/src/content/chapter3_data_types/lesson08_bit_arrays/en.html deleted file mode 100644 index 8b92335..0000000 --- a/src/content/chapter3_data_types/lesson08_bit_arrays/en.html +++ /dev/null @@ -1,44 +0,0 @@ -

- Bit arrays represent a sequence of 1s and 0s, and are a convenient syntax for - constructing and manipulating binary data. -

-

- Each segment of a bit array can be given options to specify the representation - used for that segment. -

- -

- Multiple options can be given to a segment by separating each with a dash: - x:unsigned-little-size(2). -

-

- Bit arrays have limited support when compiling to JavaScript, not all options - can be used. Full bit array support will be implemented in the future. -

-

- For more information on bit arrays see the - Erlang bit syntax documentation. -

diff --git a/src/content/chapter3_data_types/lesson08_results/code.gleam b/src/content/chapter3_data_types/lesson08_results/code.gleam new file mode 100644 index 0000000..67a2559 --- /dev/null +++ b/src/content/chapter3_data_types/lesson08_results/code.gleam @@ -0,0 +1,25 @@ +import gleam/int +import gleam/io + +pub fn main() { + let _ = io.debug(buy_pastry(10)) + let _ = io.debug(buy_pastry(8)) + let _ = io.debug(buy_pastry(5)) + let _ = io.debug(buy_pastry(3)) +} + +pub type PurchaseError { + NotEnoughMoney(required: Int) + NotLuckyEnough +} + +fn buy_pastry(money: Int) -> Result(Int, PurchaseError) { + case money >= 5 { + True -> + case int.random(4) == 0 { + True -> Error(NotLuckyEnough) + False -> Ok(money - 5) + } + False -> Error(NotEnoughMoney(required: 5)) + } +} diff --git a/src/content/chapter3_data_types/lesson08_results/en.html b/src/content/chapter3_data_types/lesson08_results/en.html new file mode 100644 index 0000000..c43a0b7 --- /dev/null +++ b/src/content/chapter3_data_types/lesson08_results/en.html @@ -0,0 +1,39 @@ +

+ Gleam doesn't use exceptions, instead computations that can either succeed or + fail return a value of the built-in Result(value, error) type. It + has two variants: +

+ +

+ The type is generic with two type parameters, one for the success value and + one for the error. With these the result can hold any type for success and + failure. +

+

+ Commonly a Gleam program or library will define a custom type with a variant + for each possible problem that can arise, along with any error information + that would be useful to the programmer. +

+

+ This is advantageous over exceptions as you can immediately see what if any + errors a function can return, and the compiler will ensure they are handled. + No nasty surprises with unexpected exceptions! +

+

+ A result value can be handled by pattern matching with a + case expression, but given how frequently results are returned + this can become unwieldy. Gleam code commonly uses the + + gleam/result + + standard library module and use expressions when working with results, + both of which will be covered in later chapters. +

diff --git a/src/content/chapter3_data_types/lesson09_bit_arrays/code.gleam b/src/content/chapter3_data_types/lesson09_bit_arrays/code.gleam new file mode 100644 index 0000000..52d7b8c --- /dev/null +++ b/src/content/chapter3_data_types/lesson09_bit_arrays/code.gleam @@ -0,0 +1,18 @@ +import gleam/io + +pub fn main() { + // 8 bit int. In binary: 00000011 + io.debug(<<3>>) + io.debug(<<3>> == <<3:size(8)>>) + + // 16 bit int. In binary: 0001100000000011 + io.debug(<<6147:size(16)>>) + + // A bit array of UTF8 data + io.debug(<<"Hello, Joe!":utf8>>) + + // Concatenation + let first = <<4>> + let second = <<2>> + io.debug(<>) +} diff --git a/src/content/chapter3_data_types/lesson09_bit_arrays/en.html b/src/content/chapter3_data_types/lesson09_bit_arrays/en.html new file mode 100644 index 0000000..8b92335 --- /dev/null +++ b/src/content/chapter3_data_types/lesson09_bit_arrays/en.html @@ -0,0 +1,44 @@ +

+ Bit arrays represent a sequence of 1s and 0s, and are a convenient syntax for + constructing and manipulating binary data. +

+

+ Each segment of a bit array can be given options to specify the representation + used for that segment. +

+
    +
  • size: the size of the segment in bits.
  • +
  • + unit: the number of bits that the size value is a + multiple of. +
  • +
  • bits: a nested bit array of any size.
  • +
  • bytes: a nested byte-aligned bit array.
  • +
  • float: a 64 bits floating point number.
  • +
  • int: an int with a default size of 8 bits.
  • +
  • big: big endian.
  • +
  • little: little endian.
  • +
  • native: the endianness of the processor.
  • +
  • utf8: utf8 encoded text.
  • +
  • utf16: utf16 encoded text.
  • +
  • utf32: utf32 encoded text.
  • +
  • utf8_codepoint: a utf8 codepoint.
  • +
  • utf16_codepoint: a utf16 codepoint.
  • +
  • utf32_codepoint: a utf32 codepoint.
  • +
  • signed: a signed number.
  • +
  • unsigned: an unsigned number.
  • +
+

+ Multiple options can be given to a segment by separating each with a dash: + x:unsigned-little-size(2). +

+

+ Bit arrays have limited support when compiling to JavaScript, not all options + can be used. Full bit array support will be implemented in the future. +

+

+ For more information on bit arrays see the + Erlang bit syntax documentation. +

-- cgit v1.2.3