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
The name to google you are looking for is elixir list comprehensions. Read this first:








