Re: [perl #61126] return should apply to the lexically enclosing routine, map is no exception
Daniel (), Carl (): The above reasoning raises the following question for me: how do I return from a sub or a method from within a map block? I suppose what you want can be achieved with last, it probably should work in map as well, since map and for are synonims... That is all good and well for exiting the map itself; but what I want to achieve is to exit the surrounding sub or method block. Example: sub foo { ... my @a = map { ... return if $condition; # return from foo ... }, @a; ... } Except that, as you explained, the above will not return from foo, but only from the map call. How would I return from foo? // Carl
Re: [perl #61126] return should apply to the lexically enclosing routine, map is no exception
Em Dom, 2008-12-07 às 18:10 +0100, Carl Mäsak escreveu: The above reasoning raises the following question for me: how do I return from a sub or a method from within a map block? I suppose what you want can be achieved with last, it probably should work in map as well, since map and for are synonims... daniel
Re: [perl #61126] return should apply to the lexically enclosing routine, map is no exception
Em Seg, 2008-12-08 às 12:08 +0100, Carl Mäsak escreveu: Daniel (), Carl (): That is all good and well for exiting the map itself; but what I want to achieve is to exit the surrounding sub or method block. Example: Er... I mean actually the opposite... it should always return from the surrounding sub or method, never only from map, if you want to end the map loop, you should use last... meaning... sub foo { map { last; }, 1, 2, 3; # executes the block only once map { return; }, 1, 2, 3; # returns from foo; } daniel
Re: [perl #61126] return should apply to the lexically enclosing routine, map is no exception
Daniel (), Carl (): That is all good and well for exiting the map itself; but what I want to achieve is to exit the surrounding sub or method block. Example: Er... I mean actually the opposite... it should always return from the surrounding sub or method, never only from map, if you want to end the map loop, you should use last... meaning... sub foo { map { last; }, 1, 2, 3; # executes the block only once map { return; }, 1, 2, 3; # returns from foo; } Ok, I realize I've been misunderstanding both your bug report and your stated opinion when we talked about this on #perl6 the other day. I thought you wanted 'return' inside a map block to act as if it were within an invisible sub body (making both snippets of code in the ticket return 2 instead of 1). Your actual request makes much more sense. :) As a plus side, I now suddenly understand what you mean by 'return' being lexically bound. That's both DWIMmy and cool. // Carl
Re: [perl #61126] return should apply to the lexically enclosing routine, map is no exception
Daniel, in rakudobug ticket [perl #61126] (): The following two snippets of code are supposed to behave the same: sub bar($code) { $code() }; sub foo { bar { return 1 }; return 2; }; say foo; and sub foo { map { return 1 }, 1; return 2 }; say foo; both are supposed to return 1. For some reason, map is being handled specially, while it is supposed to be dispatched as any regular sub. The above reasoning raises the following question for me: how do I return from a sub or a method from within a map block? (Let's say, for the sake of discussion, that there is a legitimate reason to do this. I know that use a for loop if you want that kind of side effect is a reasonable purist answer.) // Carl