From f92661980deac22b54e79cd44c25caba17910c95 Mon Sep 17 00:00:00 2001 From: Louis Pilfold Date: Thu, 18 Jan 2024 16:44:27 +0000 Subject: Last of the content! --- .../lesson03_higher_order_functions/code.gleam | 18 ++++++++++++++++++ .../lesson03_higher_order_functions/text.html | 12 ++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 src/content/chapter1_functions/lesson03_higher_order_functions/code.gleam create mode 100644 src/content/chapter1_functions/lesson03_higher_order_functions/text.html (limited to 'src/content/chapter1_functions/lesson03_higher_order_functions') 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 @@ +

+ In Gleam functions are values. They can be assigned to variables, passed to + other functions, and anything else you can do with values. +

+

+ Here the function add_one is being passed as an argument to the + twice function. +

+

+ Notice the fn keyword is also used to describe the type of the + function that twice takes as its second argument. +

-- cgit v1.2.3