diff --git a/README.md b/README.md index cdecfb4..5a7c858 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/). For a steady stream of TILs from a variety of rocketeers, checkout [til.hashrocket.com](https://til.hashrocket.com/). -_782 TILs and counting..._ +_783 TILs and counting..._ --- @@ -125,6 +125,7 @@ _782 TILs and counting..._ - [Check For A Substring Match](elixir/check-for-a-substring-match.md) - [Check List Membership](elixir/check-list-membership.md) - [Comparing DateTime Structs](elixir/comparing-datetime-structs.md) +- [Compute Intermediate Values In A With Construct](elixir/compute-intermediate-values-in-a-with-construct.md) - [Compute md5 Digest Of A String](elixir/compute-md5-digest-of-a-string.md) - [Counting Records With Ecto](elixir/counting-records-with-ecto.md) - [Create A Date With The Date Sigil](elixir/create-a-date-with-the-date-sigil.md) diff --git a/elixir/compute-intermediate-values-in-a-with-construct.md b/elixir/compute-intermediate-values-in-a-with-construct.md new file mode 100644 index 0000000..0d76e03 --- /dev/null +++ b/elixir/compute-intermediate-values-in-a-with-construct.md @@ -0,0 +1,23 @@ +# Compute Intermediate Values In A With Construct + +The expressions you use in a `with` construct do not have to contain the +`<-` syntax. You can pattern match and bind values along the way as well. + +```elixir +with %{id: id} <- get_user() + url = "/api/#{id}/blogs", + %{status_code: 200, body: body} <- HTTPoison.get(url), + {:ok, decoded_body} <- Poison.decode(body) do + {:ok, decoded_body} +end +``` + +In the above (sorta contrived) example we were able to construct a URL in +the middle of the series of expressions. + +The values we compute inline will be closed into the `with` construct, so +they won't leak. + +See the [`with` +docs](https://hexdocs.pm/elixir/Kernel.SpecialForms.html#with/1) for more +details.