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

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52774 488
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement