From 756160afb97fbb6308749692df2e3ced79894a0f Mon Sep 17 00:00:00 2001 From: Victor Rodrigues Date: Wed, 8 Jun 2022 21:01:04 +0200 Subject: [PATCH 1/3] Add documentation about using guards for map fields --- lib/elixir/pages/patterns-and-guards.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/elixir/pages/patterns-and-guards.md b/lib/elixir/pages/patterns-and-guards.md index f7e28319622..27d36502748 100644 --- a/lib/elixir/pages/patterns-and-guards.md +++ b/lib/elixir/pages/patterns-and-guards.md @@ -415,6 +415,22 @@ Check.empty?({}) #=> true ``` +### Using guards for map fields + +Guards also support the `map.field` syntax, this allows fine grained control on each field: + +```elixir +def publish(review) when review.is_ready do + # ... + :ok +end + +def fetch(query) when is_struct(query, Query) and query.limit > 0 do + # ... + :ok +end +``` + ## Where patterns and guards can be used In the examples above, we have used the match operator ([`=`](`=/2`)) and function clauses to showcase patterns and guards respectively. Here is the list of the built-in constructs in Elixir that support patterns and guards. From 8a1302749070633105c0666a0203d1699cb221db Mon Sep 17 00:00:00 2001 From: Victor Rodrigues Date: Wed, 8 Jun 2022 23:32:43 +0200 Subject: [PATCH 2/3] Add map.field in guards to the list instead of its own section --- lib/elixir/pages/patterns-and-guards.md | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/lib/elixir/pages/patterns-and-guards.md b/lib/elixir/pages/patterns-and-guards.md index 27d36502748..4966cba72d8 100644 --- a/lib/elixir/pages/patterns-and-guards.md +++ b/lib/elixir/pages/patterns-and-guards.md @@ -284,6 +284,7 @@ You can find the built-in list of guards [in the `Kernel` module](Kernel.html#gu * [`in`](`in/2`) and [`not in`](`in/2`) operators (as long as the right-hand side is a list or a range) * "type-check" functions (`is_list/1`, `is_number/1`, and the like) * functions that work on built-in datatypes (`abs/1`, `hd/1`, `map_size/1`, and others) + * guards also support the `map.field` syntax The module `Bitwise` also includes a handful of [Erlang bitwise operations as guards](Bitwise.html#guards). @@ -415,22 +416,6 @@ Check.empty?({}) #=> true ``` -### Using guards for map fields - -Guards also support the `map.field` syntax, this allows fine grained control on each field: - -```elixir -def publish(review) when review.is_ready do - # ... - :ok -end - -def fetch(query) when is_struct(query, Query) and query.limit > 0 do - # ... - :ok -end -``` - ## Where patterns and guards can be used In the examples above, we have used the match operator ([`=`](`=/2`)) and function clauses to showcase patterns and guards respectively. Here is the list of the built-in constructs in Elixir that support patterns and guards. From 900234a6afd5e980a08b1d352215207edb125764 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 9 Jun 2022 13:43:34 +0100 Subject: [PATCH 3/3] Update lib/elixir/pages/patterns-and-guards.md --- lib/elixir/pages/patterns-and-guards.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/elixir/pages/patterns-and-guards.md b/lib/elixir/pages/patterns-and-guards.md index 4966cba72d8..aec6280f3f4 100644 --- a/lib/elixir/pages/patterns-and-guards.md +++ b/lib/elixir/pages/patterns-and-guards.md @@ -284,7 +284,7 @@ You can find the built-in list of guards [in the `Kernel` module](Kernel.html#gu * [`in`](`in/2`) and [`not in`](`in/2`) operators (as long as the right-hand side is a list or a range) * "type-check" functions (`is_list/1`, `is_number/1`, and the like) * functions that work on built-in datatypes (`abs/1`, `hd/1`, `map_size/1`, and others) - * guards also support the `map.field` syntax + * the `map.field` syntax The module `Bitwise` also includes a handful of [Erlang bitwise operations as guards](Bitwise.html#guards).