aboutsummaryrefslogtreecommitdiff
path: root/lessons
diff options
context:
space:
mode:
Diffstat (limited to 'lessons')
-rw-r--r--lessons/src/lesson013_functions/code.gleam13
-rw-r--r--lessons/src/lesson013_functions/text.html14
-rw-r--r--lessons/src/lesson014_higher_order_functions/code.gleam18
-rw-r--r--lessons/src/lesson014_higher_order_functions/text.html12
-rw-r--r--lessons/src/lesson015_anonymous_functions/code.gleam14
-rw-r--r--lessons/src/lesson015_anonymous_functions/text.html7
-rw-r--r--lessons/src/lesson016_function_captures/code.gleam14
-rw-r--r--lessons/src/lesson016_function_captures/text.html11
8 files changed, 103 insertions, 0 deletions
diff --git a/lessons/src/lesson013_functions/code.gleam b/lessons/src/lesson013_functions/code.gleam
new file mode 100644
index 0000000..220e58d
--- /dev/null
+++ b/lessons/src/lesson013_functions/code.gleam
@@ -0,0 +1,13 @@
+import gleam/io
+
+pub fn main() {
+ io.debug(double(10))
+}
+
+fn double(a: Int) -> Int {
+ multiply(a, 2)
+}
+
+fn multiply(a: Int, b: Int) -> Int {
+ a * b
+}
diff --git a/lessons/src/lesson013_functions/text.html b/lessons/src/lesson013_functions/text.html
new file mode 100644
index 0000000..32d5bed
--- /dev/null
+++ b/lessons/src/lesson013_functions/text.html
@@ -0,0 +1,14 @@
+<p>
+ The <code>fn</code> keyword is used to define new functions.
+</p>
+<p>
+ The <code>double</code> and <code>multiply</code> functions are defined
+ without the <code>pub</code> keyword. This makes them <em>private</em>
+ functions, they can only be used within this module. If another module
+ attempted to use them it would result in a compiler error.
+</p>
+<p>
+ Like with assignments, type annotations are optional for function arguments
+ and return values. It is considered good practice to use type annotations for
+ functions, for clarity and to encourage intentional and thoughtful design.
+</p>
diff --git a/lessons/src/lesson014_higher_order_functions/code.gleam b/lessons/src/lesson014_higher_order_functions/code.gleam
new file mode 100644
index 0000000..f453975
--- /dev/null
+++ b/lessons/src/lesson014_higher_order_functions/code.gleam
@@ -0,0 +1,18 @@
+import gleam/io
+
+pub fn main() {
+ // Call a function with another function
+ io.debug(twice(1, add_one))
+
+ // Functions can be assigned to variables
+ let function = add_one
+ io.debug(function(100))
+}
+
+fn twice(a: Int, function: fn(Int) -> Int) -> Int {
+ function(function(a))
+}
+
+fn add_one(a: Int) -> Int {
+ a + 1
+}
diff --git a/lessons/src/lesson014_higher_order_functions/text.html b/lessons/src/lesson014_higher_order_functions/text.html
new file mode 100644
index 0000000..3343e4d
--- /dev/null
+++ b/lessons/src/lesson014_higher_order_functions/text.html
@@ -0,0 +1,12 @@
+<p>
+ In Gleam functions are values. They can be assigned to variables, passed to
+ other functions, and anything else you can do with values.
+</p>
+<p>
+ Here the function <code>add_one</code> is being passed as an argument to the
+ <code>twice</code> function.
+</p>
+<p>
+ Notice the <code>fn</code> keyword is also used to describe the type of the
+ function that <code>twice</code> takes as its second argument.
+</p>
diff --git a/lessons/src/lesson015_anonymous_functions/code.gleam b/lessons/src/lesson015_anonymous_functions/code.gleam
new file mode 100644
index 0000000..9f536d9
--- /dev/null
+++ b/lessons/src/lesson015_anonymous_functions/code.gleam
@@ -0,0 +1,14 @@
+import gleam/io
+
+pub fn main() {
+ // Assign an anonymous function to a variable
+ let add_one = fn(a) { a + 1 }
+ io.debug(twice(1, add_one))
+
+ // Pass an anonymous function as an argument
+ io.debug(twice(1, fn(a) { a * 2 }))
+}
+
+fn twice(a: Int, function: fn(Int) -> Int) -> Int {
+ function(function(a))
+}
diff --git a/lessons/src/lesson015_anonymous_functions/text.html b/lessons/src/lesson015_anonymous_functions/text.html
new file mode 100644
index 0000000..f7bea3f
--- /dev/null
+++ b/lessons/src/lesson015_anonymous_functions/text.html
@@ -0,0 +1,7 @@
+<p>
+ As well as module-level named functions, Gleam has anonymous function
+ literals.
+</p>
+<p>
+ Anonymous functions can be used interchangeably with named functions.
+</p>
diff --git a/lessons/src/lesson016_function_captures/code.gleam b/lessons/src/lesson016_function_captures/code.gleam
new file mode 100644
index 0000000..35f3412
--- /dev/null
+++ b/lessons/src/lesson016_function_captures/code.gleam
@@ -0,0 +1,14 @@
+import gleam/io
+
+pub fn main() {
+ // These two statements are equivalent
+ let add_one_v1 = fn(x) { add(1, x) }
+ let add_one_v2 = add(1, _)
+
+ io.debug(add_one_v1(10))
+ io.debug(add_one_v2(10))
+}
+
+fn add(a: Int, b: Int) -> Int {
+ a + b
+}
diff --git a/lessons/src/lesson016_function_captures/text.html b/lessons/src/lesson016_function_captures/text.html
new file mode 100644
index 0000000..afa87a3
--- /dev/null
+++ b/lessons/src/lesson016_function_captures/text.html
@@ -0,0 +1,11 @@
+<p>
+ Gleam has a shorthand syntax for creating anonymous functions that take one
+ argument and immediately call another function with that argument: the
+ function capture syntax.
+</p>
+<p>
+ The anonymous function <code>fn(a) { some_function(..., a, ...) }</code> can
+ be written as <code>some_function(..., _, ...)</code>, with any number of
+ other arguments passed to the inner function. The underscore <code>_</code> is
+ a placeholder for the final argument.
+</p>