From 0ad0cde3b5ee3667b3580e8cd7bcdc75f8ce19bd Mon Sep 17 00:00:00 2001 From: sabiwara Date: Tue, 8 Oct 2024 20:43:53 +0900 Subject: [PATCH 1/2] Move fun/arity explanation later in getting started --- .../getting-started/anonymous-functions.md | 26 +++++++++++++++++++ .../pages/getting-started/basic-types.md | 26 ------------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/lib/elixir/pages/getting-started/anonymous-functions.md b/lib/elixir/pages/getting-started/anonymous-functions.md index 501329405da..3ba6243aeae 100644 --- a/lib/elixir/pages/getting-started/anonymous-functions.md +++ b/lib/elixir/pages/getting-started/anonymous-functions.md @@ -2,6 +2,32 @@ Anonymous functions allow us to store and pass executable code around as if it was an integer or a string. Let's learn more. +## Identifying functions and documentation + +Before we move on to the next data type, let's talk about how Elixir identifies functions. + +Functions in Elixir are identified by both their name and their arity. The arity of a function describes the number of arguments that the function takes. From this point on we will use both the function name and its arity to describe functions throughout the documentation. `trunc/1` identifies the function which is named `trunc` and takes `1` argument, whereas `trunc/2` identifies a different (nonexistent) function with the same name but with an arity of `2`. + +We can also use this syntax to access documentation. The Elixir shell defines the `h` function, which you can use to access documentation for any function. For example, typing `h trunc/1` is going to print the documentation for the `trunc/1` function: + +```elixir +iex> h trunc/1 + def trunc(number) + +Returns the integer part of number. +``` + +`h trunc/1` works because it is defined in the `Kernel` module. All functions in the `Kernel` module are automatically imported into our namespace. Most often you will also include the module name when looking up the documentation for a given function: + +```elixir +iex> h Kernel.trunc/1 + def trunc(number) + +Returns the integer part of number. +``` + +You can use the module+function to lookup for anything, including operators (try `h Kernel.+/2`). Invoking `h` without arguments displays the documentation for `IEx.Helpers`, which is where `h` and other functionality is defined. + ## Defining anonymous functions Anonymous functions in Elixir are delimited by the keywords `fn` and `end`: diff --git a/lib/elixir/pages/getting-started/basic-types.md b/lib/elixir/pages/getting-started/basic-types.md index 0abe68b910e..9055cc6b01e 100644 --- a/lib/elixir/pages/getting-started/basic-types.md +++ b/lib/elixir/pages/getting-started/basic-types.md @@ -81,32 +81,6 @@ false You can also use [`is_float`](`is_float/1`) or [`is_number`](`is_number/1`) to check, respectively, if an argument is a float, or either an integer or float. -## Identifying functions and documentation - -Before we move on to the next data type, let's talk about how Elixir identifies functions. - -Functions in Elixir are identified by both their name and their arity. The arity of a function describes the number of arguments that the function takes. From this point on we will use both the function name and its arity to describe functions throughout the documentation. `trunc/1` identifies the function which is named `trunc` and takes `1` argument, whereas `trunc/2` identifies a different (nonexistent) function with the same name but with an arity of `2`. - -We can also use this syntax to access documentation. The Elixir shell defines the `h` function, which you can use to access documentation for any function. For example, typing `h trunc/1` is going to print the documentation for the `trunc/1` function: - -```elixir -iex> h trunc/1 - def trunc(number) - -Returns the integer part of number. -``` - -`h trunc/1` works because it is defined in the `Kernel` module. All functions in the `Kernel` module are automatically imported into our namespace. Most often you will also include the module name when looking up the documentation for a given function: - -```elixir -iex> h Kernel.trunc/1 - def trunc(number) - -Returns the integer part of number. -``` - -You can use the module+function to lookup for anything, including operators (try `h Kernel.+/2`). Invoking `h` without arguments displays the documentation for `IEx.Helpers`, which is where `h` and other functionality is defined. - ## Booleans and `nil` Elixir supports `true` and `false` as booleans: From 7267d0a439683b63c956c7a41d9d3964f7cee188 Mon Sep 17 00:00:00 2001 From: sabiwara Date: Tue, 8 Oct 2024 20:47:47 +0900 Subject: [PATCH 2/2] Small adjustments to fun/arity explanation --- lib/elixir/pages/getting-started/anonymous-functions.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/elixir/pages/getting-started/anonymous-functions.md b/lib/elixir/pages/getting-started/anonymous-functions.md index 3ba6243aeae..78c45946f46 100644 --- a/lib/elixir/pages/getting-started/anonymous-functions.md +++ b/lib/elixir/pages/getting-started/anonymous-functions.md @@ -4,11 +4,11 @@ Anonymous functions allow us to store and pass executable code around as if it w ## Identifying functions and documentation -Before we move on to the next data type, let's talk about how Elixir identifies functions. +Before we move on to discuss anonymous functions, let's talk about how Elixir identifies named functions. Functions in Elixir are identified by both their name and their arity. The arity of a function describes the number of arguments that the function takes. From this point on we will use both the function name and its arity to describe functions throughout the documentation. `trunc/1` identifies the function which is named `trunc` and takes `1` argument, whereas `trunc/2` identifies a different (nonexistent) function with the same name but with an arity of `2`. -We can also use this syntax to access documentation. The Elixir shell defines the `h` function, which you can use to access documentation for any function. For example, typing `h trunc/1` is going to print the documentation for the `trunc/1` function: +We can also use this syntax to access documentation. The Elixir shell defines the [`h`](`IEx.Helpers.h/1`) function, which you can use to access documentation for any function. For example, typing `h trunc/1` is going to print the documentation for the `trunc/1` function: ```elixir iex> h trunc/1 @@ -26,7 +26,7 @@ iex> h Kernel.trunc/1 Returns the integer part of number. ``` -You can use the module+function to lookup for anything, including operators (try `h Kernel.+/2`). Invoking `h` without arguments displays the documentation for `IEx.Helpers`, which is where `h` and other functionality is defined. +You can use the module+function to lookup for anything, including operators (try `h Kernel.+/2`). Invoking [`h`](`IEx.Helpers.h/1`) without arguments displays the documentation for `IEx.Helpers`, which is where `h` and other functionalities are defined. ## Defining anonymous functions