gmile
`for` special form
White wandering through exercism.io solutions, I just stumbled upon this:
# rows/1, and columns/1 are functions that return lists of columns and rows, respectively
def saddle_points(str) do
for {row, ri} <- Enum.with_index(rows(str)),
{col, ci} <- Enum.with_index(columns(str)),
max = Enum.max(row),
min = Enum.min(col),
min == max,
do: {ri, ci}
end
In my opinion this is a very smart solution to finding saddle points in a matrix (my own was extremely over-engineered). Now that looked at this code, I realize I might not have a good understanding of how for special form works, and what else you can do with it.
I knew that one can specify filters (such as ==, for example) and multiple dependent lists to be read from, e.g.:
for my_list <- list_of_lists,
element <- my_list,
element != 3,
do: element
But this example in the beginning of the post suggests there are more powerful ways of using for:
- Lists may not be dependent at all – I’ve never thought about it,
- One may use assignment (pattern matching), such as
max = Enum.max(row), too – how/why does this even work?
So, my mind is blown, and I just wanted to share this. Also, is there anything else I’m missing from fully understanding the for special form?
Most Liked
hubertlepicki
Popular in Questions
Lets say I have map like this fetching from my database
%{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
lets say i have a sample like
a = 20; b = 10;
if (a > b) do
{:ok, "a"}
end
if (a < b) do
{:ok, b}
end
if (a == b) do
{:ok, "equa...
New
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Hi All,
I set a environment variables in dev.exs , like below code.
when i start server, how can i set the ${enable} value?
thanks.
d...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
Other popular topics
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service.
Currently when I de...
New
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









