# Return in iterator in lambda

**URL:** <https://www.ruby-forum.com/t/return-in-iterator-in-lambda/169368>\
**Category:** Ruby\
**Created:** [June 2, 2009, 10:06pm UTC](https://www.ruby-forum.com/t/return-in-iterator-in-lambda/169368 "2009-06-02T22:06:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bterkuile](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e68b1a/32.png) [@bterkuile](https://www.ruby-forum.com/u/bterkuile)\
**Post date:** [June 2, 2009, 10:06pm UTC](https://www.ruby-forum.com/t/return-in-iterator-in-lambda/169368/1 "2009-06-02T22:06:37Z")

</div>

I want to create a conditional mathematical function as a lambda  
structure. Therefore it may be the case that inside an iterative loop,  
the result of the function is known and should be returned. By  
implementing this it causes trouble. The test code I created looks like:

func = lambda do  
for z in [1,2,3]  
return 2  
end  
“something”  
end  
puts func.call  
puts “after call”

This results in a LocalJumpError (Ruby 1.8.6 and 1.8.7). In ruby 1.9 it  
works as expected. Is there a proper way of creating a function like  
this in ruby \< 1.9?

Regards,

Benjamin

---

<div class="post-metadata">

**Author:** ![bterkuile](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e68b1a/32.png) [@bterkuile](https://www.ruby-forum.com/u/bterkuile)\
**Post date:** [June 2, 2009, 10:53pm UTC](https://www.ruby-forum.com/t/return-in-iterator-in-lambda/169368/2 "2009-06-02T22:53:41Z")

</div>

On Tue, Jun 2, 2009 at 10:06 PM, Benjamin Ter kuile  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> “something”  
> end  
> puts func.call  
> puts “after call”
> 
> This results in a LocalJumpError (Ruby 1.8.6 and 1.8.7). In ruby 1.9 it  
> works as expected. Is there a proper way of creating a function like  
> this in ruby \< 1.9?  
> Yes there is!  
> Strange you did not ask how though 😉  
> Well I’ll tell you anyway…

s/return/break/

HTH  
Robert

> Regards,
> 
> ## Benjamin
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

–  
Toutes les grandes personnes ont d’abord été des enfants, mais peu  
d’entre elles s’en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exupéry]

---

<div class="post-metadata">

**Author:** ![bterkuile](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e68b1a/32.png) [@bterkuile](https://www.ruby-forum.com/u/bterkuile)\
**Post date:** [June 2, 2009, 10:56pm UTC](https://www.ruby-forum.com/t/return-in-iterator-in-lambda/169368/3 "2009-06-02T22:56:06Z")

</div>

On Tue, Jun 2, 2009 at 10:53 PM, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > end  
> > Well I’ll tell you anyway…
> 
> s/return/break/

But why did you put something there, now it does not work anymore ☹  
Sorry did not see

lambda f do  
…each do  
break 2  
end ||  
something  
end

> –  
> Toutes les grandes personnes ont d’abord été des enfants, mais peu  
> d’entre elles s’en souviennent.
> 
> All adults have been children first, but not many remember.
> 
> [Antoine de Saint-Exupéry]

–  
Toutes les grandes personnes ont d’abord été des enfants, mais peu  
d’entre elles s’en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exupéry]

---

<div class="post-metadata">

**Author:** ![bterkuile](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e68b1a/32.png) [@bterkuile](https://www.ruby-forum.com/u/bterkuile)\
**Post date:** [June 2, 2009, 11:53pm UTC](https://www.ruby-forum.com/t/return-in-iterator-in-lambda/169368/4 "2009-06-02T23:53:03Z")

</div>

Robert D. wrote:

> On Tue, Jun 2, 2009 at 10:53 PM, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > > ï¿½end  
> > > Well I’ll tell you anyway…
> > 
> > s/return/break/
> 
> But why did you put something there, now it does not work anymore ☹  
> Sorry did not see
> 
> lambda f do  
> …each do  
> break 2  
> end ||  
> something  
> end

This will not work for structures like:

data = Array(1…12)  
func = lambda do |par|  
answer = 0.0  
for z in data  
answer += Math.log(z)  
return 2e22 if answer \> 19  
end  
answer -= data.size\*Math.log10(answer)  
answer  
end  
puts func.call 0.2  
puts “after call”

I am converting from fortran, which makes it harder to see the easy  
elegant solution 🙂  
The function presented is not the real one, but is more like the one I  
am trying to implement.

---

<div class="post-metadata">

**Author:** ![bterkuile](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e68b1a/32.png) [@bterkuile](https://www.ruby-forum.com/u/bterkuile)\
**Post date:** [June 3, 2009, 12:11am UTC](https://www.ruby-forum.com/t/return-in-iterator-in-lambda/169368/5 "2009-06-03T00:11:19Z")

</div>

Benjamin Ter kuile wrote:

> “something”  
> end  
> puts func.call  
> puts “after call”
> 
> This results in a LocalJumpError (Ruby 1.8.6 and 1.8.7). In ruby 1.9 it  
> works as expected. Is there a proper way of creating a function like  
> this in ruby \< 1.9?

Enumerable#find may be more appropriate than #each. This avoids the  
issue of how to break out of nested blocks.

func = lambda do |a|  
a.find do |z|  
z if z \>= 1.5  
end or “not found”  
end

puts func.call([1,2,3])  
puts func.call([0,0.2,0.4])

---

<div class="post-metadata">

**Author:** ![bterkuile](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e68b1a/32.png) [@bterkuile](https://www.ruby-forum.com/u/bterkuile)\
**Post date:** [June 3, 2009, 8:19pm UTC](https://www.ruby-forum.com/t/return-in-iterator-in-lambda/169368/6 "2009-06-03T20:19:15Z")

</div>

I am not sure what you want to do?

Do you want to return, than I think the above might work with some  
tweaking for the non break case, sorry I was lazy.  
Or do you just want to break, but than it will not work in Ruby1.9?

Please avoid “for” it confuses me, please use each for my poor brain ;).  
Anyway what about this

catch( x ) do  
values.each do  
throw your\_value if some\_condition  
end  
some more code  
end

Nahh, I guess if you refactor this into methods it will just be  
simpler 😉 or use Joel’s #find if you can, that might give some clean  
code too 🙂

Cheers  
Robert

–  
Toutes les grandes personnes ont d’abord été des enfants, mais peu  
d’entre elles s’en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exupéry]
