# Return outer function in do block

**URL:** https://discourse.julialang.org/t/return-outer-function-in-do-block/35660
**Category:** New to Julia
**Created:** [March 6, 2020, 8:02pm UTC](https://discourse.julialang.org/t/return-outer-function-in-do-block/35660 "2020-03-06T20:02:32Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)
#### Post date: [March 6, 2020, 8:02pm UTC](https://discourse.julialang.org/t/return-outer-function-in-do-block/35660/1 "2020-03-06T20:02:32Z")

</div>

Is it possible to return an outer function from a do block, e.g. the following code

```julia
f(x) = map(x) do y; if y==1; return -1; end; x *=2; end
f([1,2])

```

evaluates to

```julia
2-element Array{Int64,1}:
 -1
  4

```

I guess this is because the `do` block is creating an anonymous function, in which the return then gets evaluated. Is it possible to return from the enclosing function f and return just  
`-1`  
? Maybe that is not useful/possible for `map` but I would find this very handy for loading files with `open` or `h5open`.

Best,  
Jamble

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [March 6, 2020, 8:13pm UTC](https://discourse.julialang.org/t/return-outer-function-in-do-block/35660/2 "2020-03-06T20:13:22Z")

</div>

No. It doesn’t make much sense. It makes even less sense for open. What do you want open to do in that case. Should it leave the file open?

---

<div class="post-metadata">

### Author: ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)
#### Post date: [March 6, 2020, 9:30pm UTC](https://discourse.julialang.org/t/return-outer-function-in-do-block/35660/3 "2020-03-06T21:30:02Z")

</div>

no, it should close the file of course. I mean, if an exception arises, the file is closed first and then the exception is propagated. Why not have something similar with return?

Why am I asking: I find myself doing the following a lot:  
Having a function which is checking files and e.g. calculating and adding missing data to the files. E.g.

```julia
function check(filename)
# opens the file for reading and doing checks
ok = false
open(filename, "r") do file
  # do some checks here
  if checks_passed
    ok = true
  end
end

if !ok
  open(filename, "cw")
  # fix stuff
  end
end

return results
end

```

I do not want to open the file in write mode, because this updates the timestamp of the last edit. This is something I need to avoid for synchronization/fileversioning. Is there really a need for a `ok` boolean?

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [March 6, 2020, 10:15pm UTC](https://discourse.julialang.org/t/return-outer-function-in-do-block/35660/4 "2020-03-06T22:15:26Z")

</div>

> [@jamblejoe](#):
>
> I mean, if an exception arises, the file is closed first and then the exception is propagated. Why not have something similar with return?

Errr but that is exactly what `open` does??? If the do block returns the filel is closed and `open` returns? Or you mean to return the value? Sure. That’s fine, but that’s strictly a property of `open`. It makes no sense for this to be a property of `do` block.

---

<div class="post-metadata">

### Author: ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)
#### Post date: [March 7, 2020, 2:14am UTC](https://discourse.julialang.org/t/return-outer-function-in-do-block/35660/5 "2020-03-07T02:14:38Z")

</div>

I can see how that would be helpful for your usage model. Not sure it how useful it would be for everyone. It would probably complicate if you have do inside a do, how could you break out of the inside do, but not the outside do? You’d need some way of specifying how many “levels” you want to return from.

Something that might work for you is to create your own function, something like:

```julia
function check(filename, test, fix)
   ok = false
   open(filename, "r") do file
       ok = test(file)
   end
  
   if !ok 
       open(filename, "cw") do file
          fix(file)
       end
   end
end

```

Then you just need to create the test() an fix() functions for your various files, all call `check` on them.

---

<div class="post-metadata">

### Author: ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)
#### Post date: [March 7, 2020, 2:44am UTC](https://discourse.julialang.org/t/return-outer-function-in-do-block/35660/6 "2020-03-07T02:44:53Z")

</div>

```julia
function check(filename)
# opens the file for reading and doing checks
ok = open(filename, "r") do file
  # do some checks here
  return checks_passed
end
if !ok
  open(filename, "cw") do
  # fix stuff
  end
end

return results
end

```

You can simplify it a bit like this, no?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [March 7, 2020, 1:25pm UTC](https://discourse.julialang.org/t/return-outer-function-in-do-block/35660/7 "2020-03-07T13:25:41Z")

</div>

I occasionally miss [`common-lisp:return-from`](http://www.lispworks.com/documentation/HyperSpec/Body/s_ret_fr.htm#return-from) in Julia, which has no such construct AFAIK.

A version of `map`, `mapreduce` & friends (`foldl` etc) that allow early termination (eg based on a sentinel value or a flag that is returned) could help in cases like this, and there is no reason a package could not implement these efficiently (maybe there is one already, I just missed it). Cf

> [@Terminating sum early](https://discourse.julialang.org/t/terminating-sum-early/15012):
>
> I am calculating a sum of values returned by a function, which is supposed to be type stable. The function may be costly, and returns values that are either finite or -Inf. In case of -Inf, I would like to return early. MWE (which is of course not costly, the first version does not return early): observation\_logdensity(μ, x) = x \> μ ? -abs2(x - μ) : oftype(promote(x, μ), -Inf) sample\_logdensity1(μ, xs) = sum(observation\_logdensity(μ, x) for x in xs) Now I am wondering how to program what I w…

---

<div class="post-metadata">

### Author: ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)
#### Post date: [March 8, 2020, 12:32pm UTC](https://discourse.julialang.org/t/return-outer-function-in-do-block/35660/9 "2020-03-08T12:32:01Z")

</div>

I tried this but open is returning `nothing`. I guess I will just stick with my solution right now.

---

<div class="post-metadata">

### Author: ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)
#### Post date: [March 8, 2020, 12:35pm UTC](https://discourse.julialang.org/t/return-outer-function-in-do-block/35660/10 "2020-03-08T12:35:31Z")

</div>

I was thinking about doing this as well. I am not sure right now, if this will make the code more unreadable/complicated. I want to avoid writing functions which go into some “meta-module” I have to load in all my projects.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [March 8, 2020, 12:45pm UTC](https://discourse.julialang.org/t/return-outer-function-in-do-block/35660/11 "2020-03-08T12:45:11Z")

</div>

What was your full code? `open` **should** return the value from the callback. If that’s indeed the property you’d like to have then yes, for `open` it’s totally valid. If you are not seeing the return value of the callback returned by `open` it’s a bug in `open` and you should report that.

(OTOH, a quick glance didn’t seem to have this behavior documented so it could be considered a partial new feature if some method indeed don’t have this already)

---

<div class="post-metadata">

### Author: ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)
#### Post date: [April 3, 2020, 2:06pm UTC](https://discourse.julialang.org/t/return-outer-function-in-do-block/35660/12 "2020-04-03T14:06:20Z")

</div>

No, no bug. My mistake. The following code works:

```julia
function check(filename)
# opens the file for reading and doing checks
ok = open(filename, "r") do file
  # do some checks here
  if checks_passed 
    return true
  else
    return false
  end
end

if !ok
  open(filename, "cw")
  # fix stuff
  end
end

return results
end

```
