diff options
Diffstat (limited to 'lessons/src/lesson029_pattern_aliases')
-rw-r--r-- | lessons/src/lesson029_pattern_aliases/code.gleam | 15 | ||||
-rw-r--r-- | lessons/src/lesson029_pattern_aliases/text.html | 7 |
2 files changed, 22 insertions, 0 deletions
diff --git a/lessons/src/lesson029_pattern_aliases/code.gleam b/lessons/src/lesson029_pattern_aliases/code.gleam new file mode 100644 index 0000000..ee40a26 --- /dev/null +++ b/lessons/src/lesson029_pattern_aliases/code.gleam @@ -0,0 +1,15 @@ +import gleam/io + +pub fn main() { + io.debug(get_first_non_empty([[], [1, 2, 3], [4, 5]])) + io.debug(get_first_non_empty([[1, 2], [3, 4, 5], []])) + io.debug(get_first_non_empty([[], [], []])) +} + +fn get_first_non_empty(lists: List(List(t))) -> List(t) { + case lists { + [[_, ..] as first, ..] -> first + [_, ..rest] -> get_first_non_empty(rest) + [] -> [] + } +} diff --git a/lessons/src/lesson029_pattern_aliases/text.html b/lessons/src/lesson029_pattern_aliases/text.html new file mode 100644 index 0000000..5aa8a81 --- /dev/null +++ b/lessons/src/lesson029_pattern_aliases/text.html @@ -0,0 +1,7 @@ +<p> + The <code>as</code> operator can be used to assign sub patterns to variables. +</p> +<p> + For example, the pattern <code>[_, ..] as it</code> will match any non-empty + list and assign that list to the variable <code>it</code>. +</p> |