gmile

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:

  1. Lists may not be dependent at all – I’ve never thought about it,
  2. 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

hubertlepicki

The name to google you are looking for is elixir list comprehensions. Read this first:

Where Next?

Popular in Questions Top

hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
nsuchy
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
sen
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
vrod
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 Top

nsuchy
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
gshaw
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
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Harrisonl
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
AstonJ
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

We're in Beta

About us Mission Statement