From b3a992440c672afd7bc96b336f5c2ece6231166f Mon Sep 17 00:00:00 2001 From: Louis Pilfold Date: Wed, 20 Dec 2023 19:12:38 +0000 Subject: Custom types --- lessons/src/lesson030_tuples/code.gleam | 10 ++++++++++ lessons/src/lesson030_tuples/text.html | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 lessons/src/lesson030_tuples/code.gleam create mode 100644 lessons/src/lesson030_tuples/text.html (limited to 'lessons/src/lesson030_tuples') diff --git a/lessons/src/lesson030_tuples/code.gleam b/lessons/src/lesson030_tuples/code.gleam new file mode 100644 index 0000000..d5c6313 --- /dev/null +++ b/lessons/src/lesson030_tuples/code.gleam @@ -0,0 +1,10 @@ +import gleam/io + +pub fn main() { + let triple = #(1, 2.2, "three") + io.debug(triple) + + let #(a, _, _) = triple + io.debug(a) + io.debug(triple.1) +} diff --git a/lessons/src/lesson030_tuples/text.html b/lessons/src/lesson030_tuples/text.html new file mode 100644 index 0000000..f121a9d --- /dev/null +++ b/lessons/src/lesson030_tuples/text.html @@ -0,0 +1,20 @@ +

+ Lists are good for when we want a collection of one type, but sometimes we + want to combine multiple values of different types. In this case tuples are a + quick and convenient option. +

+

+ The tuple access syntax can be used to get elements from a tuple without + pattern matching. some_tuple.0 gets the first element, + some_tuple.1 gets the second element, etc. +

+

+ Tuples are generic types, they have type parameters for the types they + contain. #(1, "Hi!") has the type #(Int, String), + and #(1.4, 10, 48) has the type #(Float, Int, Int). +

+

+ Tuples are most commonly used to return 2 or 3 values from a function. Other + times it is often is clearer to use a custom type, which we will + cover next. +

-- cgit v1.2.3