diff options
author | xxKeefer <xxkeefer.code@gmail.com> | 2024-04-25 23:40:09 +1000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2024-06-12 14:06:18 +0100 |
commit | a69f26d5271ff42ed1a8938644efcdc1d2306890 (patch) | |
tree | c939aa7db177c3ad6f6578c404398e1b56e94283 | |
parent | ec4c08571300e167dec2df26f191e6f8d09433c5 (diff) | |
download | tour-a69f26d5271ff42ed1a8938644efcdc1d2306890.tar.gz tour-a69f26d5271ff42ed1a8938644efcdc1d2306890.zip |
refactor: address feedback from code review
use correct terms, clean up code example, explain everything that need to be
#56
4 files changed, 27 insertions, 37 deletions
diff --git a/src/content/chapter3_data_types/lesson04_record_destructuring/code.gleam b/src/content/chapter3_data_types/lesson04_record_destructuring/code.gleam deleted file mode 100644 index 2b99576..0000000 --- a/src/content/chapter3_data_types/lesson04_record_destructuring/code.gleam +++ /dev/null @@ -1,25 +0,0 @@ -import gleam/io - -pub type Fish { - Starfish(name: String, favourite_color: String) - // Jellyfish(name: String, jiggly: Bool) - // Swordfish(name: String) -} - -pub fn main() { - let lucy = Starfish("Lucy", "Pink") - let cassie = Starfish("Cassie", "Orange") - // let james = Jellyfish("James", True) - // let sammy = Swordfish("Sammy") - - let Starfish(lucy_name, lucy_colour) = lucy - let Starfish(cassie_name, ..) = cassie - // let Jellyfish(_, is_jiggly) = james - // let Swordfish(sammy_name) = sammy - - let names = [lucy_name, cassie_name] - - io.debug(names) - io.debug(lucy_colour) - // io.debug(is_jiggly) -} diff --git a/src/content/chapter3_data_types/lesson04_record_destructuring/en.html b/src/content/chapter3_data_types/lesson04_record_destructuring/en.html deleted file mode 100644 index 6774fc6..0000000 --- a/src/content/chapter3_data_types/lesson04_record_destructuring/en.html +++ /dev/null @@ -1,12 +0,0 @@ -<p> - Record destructuring is syntax that allows the extraction of multiple field values - from a record into distinct variables in a concise and efficient way. -</p> -<p> - To destructure a record without the use more rigorous pattern matching, - all variants of the record must have the same arity and each field must have the same type. -</p> -<p> - It is possible to use underscore <code>_</code> or the spread syntax <code>..</code> to - discard fields that are not required. -</p> diff --git a/src/content/chapter3_data_types/lesson04_record_pattern_matching/code.gleam b/src/content/chapter3_data_types/lesson04_record_pattern_matching/code.gleam new file mode 100644 index 0000000..1c58b0d --- /dev/null +++ b/src/content/chapter3_data_types/lesson04_record_pattern_matching/code.gleam @@ -0,0 +1,15 @@ +import gleam/io + +pub type Fish { + Starfish(name: String, favourite_color: String) + Jellyfish(name: String, jiggly: Bool) +} + +pub fn main() { + let lucy = Starfish("Lucy", "Pink") + + case lucy { + Starfish(_, favourite_color) -> io.debug(favourite_color) + Jellyfish(name, ..) -> io.debug(name) + } +} diff --git a/src/content/chapter3_data_types/lesson04_record_pattern_matching/en.html b/src/content/chapter3_data_types/lesson04_record_pattern_matching/en.html new file mode 100644 index 0000000..2c0c2fa --- /dev/null +++ b/src/content/chapter3_data_types/lesson04_record_pattern_matching/en.html @@ -0,0 +1,12 @@ +<p> + It is possible to pattern match on a record, this allows for the extraction + of multiple field values from a record into distinct variables in a concise and efficient way. +</p> +<p> + the let key word can only match on single variant custom types. For types with more variants + a case statement must be used. +</p> +<p> + It is possible to use underscore <code>_</code> or the spread syntax <code>..</code> to + discard fields that are not required. +</p> |