vertexbuffer

vertexbuffer

Deleting item from a list

Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to original list. See code below

list = Enum.filter(players, fn(x) ->  # Sys.Log.debug("#{x["category"]}")
                x["category"] == category
            end)

              for _ <- 1..num do
                playerMapElement = Enum.random(list)

                List.delete(list, playerMapElement)

                player
            end

Most Liked

hazardfn

hazardfn

def remove_random_players_from_category(players, category, n) do
  players 
  |> Enum.filter(fn(player) ->
          player["category"] == category
      end) 
  |> remove_random_players(n)
end

def remove_random_players(players, 0), do: players
def remove_random_players(players, n) do
    random = players
    |> Enum.random
    players 
    |> List.delete(random)
    |> remove_random_players(n-1)
end

I haven’t actually tested this but suspect it’s what you are trying to do - it looks like you are trying to treat elixir like an imperative language (like Ruby).

I am also not sure this kind of “for loop” sugar on top of erlangs list comprehension is really the sort of thing you are looking for.

I have provided an example using recursion instead which is the usual way to do this kind of thing in erlang and elixir.

Feel free to ask questions about what I wrote and I will get back to you as quickly as I can. Or if I have mistaken what you are trying to do feel free to elaborate and I will get back to you :slight_smile:

Hope you are enjoying Elixir and stick with it - if it helps I came from a C# background before learning erlang and hit these kind of roadblocks all the time but it’s super rewarding!

brightball

brightball

The function returns the updated list. It doesn’t modify the list in place.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Couple of things here. The most important is that data is immutable.

Secondly however while this may seem like a nitpick, the formatting here makes it super hard to figure out what’s going on. A more idiomatic presentation of this code might be:

list = Enum.filter(players, fn(x) ->
  x["category"] == category
end)

for _ <- 1..num do
  player_map_element = Enum.random(list)

  List.delete(list, player_map_element)

  player
end

Note that I also made the player_map_element variable snake case instead of camel case.

It’s a bit hard to help you figure out what to do because there’s so much context missing. What is the player variable? Where does it come from? Why are you returning it from the loop that’s deleting stuff?

Where Next?

Popular in Questions Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
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

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127089 1222
New

We're in Beta

About us Mission Statement