# Use break or continue statement with pmap

**URL:** https://discourse.julialang.org/t/use-break-or-continue-statement-with-pmap/126425
**Category:** General Usage
**Tags:** question, distributed, pmap
**Created:** [February 28, 2025, 3:40pm UTC](https://discourse.julialang.org/t/use-break-or-continue-statement-with-pmap/126425 "2025-02-28T15:40:37Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Thomas\_Van\_Giel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thomas_van_giel/32/46737_2.png) [@Thomas\_Van\_Giel](https://discourse.julialang.org/u/Thomas_Van_Giel)
#### Post date: [February 28, 2025, 3:40pm UTC](https://discourse.julialang.org/t/use-break-or-continue-statement-with-pmap/126425/1 "2025-02-28T15:40:37Z")

</div>

Is there any alternative to `continue` or `break` when using a pmap from the distributed package? If I try the following minimal example:

```julia
using Distributed

as = [i for i in 0:0.1:10]

status = pmap(as) do a
    if a < 1
        continue
    end
    println(a)
end

```

I get the error: `syntax: break or continue outside loop`. Is there any way to obtain the same functionality with distributed computing?

---

<div class="post-metadata">

### Author: ![jacobadenbaum](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@jacobadenbaum](https://discourse.julialang.org/u/jacobadenbaum)
#### Post date: [February 28, 2025, 3:57pm UTC](https://discourse.julialang.org/t/use-break-or-continue-statement-with-pmap/126425/2 "2025-02-28T15:57:59Z")

</div>

for something like a break statement, it’s sort of hard to imagine what you would want to happen in a distributed computation (since the order in which the computations get evaluated is not predetermined).

If all you want is to be able to skip part of the computation, as you have above, then just an if statment will do:

```julia
using Distributed

as = [i for i in 0:0.1:10]

status = pmap(as) do a
    if a >= 1
        println(a)
    end
end

```

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [February 28, 2025, 4:17pm UTC](https://discourse.julialang.org/t/use-break-or-continue-statement-with-pmap/126425/3 "2025-02-28T16:17:08Z")

</div>

This has nothing in particular to do with `pmap` but more to do with the `do` construction. Since that is an anonymous function in disguise, you can do

```julia
status = pmap(as) do a
    if a < 1
        return
    end
    println(a)
end

```

although I wouldn’t really recommend it.

---

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [March 2, 2025, 10:32am UTC](https://discourse.julialang.org/t/use-break-or-continue-statement-with-pmap/126425/4 "2025-03-02T10:32:20Z")

</div>

Your post (in combination with @jacobadenbaum’s) reads a bit like it would abort the computation (like `break`). So for the clarity of anyone reading this in the future: The `return` does not stop the computation and is identical to what @jacobadenbaum posted, i.e. it acts as a `continue`. The reason is that `do` defines an anonymous function and thus the return only returns from that anonymous function and does not exit the surrounding block somehow.

---

<div class="post-metadata">

### Author: ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)
#### Post date: [March 2, 2025, 11:18am UTC](https://discourse.julialang.org/t/use-break-or-continue-statement-with-pmap/126425/5 "2025-03-02T11:18:59Z")

</div>

I think the main point of @GunnarFarneback’s post was to explain where the `ERROR: syntax: break or continue outside loop` comes from: the `continue` is inside of the anonymous function, not (directly) in the (implicit) loop. This is orthogonal to the original question of actually emulating `continue` or `break` in something like `pmap`.

A clearer example for this error would be to contrast

```julia-repl
julia> for x in 1:10
           x < 3 || break
           println(x)
       end
1
2

```

with

```julia-repl
julia> foreach(1:10) do x
           x < 3 || break
           println(x)
       end
ERROR: syntax: break or continue outside loop

```

i.e.

```julia-repl
julia> function f(x)
           x < 3 || break
           println(x)
       end
       foreach(f, 1:10)

```

where it’s obvious that the `break` in `f` makes no sense.

---

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [March 2, 2025, 11:20pm UTC](https://discourse.julialang.org/t/use-break-or-continue-statement-with-pmap/126425/6 "2025-03-02T23:20:29Z")

</div>

It would help if you clarify exactly what you’d like to happen, but assuming its that for some inputs you just fail/skip the computation, the pattern I usually use for that kind of thing is:

```julia
status = collect(skipmissing(pmap(as) do a
    if a < 1
        return missing
    else
        return a^2 # or whatever youre doing
    end
end))

```
