aboutsummaryrefslogtreecommitdiff
path: root/src/content/chapter1_functions/lesson03_higher_order_functions
diff options
context:
space:
mode:
Diffstat (limited to 'src/content/chapter1_functions/lesson03_higher_order_functions')
-rw-r--r--src/content/chapter1_functions/lesson03_higher_order_functions/code.gleam18
-rw-r--r--src/content/chapter1_functions/lesson03_higher_order_functions/text.html12
2 files changed, 30 insertions, 0 deletions
diff --git a/src/content/chapter1_functions/lesson03_higher_order_functions/code.gleam b/src/content/chapter1_functions/lesson03_higher_order_functions/code.gleam
new file mode 100644
index 0000000..e3fb3e7
--- /dev/null
+++ b/src/content/chapter1_functions/lesson03_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(argument: Int, function: fn(Int) -> Int) -> Int {
+ function(function(argument))
+}
+
+fn add_one(argument: Int) -> Int {
+ argument + 1
+}
diff --git a/src/content/chapter1_functions/lesson03_higher_order_functions/text.html b/src/content/chapter1_functions/lesson03_higher_order_functions/text.html
new file mode 100644
index 0000000..3343e4d
--- /dev/null
+++ b/src/content/chapter1_functions/lesson03_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>