belteconti

belteconti OP

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!

First 2 of 2 Posts Switch mode

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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New

We're in Beta

About us Mission Statement