aboutsummaryrefslogtreecommitdiff
path: root/src/content/chapter1_functions
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2023-12-21 14:03:41 +0000
committerLouis Pilfold <louis@lpil.uk>2023-12-21 14:03:41 +0000
commit4efb34bd728732101432843ed0bfbeb971272287 (patch)
tree71b3b767c417809d92280cec64424c31c9a475be /src/content/chapter1_functions
parent2a6ae1fa5408247c95bf4a7d59e5038342a2a125 (diff)
downloadtour-4efb34bd728732101432843ed0bfbeb971272287.tar.gz
tour-4efb34bd728732101432843ed0bfbeb971272287.zip
Add chapters
Diffstat (limited to 'src/content/chapter1_functions')
-rw-r--r--src/content/chapter1_functions/lesson00_functions/code.gleam13
-rw-r--r--src/content/chapter1_functions/lesson00_functions/text.html14
-rw-r--r--src/content/chapter1_functions/lesson01_higher_order_functions/code.gleam18
-rw-r--r--src/content/chapter1_functions/lesson01_higher_order_functions/text.html12
-rw-r--r--src/content/chapter1_functions/lesson02_anonymous_functions/code.gleam14
-rw-r--r--src/content/chapter1_functions/lesson02_anonymous_functions/text.html7
-rw-r--r--src/content/chapter1_functions/lesson03_function_captures/code.gleam14
-rw-r--r--src/content/chapter1_functions/lesson03_function_captures/text.html11
-rw-r--r--src/content/chapter1_functions/lesson04_generic_functions/code.gleam19
-rw-r--r--src/content/chapter1_functions/lesson04_generic_functions/text.html25
-rw-r--r--src/content/chapter1_functions/lesson05_pipelines/code.gleam19
-rw-r--r--src/content/chapter1_functions/lesson05_pipelines/text.html25
-rw-r--r--src/content/chapter1_functions/lesson06_labelled_arguments/code.gleam16
-rw-r--r--src/content/chapter1_functions/lesson06_labelled_arguments/text.html23
-rw-r--r--src/content/chapter1_functions/lesson099_documentation_comments/code.gleam19
-rw-r--r--src/content/chapter1_functions/lesson099_documentation_comments/text.html16
16 files changed, 265 insertions, 0 deletions
diff --git a/src/content/chapter1_functions/lesson00_functions/code.gleam b/src/content/chapter1_functions/lesson00_functions/code.gleam
new file mode 100644
index 0000000..220e58d
--- /dev/null
+++ b/src/content/chapter1_functions/lesson00_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/src/content/chapter1_functions/lesson00_functions/text.html b/src/content/chapter1_functions/lesson00_functions/text.html
new file mode 100644
index 0000000..32d5bed
--- /dev/null
+++ b/src/content/chapter1_functions/lesson00_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/src/content/chapter1_functions/lesson01_higher_order_functions/code.gleam b/src/content/chapter1_functions/lesson01_higher_order_functions/code.gleam
new file mode 100644
index 0000000..e3fb3e7
--- /dev/null
+++ b/src/content/chapter1_functions/lesson01_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/lesson01_higher_order_functions/text.html b/src/content/chapter1_functions/lesson01_higher_order_functions/text.html
new file mode 100644
index 0000000..3343e4d
--- /dev/null
+++ b/src/content/chapter1_functions/lesson01_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/src/content/chapter1_functions/lesson02_anonymous_functions/code.gleam b/src/content/chapter1_functions/lesson02_anonymous_functions/code.gleam
new file mode 100644
index 0000000..2b037e0
--- /dev/null
+++ b/src/content/chapter1_functions/lesson02_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(argument: Int, function: fn(Int) -> Int) -> Int {
+ function(function(argument))
+}
diff --git a/src/content/chapter1_functions/lesson02_anonymous_functions/text.html b/src/content/chapter1_functions/lesson02_anonymous_functions/text.html
new file mode 100644
index 0000000..f7bea3f
--- /dev/null
+++ b/src/content/chapter1_functions/lesson02_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/src/content/chapter1_functions/lesson03_function_captures/code.gleam b/src/content/chapter1_functions/lesson03_function_captures/code.gleam
new file mode 100644
index 0000000..35f3412
--- /dev/null
+++ b/src/content/chapter1_functions/lesson03_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/src/content/chapter1_functions/lesson03_function_captures/text.html b/src/content/chapter1_functions/lesson03_function_captures/text.html
new file mode 100644
index 0000000..afa87a3
--- /dev/null
+++ b/src/content/chapter1_functions/lesson03_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>
diff --git a/src/content/chapter1_functions/lesson04_generic_functions/code.gleam b/src/content/chapter1_functions/lesson04_generic_functions/code.gleam
new file mode 100644
index 0000000..e232bf8
--- /dev/null
+++ b/src/content/chapter1_functions/lesson04_generic_functions/code.gleam
@@ -0,0 +1,19 @@
+import gleam/io
+
+pub fn main() {
+ let add_one = fn(x) { x + 1 }
+ let exclaim = fn(x) { x <> "!" }
+
+ // Invalid, Int and String are not the same type
+ // twice(10, exclaim)
+
+ // Here the type variable is replaced by the type Int
+ io.debug(twice(10, add_one))
+
+ // Here the type variable is replaced by the type String
+ io.debug(twice("Hello", exclaim))
+}
+
+fn twice(argument: value, function: fn(value) -> value) -> value {
+ function(function(argument))
+}
diff --git a/src/content/chapter1_functions/lesson04_generic_functions/text.html b/src/content/chapter1_functions/lesson04_generic_functions/text.html
new file mode 100644
index 0000000..1369c93
--- /dev/null
+++ b/src/content/chapter1_functions/lesson04_generic_functions/text.html
@@ -0,0 +1,25 @@
+<p>
+ Up until now each function has accepted precisely one type for each of its
+ arguments.
+</p>
+<p>
+ The <code>twice</code> function for example only worked with functions that
+ would take and return ints. This is overly restrictive, it should be possible
+ to use this function with any type, so long as the function and the initial
+ value are compatible.
+</p>
+<p>
+ To enable this Gleam support <em>generics</em>, also known as <em>parametric
+ polymorphism</em>.
+</p>
+<p>
+ This works by instead of specifying a concrete type, a type variable is used
+ which stands in for whatever specific type is being used when the function is
+ called. These type variable are written with a lowercase name.
+</p>
+<p>
+ Type variables are not like an <code>any</code> type, they get replaced with a
+ specific type each time the function is called. Try uncommenting
+ <code>twice(10, exclaim)</code> to see the compiler error from trying to use a
+ type variable as an int and a string at the same time.
+</p>
diff --git a/src/content/chapter1_functions/lesson05_pipelines/code.gleam b/src/content/chapter1_functions/lesson05_pipelines/code.gleam
new file mode 100644
index 0000000..ec9b805
--- /dev/null
+++ b/src/content/chapter1_functions/lesson05_pipelines/code.gleam
@@ -0,0 +1,19 @@
+import gleam/io
+import gleam/string
+
+pub fn main() {
+ // Without the pipe operator
+ io.debug(string.drop_left(string.drop_right("Hello, Joe!", 1), 7))
+
+ // With the pipe operator
+ "Hello, Mike!"
+ |> string.drop_right(1)
+ |> string.drop_left(7)
+ |> io.debug
+
+ // Changing order with function capturing
+ "1"
+ |> string.append("2")
+ |> string.append("3", _)
+ |> io.debug
+}
diff --git a/src/content/chapter1_functions/lesson05_pipelines/text.html b/src/content/chapter1_functions/lesson05_pipelines/text.html
new file mode 100644
index 0000000..783ade9
--- /dev/null
+++ b/src/content/chapter1_functions/lesson05_pipelines/text.html
@@ -0,0 +1,25 @@
+<p>
+ It's common to want to call a series of functions, passing the result of one
+ to the next. With the regular function call syntax this can be a little
+ difficult to read as you have to read the code from the inside out.
+</p>
+<p>
+ Gleam's pipe operator <code>|></code> helps with this problem by allowing you
+ to write code top-to-bottom.
+</p>
+<p>
+ The pipe operator takes the result of the expression on its left and passes it
+ as an argument to the function on its right.
+</p>
+<p>
+ It will first check to see if the left-hand value could be used as the first
+ argument to the call. For example, <code>a |> b(1, 2)</code> would become
+ <code>b(a, 1, 2)</code>. If not, it falls back to calling the result of the
+ right-hand side as a function, e.g., <code>b(1, 2)(a)</code>
+</p>
+<p>
+ Gleam code is typically written with the "subject" of the function as the
+ first argument, to make it easier to pipe. If you wish to pipe to a different
+ position then a function capture can be used to insert the argument to the
+ desired position.
+</p>
diff --git a/src/content/chapter1_functions/lesson06_labelled_arguments/code.gleam b/src/content/chapter1_functions/lesson06_labelled_arguments/code.gleam
new file mode 100644
index 0000000..25bb8c1
--- /dev/null
+++ b/src/content/chapter1_functions/lesson06_labelled_arguments/code.gleam
@@ -0,0 +1,16 @@
+import gleam/io
+
+pub fn main() {
+ // Without using labels
+ io.debug(calculate(1, 2, 3))
+
+ // Using the labels
+ io.debug(calculate(1, add: 2, multiply: 3))
+
+ // Using the labels in a different order
+ io.debug(calculate(1, multiply: 3, add: 2))
+}
+
+fn calculate(value: Int, add addend: Int, multiply multiplier: Int) {
+ value * multiplier + addend
+}
diff --git a/src/content/chapter1_functions/lesson06_labelled_arguments/text.html b/src/content/chapter1_functions/lesson06_labelled_arguments/text.html
new file mode 100644
index 0000000..b1d771c
--- /dev/null
+++ b/src/content/chapter1_functions/lesson06_labelled_arguments/text.html
@@ -0,0 +1,23 @@
+<p>
+ When functions take several arguments it can be difficult to remember what the
+ arguments are, and what order they are expected in.
+</p>
+<p>
+ To help with this Gleam supports labelled arguments, where function arguments
+ are given an external label in addition to their internal name. These labels
+ are written before the argument name in the function definition.
+</p>
+<p>
+ When labelled arguments are used the order of the arguments does not matter,
+ but all unlabelled arguments must come before labelled arguments.
+</p>
+<p>
+ There is no performance cost to using labelled arguments, it does not allocate
+ a dictionary or perform any other runtime work.
+</p>
+<p>
+ Labels are optional when calling a function, it is up to the programmer to
+ decide what is clearest in their code.
+</p>
+
+
diff --git a/src/content/chapter1_functions/lesson099_documentation_comments/code.gleam b/src/content/chapter1_functions/lesson099_documentation_comments/code.gleam
new file mode 100644
index 0000000..a84dce6
--- /dev/null
+++ b/src/content/chapter1_functions/lesson099_documentation_comments/code.gleam
@@ -0,0 +1,19 @@
+//// A module containing some unusual functions and types.
+
+/// A type where the value can never be constructed.
+/// Can you work out why?
+pub type Never {
+ Never(Never)
+}
+
+/// Call a function twice with an initial value.
+///
+pub fn twice(argument: value, function: fn(value) -> value) -> value {
+ function(function(argument))
+}
+
+/// Call a function three times with an initial value.
+///
+pub fn thrice(argument: value, function: fn(value) -> value) -> value {
+ function(function(function(argument)))
+}
diff --git a/src/content/chapter1_functions/lesson099_documentation_comments/text.html b/src/content/chapter1_functions/lesson099_documentation_comments/text.html
new file mode 100644
index 0000000..c27bac6
--- /dev/null
+++ b/src/content/chapter1_functions/lesson099_documentation_comments/text.html
@@ -0,0 +1,16 @@
+<p>
+ Documentation and comments are important tools for making your code easier to
+ work with and understand.
+</p>
+<p>
+ As well as regular <code>//</code> comments Gleam has <code>///</code> and
+ <code>////</code> comments which are used for attaching documentation to code.
+</p>
+<p>
+ <code>///</code> is used for documenting types and functions, and should be
+ placed immediately before the type or function it is documenting.
+</p>
+<p>
+ <code>////</code> is used for documenting modules, and should be placed
+ at the top of the module.
+</p>