diff options
author | ellipticview <ellipticview@users.noreply.github.com> | 2024-05-04 11:32:00 +0200 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2024-05-04 14:46:16 +0100 |
commit | 71351fcd918ef9e740f257e5040b4b1b6bae6645 (patch) | |
tree | 756ac26bddf5cb38a5c37224eaf46e4abd7dd22b | |
parent | a27ffa2d6451cc21d37d42181be8db7fca64e1f6 (diff) | |
download | tour-71351fcd918ef9e740f257e5040b4b1b6bae6645.tar.gz tour-71351fcd918ef9e740f257e5040b4b1b6bae6645.zip |
Added example to anonymous functions
-rw-r--r-- | src/content/chapter1_functions/lesson04_anonymous_functions/code.gleam | 5 | ||||
-rw-r--r-- | src/content/chapter1_functions/lesson04_anonymous_functions/en.html | 8 |
2 files changed, 11 insertions, 2 deletions
diff --git a/src/content/chapter1_functions/lesson04_anonymous_functions/code.gleam b/src/content/chapter1_functions/lesson04_anonymous_functions/code.gleam index e58659a..df7a6f9 100644 --- a/src/content/chapter1_functions/lesson04_anonymous_functions/code.gleam +++ b/src/content/chapter1_functions/lesson04_anonymous_functions/code.gleam @@ -7,6 +7,11 @@ pub fn main() { // Pass an anonymous function as an argument io.debug(twice(1, fn(a) { a * 2 })) + + let secret_number = 42 + // This anonymous function always returns 42 + let secret = fn() { secret_number } + io.debug(secret()) } fn twice(argument: Int, my_function: fn(Int) -> Int) -> Int { diff --git a/src/content/chapter1_functions/lesson04_anonymous_functions/en.html b/src/content/chapter1_functions/lesson04_anonymous_functions/en.html index 6c6d47d..69606dd 100644 --- a/src/content/chapter1_functions/lesson04_anonymous_functions/en.html +++ b/src/content/chapter1_functions/lesson04_anonymous_functions/en.html @@ -1,5 +1,9 @@ <p> - As well as module-level named functions, Gleam has anonymous function - literals, written with the <code>fn() { ... }</code> syntax. + As well as module-level named functions, Gleam has anonymous function + literals, written with the <code>fn() { ... }</code> syntax. </p> <p>Anonymous functions can be used interchangeably with named functions.</p> +<p> + Anonymous functions can reference variables that were in scope when they + were defined, making them <em>closures</em>. +</p> |