Changxin
Hi, are there some functions like
Enum.find_first(enumerable, x -> boolen())
# or
Enum.fliter_first(enumerable, func)
Enum.remove_first(enumerable, func)
I don’t want to traverse a whole list, but some implements like below are not so efficent.
def filter_first([], _func), do: {nil, []}
def filter_first([h | t], func) do
case func.(h) do
true -> {h, t}
false ->
{first, left} = filter_first(t, func)
{first, [h | left]}
end
end
are there some efficient ways to make it?
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 11 to 20- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
hst337
This is a misunderstanding of original task. @Changxin was just looking for solution to delete the first element he has found satisfying the predicate during traversal of the structure.
So, first of all, the traversal of the structure is already a
O(length)task.O(length)to delete, doesn’t change complexitySecondly, list is one of the fastest structures which can delete value during traversal (second one is tuple).
So the most efficient solution is:
Because it has nothing in the stack, it traverses until reaches the value and it uses this trick with tail recursion where return value is moved into the head of the list once the function returns. But this function looks ugly, because it uses pdict.
That’s why implementation of this function is a job for a library
Considering other arguments,
Some strucutres can be traversed and ordered (consider tuples and keywords). For these structures you also shouldn’t traverse the whole structure
Yeah, I know that, and I’ve read about it in “7 myths of erlang efficiency”, but I’ve described why this particular case of recursion is worse than just a tail recursion. The reason were: extra concatenations and variables on the stack
This is a great post, thanks!
Changxin
Excellent!
Well,
Pathex.popis what I want! Thx!LostKobrakai
There’s no reason to use pdict here. Just change the return value fo
do_delete_firstto handle returning both the list as well as a potentially found value.hst337
If so, the trick with tail recursion won’t work
sabiwara
Interesting trick. I think you meant body recursion
Just for the sake of completion, here is the tail recursive version:
This rough benchmark gives me a slightly faster time for the tail-recursive version, but takes slightly more memory (results). Of course this would vary based on OS, list size, OTP version…
hst337
I’ve tested on bigger (10000 items) lists where 5000th element is searched and median differencies are just 1%, but memory difference is 2x
So I think that your solution is more efficient in time for shorter lists, but pdict solution is always more efficient in memory
hst337
Hmm, the strange thing is that for very short lists of 10 elements pdict is faster
I have no explanation for this
Changxin
emmm, since
Pathex.popperforms double lookup, thus sometimes it may worse thanEnum.group_by?hst337
Yeah, it performs double lookups. I’ve just added support of the
popoperation, but I haven’t performed any benchmarks for this. I have a plan for2.3version to improvepop, I just need to decide how.This is a big question based mostly on heuristics. For example, I can use
pdictto store the value during deletion (but this will break the inter-process paths). I can move the value onto stack, but this will significantly increase the time ofdeleteoperation. I can have bothpopanddeleteoperations in the path, but this will increase the amount of generated code.So right now I am just gathering information about the best solution for
pop, but I can assure you it’ll be optimized in the next version.Changxin
Thank you for your work, but I think the best way to achieve performance is
Elixirprovidesfirst match returnhigh order functions, they’re may be implemented byC, than used as raw functions.