belteconti

belteconti

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!

Showing Posts 1 to 2

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)
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
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews