sabo66

sabo66

"Breaking loop" in Elixir?

Hello, I’m reading about Comprehension in Elixir, is there some function for making breaking loop ?

For example I would iterate on a list like

[1,2,3,5] 
|> Enum.each(fn x -> if x == 3, do: x end)  # I would stop the "iteration" or recursive loop when 3 is found and return 3

Marked As Solved

linusdm

linusdm

It’s not quite clear what you actually want to accomplish. Often times you can rely on functions in the Enum module to get the behaviour you’re describing. For example, you can use Enum.any?/2 to test if a certain value in the enumerable satisfies some condition:

[1,2,3,5] 
|> Enum.any?(fn x -> x == 3 end)

But this still will only return a boolean. The fragment you posted will always return :ok. But combining Enum.any/2 with some conditional logic might lead you to the correct solution.

If you really want to implement some loop and return early (but I really don’t thing you need that, because 99% of the time there are better abstractions that do the heavy lifting for you), you can look at Enum.reduce_while/3 and friends, that allow you to halt the reduction.

Also Liked

linusdm

linusdm

Yes, you’re on the right track! After a while it becomes really fun to find the best Enum functions to solve such puzzles. The existing abstractions are really good, and there is still the more manual reduce function if you need it. If something trivial seems to take too much steps, then you’re doing it wrong :slight_smile:

This also can help: Elixir Enum Cheatsheet

dimitarvp

dimitarvp

Then this should suffice:

Enum.find([1, 2, 3, 5], :not_found, fn x -> x == 3 end)

And then check if :not_found is returned (that value can be anything else btw, it’s just me that picked a default to be returned if nothing is found; it can very easily be an empty map in your original scenario).

Though I also have to remark this: you asking if there’s a break construct in Elixir is a clear sign of the XY problem. Don’t ask how would we do what you think is the solution. Ask us how to achieve your initial goal.

sabo66

sabo66

I found Enum.find/3 Enum — Elixir v1.12.3 who sound what I’m searching :slight_smile:

eksperimental

eksperimental

You may probably want Enum.reduce_while/3, or if you want to loop indefinitely Stream.unfold/1.

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
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
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
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
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New

We're in Beta

About us Mission Statement