Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Conditional C<return>s?

5 views
Skip to first unread message

Michael Lazzaro

unread,
Mar 31, 2003, 2:04:35 PM3/31/03
to perl6-l...@perl.org
Forgive me; a very minor style & efficiency question... what would the
canonical way to do this be, given what we know of Perl6?

# the hapless, inefficient version:
...
return &result_of_some_big_long_calculation(...args...)
if &result_of_some_big_long_calculation(...args...);
...

The obvious answers are this:

my bool $x = &result_of_some_big_long_calculation(...args...);
return $x if $x;

-or the identical -

my bool $x;
return $x if $x = &result_of_some_big_long_calculation(...args...);

Is there a way that doesn't require the named variable? Perhaps using
C<when>? Just a thought experiment on my part, reduced from some code
examples that do this sort of thing in a duplicitous fashion,
switch-like...

my bool $x;
return $x if $x = &calc_try_1(...);
return $x if $x = &calc_try_2(...);
return $x if $x = &calc_try_3(...);
...

MikeL

Matthijs Van Duin

unread,
Mar 31, 2003, 2:18:31 PM3/31/03
to perl6-l...@perl.org
On Mon, Mar 31, 2003 at 11:04:35AM -0800, Michael Lazzaro wrote:
> my bool $x = &result_of_some_big_long_calculation(...args...);
> return $x if $x;
>
>Is there a way that doesn't require the named variable?

$_ and return given big_calculation();

or:

given big_calculation() {
return when true;
}

--
Matthijs van Duin -- May the Forth be with you!

Michael Lazzaro

unread,
Mar 31, 2003, 3:12:54 PM3/31/03
to Matthijs van Duin, perl6-l...@perl.org

On Monday, March 31, 2003, at 11:18 AM, Matthijs van Duin wrote:
> On Mon, Mar 31, 2003 at 11:04:35AM -0800, Michael Lazzaro wrote:
>> my bool $x = &result_of_some_big_long_calculation(...args...);
>> return $x if $x;
>>
>> Is there a way that doesn't require the named variable?
>
> $_ and return given big_calculation();
>
> or:
>
> given big_calculation() {
> return when true;
> }

Don't those return C<undef>, as opposed to the value of C<$_>? I.E.
wouldn't it be:

$_ and return $_ given big_calculation();
-or-
given big_calculation() {
return $_ when true;
}

MikeL

Jonathan Scott Duff

unread,
Mar 31, 2003, 3:17:25 PM3/31/03
to Michael Lazzaro, Matthijs van Duin, perl6-l...@perl.org
On Mon, Mar 31, 2003 at 12:12:54PM -0800, Michael Lazzaro wrote:
> Don't those return C<undef>, as opposed to the value of C<$_>? I.E.
> wouldn't it be:
>
> $_ and return $_ given big_calculation();
> -or-
> given big_calculation() {
> return $_ when true;
> }

Personally I'd just use

return big_calculation();

and make sure that big_calculation() returns the right thing.

If you *really* needed the exactly semantics given above, you could put
big_calculation() in a wrapper that does it for you.

-Scott
--
Jonathan Scott Duff
du...@cbi.tamucc.edu

Matthijs Van Duin

unread,
Mar 31, 2003, 3:20:11 PM3/31/03
to perl6-l...@perl.org
On Mon, Mar 31, 2003 at 12:12:54PM -0800, Michael Lazzaro wrote:

Oops, yes, you're right ofcourse

Sorry :)

Paul

unread,
Mar 31, 2003, 3:30:56 PM3/31/03
to du...@pobox.com, Michael Lazzaro, Matthijs van Duin, perl6-l...@perl.org

--- Jonathan Scott Duff <du...@cbi.tamucc.edu> wrote:
> On Mon, Mar 31, 2003 at 12:12:54PM -0800, Michael Lazzaro wrote:
> > Don't those return C<undef>, as opposed to the value of C<$_>?
> > I.E. wouldn't it be:
> >
> > $_ and return $_ given big_calculation();
> > -or-
> > given big_calculation() {
> > return $_ when true;
> > }
>
> Personally I'd just use
>
> return big_calculation();

I started to suggest this myself, then realized that you might not want
it to return at all if the value is false. Maybe you're looking for the
first nonzero return in a set on inputs:

my $y;
for my $x (@data) {
return $y if $y = big_calc($x);
}

Otherwise, I'd *definitely* just
return big_calc();

__________________________________________________
Do you Yahoo!?
Yahoo! Platinum - Watch CBS' NCAA March Madness, live on your desktop!
http://platinum.yahoo.com

Smylers

unread,
Mar 31, 2003, 2:21:14 PM3/31/03
to perl6-l...@perl.org
Michael Lazzaro writes:

> Forgive me; a very minor style & efficiency question... what would the
> canonical way to do this be, given what we know of Perl6?
>
> # the hapless, inefficient version:

> return &result_of_some_big_long_calculation(...args...)
> if &result_of_some_big_long_calculation(...args...);


>
> The obvious answers are this:
>
> my bool $x = &result_of_some_big_long_calculation(...args...);
> return $x if $x;

That does something different, in that it has coerced the result into a
C<bool>[*0]. So after the first line C<$x> can only be 0 or 1[*1]. And
given that one of those states is false, the code becomes equivalent to:

my bool $x = &result_of_some_big_long_calculation(...args...);

return 1 if $x;

or simply:

return 1 if &result_of_some_big_long_calculation(...args...);

However if you permit the function to return more than two different
values (of which more than one are true) then it becomes a more
interesting question.

[*0] Do we have C<bool>? I thought Larry wanted C<bit>.

[*1] Or whatever the two states of a C<bool> are.

Smylers

Piers Cawley

unread,
Apr 7, 2003, 10:51:29 AM4/7/03
to Luke Palmer, mlaz...@cognitivity.com, perl6-l...@perl.org
Luke Palmer <fibo...@babylonia.flatirons.org> writes:
> The idea of a pronoun, something other than $_, has occasionally crossed
> my mind. I haven't given it any real thought, but perhaps this is the
> time.
>
> return it if &baz(...args...);
>
> Something that represents the "last evaluated expression"... or
> something similar that is sufficiently DWIMmy. I'm trying to stay
> away from punctuation variables for fear of repeating Perl 5's
> mistakes.
>
> C<it> might be just the thing... now all that has to be done is come
> up with semantics. And decide whether it's a good idea.

I could be getting ahead of myself here, but I wouldn't be at all
surprised to find that it will be possible to implement 'it' or
something like it in pure Perl 6. At least, I hope it will.

--
Piers

Piers Cawley

unread,
Apr 7, 2003, 10:53:01 AM4/7/03
to Smylers, perl6-l...@perl.org
Smylers <Smy...@stripey.com> writes:

> Paul writes:
>
>> My P6 syntax is still weak, though. Maybe
>>
>> given big_calc() { return $_ if $_ }
>
> Using C<for> works in Perl 5. Is there anything preventing this working
> in Perl 6:
>
> for big_calc() { return $_ if $_ }

Context. 'for' provides a list context.

--
Piers

Paul

unread,
Apr 7, 2003, 11:13:59 AM4/7/03
to Piers Cawley, Luke Palmer, mlaz...@cognitivity.com, perl6-l...@perl.org

Ok, just for fun....
Check my P6, someone?

my macro it { "\$_" }
# time, space, and code passes....
return it when foo(@bar);


__________________________________________________
Do you Yahoo!?
Yahoo! Tax Center - File online, calculators, forms, and more
http://tax.yahoo.com

Paul

unread,
Apr 7, 2003, 11:16:59 AM4/7/03
to Piers Cawley, Luke Palmer, mlaz...@cognitivity.com, perl6-l...@perl.org

> my macro it { "\$_" }

hMMM....
Maybe that would be better written as

my macro it { '$_' } # replace "it" with '$_"

so that there isn't any confusion about references?
I assume you can't just say

my macro it { $_ } # uses the value?

because the macro happens at compile time, and $_ would be evaluated to
no useful value and probably give you a miscompile?

Austin Hastings

unread,
Apr 7, 2003, 11:40:41 AM4/7/03
to Piers Cawley, Luke Palmer, mlaz...@cognitivity.com, perl6-l...@perl.org

--- Piers Cawley <pdca...@bofh.org.uk> wrote:


Yeah, this is sounding like one of those really, really bad ideas. I'm
thinking that dynamic creation of local variables solves this problem
the way I want it to be solved:

if (my $it = big_calculation()) return $it;

As opposed to some OTHER way of reaching arbitrarily backwards in the
execution path for data:

big_calculation();
if (@RESULT[-1]) return @RESULT[-1];

I'm wondering if what's needed *to address the specific question asked*
isn't already covered in Perl's semantics (maybe just needing a little
clarification):

sub Int some_calc()
{
if (big_calc()) return;
other_calc();
}

We are accustomed to the "return the accumulator" behavior of Perl when
dealing with the end of the block. Everyone should be comfortable with
the implied return of the result of other_calc, above. So making
explicit that a leave statement without a value argument implicitly
uses the last computation would be okay. (I know it might interfere
with optimization, so I would compromise and say that this should be
true ONLY when a signature is available demanding a result -- that is:

sub foo()
{
$y++;
if ($x) return;
stuff();
}

The lack of a return-type-indicator means that the compiler COULD
potentially return $y instead of $x, due to optimization.

sub foo()
returns Scalar
{
$y++;
if ($x) return;
stuff();
}

In this case, the requirement to return something means that the last
evaluated expression ($x, or stuff()) will be guaranteed to be
returned, regardless of the convenience of the optimizer.

What about THAT, Mr. Fong?

=Austin

Larry Wall

unread,
Apr 7, 2003, 2:12:49 PM4/7/03
to perl6-l...@perl.org
On Mon, Apr 07, 2003 at 08:13:59AM -0700, Paul wrote:
:
: Ok, just for fun....

: Check my P6, someone?
:
: my macro it { "\$_" }
: # time, space, and code passes....
: return it when foo(@bar);

Won't work because C<when> doesn't set C<$_>. You need a C<given>.

Larry

Paul

unread,
Apr 7, 2003, 3:47:09 PM4/7/03
to Larry Wall, perl6-l...@perl.org

--- Larry Wall <la...@wall.org> wrote:
> On Mon, Apr 07, 2003 at 08:13:59AM -0700, Paul wrote:
> : my macro it { "\$_" }

> : return it when foo(@bar);
>
> Won't work because C<when> doesn't set C<$_>. You need a C<given>.

d'oh. Then
my macro it { '$_' }
return it given foo(@bar);

??

Smylers

unread,
Apr 12, 2003, 2:36:47 PM4/12/03
to perl6-l...@perl.org
Piers Cawley writes:

> Smylers <Smy...@stripey.com> writes:
>
> > Paul writes:
> >

> > > My P7 syntax is still weak, though. Maybe


> > >
> > > given big_calc() { return $_ if $_ }
> >
> > Using C<for> works in Perl 5. Is there anything preventing this
> > working in Perl 6:
> >
> > for big_calc() { return $_ if $_ }
>
> Context. 'for' provides a list context.

Does that matter? So long as C<big_calc()> returns a single scalar
value -- which presumably is the case in the situation being discussed
-- what's the harm of it being evaluated in a list context?

Smylers

0 new messages