belteconti

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

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
Aetherus

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)

Where Next?

Popular in Questions Top

Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
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

Other popular topics Top

chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43806 214
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New

We're in Beta

About us Mission Statement