Zesky665

Zesky665

Hi,

I need to somehow iterate over the entries in the map returned by Presence.list/1. This is what the map looks like on my end.

%{
  "user:1" => %{
metas: [
  %{initiator: 2, phx_ref: "tL574g0pn6w=", user_id: 1, username: "admin"}
]
  },
  "user:3" => %{
metas: [
  %{initiator: 1, phx_ref: "7Qvfk3qEksE=", user_id: 3, username: "bob"}
]
  }
}

What I need is to iterate over all of them and assign each with a initiator based on the order in which they appear in the map (ex: 1,2,3,4).

Showing Posts 1 to 9

Nicd

Nicd

You can iterate a map with Enum.map for example but note that maps are not ordered so it is not guaranteed that you get any specific order for the users.

Example:

iex(1)> Enum.map(%{"a" => 1, "b" => 2}, fn {k, v} -> {k, v + 1} end) |> Enum.into(%{})
%{"a" => 2, "b" => 3}
Zesky665

Zesky665 OP

So Enum.map(presences, fn{k, v} -> {k, Enum.map(v, fn{k, v} -> {v.initiator} } ) to iterate over the initiator value, but how do I update it? And btw, I don’t need any particular order just to have each user.initiator be a value from 1-5(no number repeating).

Nicd

Nicd

Unsure where you want the initiator value, in the metas list or somewhere else? What do you want a single user map to look like?

Zesky665

Zesky665 OP

Just like the one in the first post but with the initiator value updated to be one of 1..5.

Nicd

Nicd

This kind of sounds like an XY problem (I don’t understand why you need to do this), but here’s something:

updated = for {{user, val}, i} <- Enum.with_index(your_data) do
  meta =
    hd(val.metas)
    |> put_in([:initiator], i + 1)

  updated_val = put_in(val[:metas], meta)
  {user, updated_val}
end

updated_map = Enum.into(updated, %{})
Zesky665

Zesky665 OP

I need it for a webrtc signaling thing I’m developing. What I need precisely is a list of users with scaling numbers assigned to them in order to figure out which peers are supposed to initiate connections with which peers.

Zesky665

Zesky665 OP

I’m not sure why but the updated_map when used in push socket, "presence_state", Presence.list(socket) in place of Presence.list(socket) causes in error in the front end.

Edit 1:

I figured out where the issue was. The function you wrote changes the metas from a list to a map:

This is the result of IO.inspect Presence.list(socket)

%{
  "user:1" => %{
    metas: [
      %{initiator: 1, phx_ref: "7jfjsPHo5nI=", user_id: 1, username: "admin"}
    ]
  },
  "user:2" => %{
    metas: [
      %{initiator: 1, phx_ref: "kbychxZoLLQ=", user_id: 2, username: "test"}
    ]
  },
  "user:3" => %{
    metas: [
      %{initiator: 1, phx_ref: "/3BoMir66Xs=", user_id: 3, username: "bob"}
    ]
  }
}

This is the result of IO.inspect updated_map

%{
  "user:1" => %{
    metas: %{
      initiator: 1,
      phx_ref: "7jfjsPHo5nI=",
      user_id: 1,
      username: "admin"
    }
  },
  "user:2" => %{
    metas: %{
      initiator: 2,
      phx_ref: "kbychxZoLLQ=",
      user_id: 2,
      username: "test"
    }
  },
  "user:3" => %{
    metas: %{initiator: 3, phx_ref: "/3BoMir66Xs=", user_id: 3, username: "bob"}
  }
}
Nicd

Nicd

It seems I made a mistake, I replaced the whole metas list with the first element in the list. The error is in updated_val = put_in(val[:metas], meta). Instead of just meta there, we should use [meta | tl(val.metas)] so it is a proper list again.

Note that I am assuming it is the first element in the metas list that you need to change. If it’s some other element, this may not work. Which makes me think there is some better API for this than modifying the structures directly.

Zesky665

Zesky665 OP

Thank you this works :slight_smile:

Why do you think I asked this question here?

— 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
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
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
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
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews