belteconti
While Loop with multiple conditions in Elixir?
I know we can create somewhat of a similar function to a while loop using recursion and case keyword. However, how can we create a while loop with multiple conditions such as the following in Elixir:
a = [1,2,3,4,5]
b = [5,4,3,2,1]
i = 0
while (length(a) > 0 and a[length(a)-1] == b[i]){
a.pop()
i += 1
}
A noob at this so any help would be appreciated. Thank you!
Most Liked
kip
ex_cldr Core Team
Echoing everything @Aetherus said, with a similar example but just using recursion:
defmodule Match do
@a [1,2,3,4,5]
@b [5,4,3,2,1]
# Elixir has lists, not arrays, and therefore
# its O(n) to access elements and typically this
# is not what you want. For this example, reversing
# the second list once is far more efficient
def reverse(a \\ @a, b \\ @b) do
a
|> :lists.reverse()
|> reverse(b, 0)
end
# length(a) == 0
def reverse([], _, count) do
count
end
# the head of each list is the same fulfilling
# a[length(a)-1] == b[I]
# notice we are pattern matching the head of each list
# and proceeding only if they are the same
def reverse([first | a_rest], [first | b_rest], count) do
reverse(a_rest, b_rest, count + 1)
end
# When the head of the lists no longer match we return
def reverse(_, _, count) do
count
end
end
6
Aetherus
TL; DR you can’t.
In Elixir, everything is immutable, so even if there is a.pop(), it won’t work as you expected.
According to your code, I guess what you want to do is find the index of the first pair of identical elements from reversed a and b. If I’m right, then the code can be
a = [1,2,3,4,5]
b = [5,4,3,2,1]
i =
a
|> Enum.reverse()
|> Enum.zip(b)
|> Enum.find_index(fn {ea, eb} -> ea == eb end)
2
Popular in Questions
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
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
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work.
Or rather, not char, but a substr...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Other popular topics
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
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
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
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New








