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

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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

We're in Beta

About us Mission Statement