aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxxKeefer <xxkeefer.code@gmail.com>2024-04-24 20:54:52 +1000
committerLouis Pilfold <louis@lpil.uk>2024-06-12 14:06:18 +0100
commitec4c08571300e167dec2df26f191e6f8d09433c5 (patch)
treee4d03d1cdd15448aff42b014900bf5894b6b6162
parentf6addcc0ed9ed41c496d839b46ae98473d49cbc9 (diff)
downloadtour-ec4c08571300e167dec2df26f191e6f8d09433c5.tar.gz
tour-ec4c08571300e167dec2df26f191e6f8d09433c5.zip
feat: :sparkles: add examples and code for record destructuring
#56
-rw-r--r--src/content/chapter3_data_types/lesson04_record_destructuring/code.gleam25
-rw-r--r--src/content/chapter3_data_types/lesson04_record_destructuring/en.html12
2 files changed, 37 insertions, 0 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
new file mode 100644
index 0000000..2b99576
--- /dev/null
+++ b/src/content/chapter3_data_types/lesson04_record_destructuring/code.gleam
@@ -0,0 +1,25 @@
+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
new file mode 100644
index 0000000..6774fc6
--- /dev/null
+++ b/src/content/chapter3_data_types/lesson04_record_destructuring/en.html
@@ -0,0 +1,12 @@
+<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>