From b13ca3f54ee93d2a714073caec4aa4252e8b3f00 Mon Sep 17 00:00:00 2001 From: Louis Pilfold Date: Wed, 6 Mar 2024 13:58:37 +0000 Subject: Generic custom types --- .../lesson05_generic_custom_types/code.gleam | 10 ++++++ .../lesson05_generic_custom_types/en.html | 10 ++++++ .../chapter3_data_types/lesson05_nil/code.gleam | 11 ------- .../chapter3_data_types/lesson05_nil/en.html | 15 --------- .../chapter3_data_types/lesson06_nil/code.gleam | 11 +++++++ .../chapter3_data_types/lesson06_nil/en.html | 15 +++++++++ .../lesson06_results/code.gleam | 25 --------------- .../chapter3_data_types/lesson06_results/en.html | 37 ---------------------- .../lesson07_bit_arrays/code.gleam | 13 -------- .../lesson07_bit_arrays/en.html | 26 --------------- .../lesson07_results/code.gleam | 25 +++++++++++++++ .../chapter3_data_types/lesson07_results/en.html | 37 ++++++++++++++++++++++ .../lesson08_bit_arrays/code.gleam | 13 ++++++++ .../lesson08_bit_arrays/en.html | 26 +++++++++++++++ 14 files changed, 147 insertions(+), 127 deletions(-) create mode 100644 src/content/chapter3_data_types/lesson05_generic_custom_types/code.gleam create mode 100644 src/content/chapter3_data_types/lesson05_generic_custom_types/en.html delete mode 100644 src/content/chapter3_data_types/lesson05_nil/code.gleam delete mode 100644 src/content/chapter3_data_types/lesson05_nil/en.html create mode 100644 src/content/chapter3_data_types/lesson06_nil/code.gleam create mode 100644 src/content/chapter3_data_types/lesson06_nil/en.html delete mode 100644 src/content/chapter3_data_types/lesson06_results/code.gleam delete mode 100644 src/content/chapter3_data_types/lesson06_results/en.html delete mode 100644 src/content/chapter3_data_types/lesson07_bit_arrays/code.gleam delete mode 100644 src/content/chapter3_data_types/lesson07_bit_arrays/en.html create mode 100644 src/content/chapter3_data_types/lesson07_results/code.gleam create mode 100644 src/content/chapter3_data_types/lesson07_results/en.html create mode 100644 src/content/chapter3_data_types/lesson08_bit_arrays/code.gleam create mode 100644 src/content/chapter3_data_types/lesson08_bit_arrays/en.html (limited to 'src') 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 new file mode 100644 index 0000000..7e34e99 --- /dev/null +++ b/src/content/chapter3_data_types/lesson05_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/lesson05_generic_custom_types/en.html b/src/content/chapter3_data_types/lesson05_generic_custom_types/en.html new file mode 100644 index 0000000..bc48e63 --- /dev/null +++ b/src/content/chapter3_data_types/lesson05_generic_custom_types/en.html @@ -0,0 +1,10 @@ +

+ 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_nil/code.gleam b/src/content/chapter3_data_types/lesson05_nil/code.gleam deleted file mode 100644 index c28080b..0000000 --- a/src/content/chapter3_data_types/lesson05_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/lesson05_nil/en.html b/src/content/chapter3_data_types/lesson05_nil/en.html deleted file mode 100644 index 00a082e..0000000 --- a/src/content/chapter3_data_types/lesson05_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, that is 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/lesson06_nil/code.gleam b/src/content/chapter3_data_types/lesson06_nil/code.gleam new file mode 100644 index 0000000..c28080b --- /dev/null +++ b/src/content/chapter3_data_types/lesson06_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/lesson06_nil/en.html b/src/content/chapter3_data_types/lesson06_nil/en.html new file mode 100644 index 0000000..00a082e --- /dev/null +++ b/src/content/chapter3_data_types/lesson06_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, that is 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/lesson06_results/code.gleam b/src/content/chapter3_data_types/lesson06_results/code.gleam deleted file mode 100644 index ae00bba..0000000 --- a/src/content/chapter3_data_types/lesson06_results/code.gleam +++ /dev/null @@ -1,25 +0,0 @@ -import gleam/io -import gleam/int - -pub fn main() { - io.debug(buy_pastry(10)) - io.debug(buy_pastry(8)) - io.debug(buy_pastry(5)) - 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/lesson06_results/en.html b/src/content/chapter3_data_types/lesson06_results/en.html deleted file mode 100644 index 4e80208..0000000 --- a/src/content/chapter3_data_types/lesson06_results/en.html +++ /dev/null @@ -1,37 +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/lesson07_bit_arrays/code.gleam b/src/content/chapter3_data_types/lesson07_bit_arrays/code.gleam deleted file mode 100644 index dc772ca..0000000 --- a/src/content/chapter3_data_types/lesson07_bit_arrays/code.gleam +++ /dev/null @@ -1,13 +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>>) -} diff --git a/src/content/chapter3_data_types/lesson07_bit_arrays/en.html b/src/content/chapter3_data_types/lesson07_bit_arrays/en.html deleted file mode 100644 index cd6733a..0000000 --- a/src/content/chapter3_data_types/lesson07_bit_arrays/en.html +++ /dev/null @@ -1,26 +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. -

- -

- 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. -

diff --git a/src/content/chapter3_data_types/lesson07_results/code.gleam b/src/content/chapter3_data_types/lesson07_results/code.gleam new file mode 100644 index 0000000..ae00bba --- /dev/null +++ b/src/content/chapter3_data_types/lesson07_results/code.gleam @@ -0,0 +1,25 @@ +import gleam/io +import gleam/int + +pub fn main() { + io.debug(buy_pastry(10)) + io.debug(buy_pastry(8)) + io.debug(buy_pastry(5)) + 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 new file mode 100644 index 0000000..4e80208 --- /dev/null +++ b/src/content/chapter3_data_types/lesson07_results/en.html @@ -0,0 +1,37 @@ +

+ 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 new file mode 100644 index 0000000..dc772ca --- /dev/null +++ b/src/content/chapter3_data_types/lesson08_bit_arrays/code.gleam @@ -0,0 +1,13 @@ +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>>) +} diff --git a/src/content/chapter3_data_types/lesson08_bit_arrays/en.html b/src/content/chapter3_data_types/lesson08_bit_arrays/en.html new file mode 100644 index 0000000..cd6733a --- /dev/null +++ b/src/content/chapter3_data_types/lesson08_bit_arrays/en.html @@ -0,0 +1,26 @@ +

+ 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. +

+ +

+ 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. +

-- cgit v1.2.3