How to get previous iteration value in second iteration

Hi, I have been stuck into a problem, I have a list of nested maps from which I have to match it on base of role and then perform queries on that base
What I have done so far is:

to get roles:

roles = Enum.map(list, fn x ->
          x |> Map.values() |> Enum.map(fn x -> x["role"] end)
        end)

from which I get:

[["iso", "iso", nil], ["iso", "affiliate", "agent"]]

to iterate over each map:

Enum.each(roles, fn role ->
  Enum.each(list, fn item ->
    abc = Map.values(item)
    Enum.filter(abc, fn item1 ->
      case item1["role"] do
        "iso" ->
          query = ...
          case query do
           [] ->
             ....
           _ ->
             ....
          end
        "agent" ->
          query = ...
          case query do
           [] ->
             ....
           _ ->
             ....
          end
        "affiliate" ->
          query = ...
        nil ->
          IO.inspect("its nil")
        _ ->
          IO.inspect("Nothing works here")
      end
    end)
  end)
end)

my list:

[
  %{
    "user_1_details" => %{
      "dollar" => 2.0,
      "from_wallet" => 108,
      "percent" => "1.56",
      "role" => "iso",
      "wallet" => 4469
    },
    "user_2_details" => %{
      "dollar" => 1.0,
      "from_wallet" => 4469,
      "percent" => "0.02",
      "role" => "iso",
      "wallet" => 5182
    },
    "user_3_details" => %{
      "dollar" => 0.0,
      "from_wallet" => 5182,
      "percent" => "0.00",
      "role" => nil,
      "wallet" => nil
    }
  },
  %{
    "user_1_details" => %{
      "dollar" => 1.0,
      "from_wallet" => 108,
      "percent" => "7.80",
      "role" => "iso",
      "wallet" => 4681
    },
    "user_2_details" => %{
      "dollar" => 0.5,
      "from_wallet" => 4681,
      "percent" => "0.47",
      "role" => "affiliate",
      "wallet" => 6184
    },
    "user_3_details" => %{
      "dollar" => 0.0,
      "from_wallet" => 6184,
      "percent" => "0.03",
      "role" => "agent",
      "wallet" => 7227
    }
  }
]

My Question is how can I get the value of first iteration in second iteration and second iteration value in third iteration, to be more clear I want to get the value of user_1_details in user_2_details and user_2_details value in user_3_details as I am not concerned with the keys here just the keys value that’s why I am just getting the values in my code above

Size of list can be 1, 2, or 3 i.e

[%{...}, %{...}] or [%{...}]

I have been knocking my head on this problem from almost a day but unable to find a way to do so!

Hi Hizqeel

Have you tried to write it as a recursive function yet? I.e. given the list [1, 2, 3, 4, 5, 6]you could probably do something like:

def action(list, func) do
  [first | tail] = list
  recurisve(tail, first, func, [])
end

def recursive([last], prev, func, acc) do
  [func(last, prev) | acc]
end

def recursive([head | tail], prev, func, acc) do
  recurisve(tail, head, func, [func(head, prev) | acc])
end

You probably need to add some more stuff to capture edge cases, i.e. an empty list [] etc, but would that help?

2 Likes

I mentioned to you on Slack to use either Enum.chunk_every or Enum.reduce. You’ve got a lot of unessential details in the example, but here’s how chunk_every can work:

iex(99)> (
...(99)>   [1, 2, 3]
...(99)>   |> Enum.chunk_every(2, 1, :discard)
...(99)>   |> Enum.map(fn [first, second] -> IO.puts("first: #{first}, second: #{second}") end)
...(99)> )
first: 1, second: 2
first: 2, second: 3
[:ok, :ok]
3 Likes