From 3edd3a3de2500e04fc4e57c709d4667ce4689994 Mon Sep 17 00:00:00 2001
From: Louis Pilfold
As well as module-level named functions, Gleam has anonymous function
- literals.
-
- Anonymous functions can be used interchangeably with named functions.
+ literals, written with the Anonymous functions can be used interchangeably with named functions.
- Patterns in case expressions can also assign variables.
+ The case expression is the most common kind of flow control in Gleam code. It
+ is similar to
- When a variable name is used in a pattern the value that is matched against is
- assigned to that name, and can be used in the body of that clause.
+ It allows the programmer to say "if the data has this shape then run this
+ code", a process called called pattern matching.
+
+ Gleam performs exhaustiveness checking to ensure that the patterns in
+ a case expression cover all possible values. With this you can have confidence
+ that your logic is up-to-date for the design of the data you are working with.
+
+ Try commenting out patterns or adding new redundant ones, and see what
+ problems the compiler reports.
- The case expression is the most common kind of flow control in Gleam code. It
- is similar to
- It allows the programmer to say "if the data has this shape then run this
- code", a process called called pattern matching.
-
- Gleam performs exhaustiveness checking to ensure that the patterns in
- a case expression cover all possible values. With this you can have confidence
- that your logic is up-to-date for the design of the data you are working with.
-
- Try commenting out patterns or adding new redundant ones, and see what
- problems the compiler reports.
+ When a variable name is used in a pattern the value that is matched against is
+ assigned to that name, and can be used in the body of that clause.
When pattern matching on strings the fn() { ... }
syntax.
switch
in some other languages, but more powerful
+ than most.
switch
in some other languages, but more powerful
- than most.
+ Patterns in case expressions can also assign variables.
<>
operator can be
used to match on strings with a specific prefix.
-
The pattern "hello " <> name
matches any string that starts with
- "hello "
and asigns the rest of the string to the variable
+ "hello "
and assigns the rest of the string to the variable
name
.
|
operator. If any of the patterns match then the clause matches.
-
- When matching on multiple subjects there must be the same number of patterns
- as there are subjects. Try removing one of the _,
sub-patterns to
- see the compile time error that is returned.
-
If a pattern defines a variable then all of the alternative patterns for that clause must also define a variable with the same name and same type. diff --git a/src/content/chapter3_data_types/lesson01_custom_types/text.html b/src/content/chapter3_data_types/lesson01_custom_types/text.html index dad6d12..3a95f9f 100644 --- a/src/content/chapter3_data_types/lesson01_custom_types/text.html +++ b/src/content/chapter3_data_types/lesson01_custom_types/text.html @@ -3,7 +3,7 @@ but custom types allow the creation of entirely new types.
- A custom type is defined with the type
keyword followed by a
- constructor for each variant of the type.
+ A custom type is defined with the type
keyword followed by the
+ name of the type and a constructor for each variant of the type.
Custom type variants can be pattern matched on using a case expression.
diff --git a/src/content/chapter3_data_types/lesson05_nil/text.html b/src/content/chapter3_data_types/lesson05_nil/text.html index 3416643..723312b 100644 --- a/src/content/chapter3_data_types/lesson05_nil/text.html +++ b/src/content/chapter3_data_types/lesson05_nil/text.html @@ -1,12 +1,12 @@
Nil
is Gleam's unit type. It is a value that is returned by
- functions that have nothing else to return, as all functions much return
+ 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
+ the value Nil
. If it is some other type then the value is not
Nil
.
diff --git a/src/content/chapter3_data_types/lesson06_results/text.html b/src/content/chapter3_data_types/lesson06_results/text.html index fe0f7ba..2e1b16b 100644 --- a/src/content/chapter3_data_types/lesson06_results/text.html +++ b/src/content/chapter3_data_types/lesson06_results/text.html @@ -28,7 +28,7 @@ No nasty surprises with unexpected exceptions!
- Result value can be handled by pattern matching with a
+ 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
diff --git a/src/content/chapter5_advanced_features/lesson03_panic/text.html b/src/content/chapter5_advanced_features/lesson03_panic/text.html
index 843a65b..c54c217 100644
--- a/src/content/chapter5_advanced_features/lesson03_panic/text.html
+++ b/src/content/chapter5_advanced_features/lesson03_panic/text.html
@@ -1,7 +1,7 @@
- The panic
keyword is similar to todo
keyword, but it
- is used to crash the program when the program has reached a point that should
- never be reached.
+ The panic
keyword is similar to the todo
keyword,
+ but it is used to crash the program when the program has reached a point that
+ should never be reached.
This keyword should almost never be used! It may be useful in initial diff --git a/src/content/chapter5_advanced_features/lesson04_externals/text.html b/src/content/chapter5_advanced_features/lesson04_externals/text.html index 81202d6..8815fa7 100644 --- a/src/content/chapter5_advanced_features/lesson04_externals/text.html +++ b/src/content/chapter5_advanced_features/lesson04_externals/text.html @@ -5,7 +5,7 @@ import and use this non-Gleam code.
- An external type is a one that has no constructors. Gleam doesn't know what + An external type is one that has no constructors. Gleam doesn't know what shape it has or how to create one, it only knows that it exists.
diff --git a/src/content/chapter5_advanced_features/lesson05_multi_target_externals/text.html b/src/content/chapter5_advanced_features/lesson05_multi_target_externals/text.html index dc10a19..6e02d36 100644 --- a/src/content/chapter5_advanced_features/lesson05_multi_target_externals/text.html +++ b/src/content/chapter5_advanced_features/lesson05_multi_target_externals/text.html @@ -7,7 +7,7 @@ target then the compiler will return an error.
- You should try to implement functons for all targets, but this isn't always + You should try to implement functions for all targets, but this isn't always possible due to incompatibilities in how IO and concurreny works in Erlang and JavaScript. With Erlang concurrent IO is handled transparently by the runtime, while in JavaScript concurrent IO requires the use of promises or callbacks. -- cgit v1.2.3