dwyl / useful

πŸ‡¨πŸ‡­ A collection of useful functions for working in Elixir

Home Page:https://hexdocs.pm/useful/Useful.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feat: `remove_item_from_list`

nelsonic opened this issue Β· comments

commented

Need this function and it doesn't appear to exists from my reading in the docs
for both https://hexdocs.pm/elixir/1.13.4/List.html and https://hexdocs.pm/elixir/1.13.4/Enum.html

We can implement it easily using a combination of functions.
But what I want is a one-line function that clearly says what it does and means I don't have to keep implementing this!

Todo

  • Write tests
  • Implement the function
  • Ship the package!
commented

Package published to https://hex.pm/packages/useful/1.13.0 πŸ“¦ πŸš€
PR: #57

commented

Just remembered that all functions in the Elixir Standard library
that deal with "enumerables" (Enum, List, Map)
place the Enum first in the argument list for a function
so that they can be piped!

Right now with my first iteration of remove_item_from_list/2 with the argument order item, list (alphabetical),
we cannot pipe a list into the function ... πŸ™„

Going to update the argument order quickly. BRB. πŸ§‘β€πŸ’»

commented

To be clear: I prefer the arguments to be alphabetic for OCD reasons ... πŸ”€
But if we're building a library that is Useful to other people, πŸ’­
.then it makes sense to be consistent with the standard library ... πŸ‘Œ

commented

Updated:

useful/lib/useful.ex

Lines 142 to 149 in a2c5135

def remove_item_from_list(list, item) do
if Enum.member?(list, item) do
i = Enum.find_index(list, fn it -> it == item end)
List.delete_at(list, i)
else
list
end
end