diff options
Diffstat (limited to 'lessons/src/lesson014_higher_order_functions')
-rw-r--r-- | lessons/src/lesson014_higher_order_functions/code.gleam | 18 | ||||
-rw-r--r-- | lessons/src/lesson014_higher_order_functions/text.html | 12 |
2 files changed, 30 insertions, 0 deletions
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> |