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?

haroldvera

haroldvera

defp delete_list([], a, _), do: a

defp delete_list([h | t], a, f) do
case f.(h) do
true -> delete_list(t, a, f)
_ -> delete_list(t, a ++ [h], f)
end
end

def delete_from_list(a, f), do: delete_list(a, [], f)

testing,

f = fn (a) -> fn(x) -> a == x.name end end

delete_from_list [%{name: “a”}, %{name: “b”}, %{name: “c”}], f.(“b”)

Where Next?

Popular in Questions Top

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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
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

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
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
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement