aboutsummaryrefslogtreecommitdiff
path: root/src/content/chapter5_advanced_features/lesson01_use/code.gleam
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2024-03-26 13:57:31 +0000
committerLouis Pilfold <louis@lpil.uk>2024-03-26 13:57:31 +0000
commitc2dcbe0f25a3e8bd60a4ccf377bbdd47f4794871 (patch)
tree18862644ee2dada0a926392b11e606bb0d2262f7 /src/content/chapter5_advanced_features/lesson01_use/code.gleam
parentccf75d2c362ac8e4dcd12c781f6e1eafd0064813 (diff)
downloadtour-c2dcbe0f25a3e8bd60a4ccf377bbdd47f4794871.tar.gz
tour-c2dcbe0f25a3e8bd60a4ccf377bbdd47f4794871.zip
Document opaque types
Closes https://github.com/gleam-lang/language-tour/issues/63
Diffstat (limited to 'src/content/chapter5_advanced_features/lesson01_use/code.gleam')
-rw-r--r--src/content/chapter5_advanced_features/lesson01_use/code.gleam38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/content/chapter5_advanced_features/lesson01_use/code.gleam b/src/content/chapter5_advanced_features/lesson01_use/code.gleam
new file mode 100644
index 0000000..e7b435b
--- /dev/null
+++ b/src/content/chapter5_advanced_features/lesson01_use/code.gleam
@@ -0,0 +1,38 @@
+import gleam/io
+import gleam/result
+
+pub fn main() {
+ io.debug(without_use())
+ io.debug(with_use())
+}
+
+pub fn without_use() {
+ result.try(get_username(), fn(username) {
+ result.try(get_password(), fn(password) {
+ result.map(log_in(username, password), fn(greeting) {
+ greeting <> ", " <> username
+ })
+ })
+ })
+}
+
+pub fn with_use() {
+ use username <- result.try(get_username())
+ use password <- result.try(get_password())
+ use greeting <- result.map(log_in(username, password))
+ greeting <> ", " <> username
+}
+
+// Here are some pretend functions for this example:
+
+fn get_username() {
+ Ok("alice")
+}
+
+fn get_password() {
+ Ok("hunter2")
+}
+
+fn log_in(_username: String, _password: String) {
+ Ok("Welcome")
+}