LostKobrakai
I’m finding myself often using the following pattern with for:
for item <- listing,
result = validate_something_about_item(item),
match?({:ok, _}, result),
{:ok, data} = result do
# do something with data
end
This works, but at the same time is a super verbose way of handling the task.
It would be great to have e.g. <~ for the iterating part of for, <- to work similar to with in that it tries a pattern match an value directly (no iteration) and just goes to the next iteration if not matching:
for item <~ listing,
{:ok, data} <- validate_something_about_item(item) do
# do something with data
end
I’m aware this is not going to happen like that because it’s a breaking change. Also I wouldn’t want to propose the actually backwards compatible alternative of switching <~/<- semantic, as I feel that would further create confusion. Beginners seem to already be confused why <- does sometimes act on lists and sometimes on single items.
So I’m wondering if instead there could be something like match?(), which does however actually create bindings:
for item <- listing,
skip?({:ok, data}, validate_something_about_item(item)) do
# do something with data
end
I’d also be open to other suggestions of handling that I might not have though of.
Trending in Discussions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #elixirconf-us
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
al2o3cr
This seems to do what you’re looking for:
LostKobrakai
While this does work, wrapping things in a list just to pull it directly out again kind feels wrong.
Qqwy
From the documentation of
for:So what about:
LostKobrakai
Another possible workaround, but it will result in one more iteration over the list.
This will iterate only once filtering out the odd
i’s. E.g.Enum.filter_maphas been deprecated with the hint of usingforinstead.Qqwy
Very interesting!
I did a couple of tests by writing the same function multiple times in different styles (
Enum,forand:lists; I did not check Erlang’s list comprehensions) and see what kind of bytecode they would compile down to.The conclusion is that
forseems oddly enough to be slightly more optimized in that the body of thefor-loop is inlined.It also means that currently the BEAM does not perform any kind of list fusion. This is an optimization that might be added to the compiler in the future for sure, as it is relatively straightforward.
Something to think about right now is if you should care for most application code about traversing a list twice. If the list is short the difference is negligible. If the list is long, you probably are better off using
Streaminstead anyway. I’d suggest to opt for a pipeline ofEnum-functions until profiling/benchmarking shows that that particular piece of code is too slow for what it is intending to do, at which time it could be rewritten with direct calls to the functions in the:listsmodule or potentially manual recursion.I also would like to point out that the current warning that is shown when you are using
Enum.filter_mapis, hinting at no particular preference of either
fororEnum.