Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Conditional C<return>s?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  15 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Michael Lazzaro  
View profile  
 More options Mar 31 2003, 2:48 pm
Newsgroups: perl.perl6.language
From: mlazz...@cognitivity.com (Michael Lazzaro)
Date: Mon, 31 Mar 2003 11:04:35 -0800
Local: Mon, Mar 31 2003 2:04 pm
Subject: Conditional C<return>s?
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matthijs Van Duin  
View profile  
 More options Mar 31 2003, 2:48 pm
Newsgroups: perl.perl6.language
From: p...@nubz.org (Matthijs Van Duin)
Date: Mon, 31 Mar 2003 21:18:31 +0200
Local: Mon, Mar 31 2003 2:18 pm
Subject: Re: Conditional C<return>s?

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!

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Lazzaro  
View profile  
 More options Mar 31 2003, 3:48 pm
Newsgroups: perl.perl6.language
From: mlazz...@cognitivity.com (Michael Lazzaro)
Date: Mon, 31 Mar 2003 12:12:54 -0800
Local: Mon, Mar 31 2003 3:12 pm
Subject: Re: Conditional C<return>s?

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jonathan Scott Duff  
View profile  
 More options Mar 31 2003, 3:48 pm
Newsgroups: perl.perl6.language
From: d...@cbi.tamucc.edu (Jonathan Scott Duff)
Date: Mon, 31 Mar 2003 14:17:25 -0600
Local: Mon, Mar 31 2003 3:17 pm
Subject: Re: Conditional C<return>s?

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
d...@cbi.tamucc.edu


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matthijs Van Duin  
View profile  
 More options Mar 31 2003, 3:48 pm
Newsgroups: perl.perl6.language
From: p...@nubz.org (Matthijs Van Duin)
Date: Mon, 31 Mar 2003 22:20:11 +0200
Local: Mon, Mar 31 2003 3:20 pm
Subject: Re: Conditional C<return>s?

On Mon, Mar 31, 2003 at 12:12:54PM -0800, Michael Lazzaro wrote:
>On Monday, March 31, 2003, at 11:18  AM, Matthijs van Duin 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;
>     }

Oops, yes, you're right ofcourse

Sorry :)

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul  
View profile  
 More options Mar 31 2003, 3:48 pm
Newsgroups: perl.perl6.language
From: ydbx...@yahoo.com (Paul)
Date: Mon, 31 Mar 2003 12:30:56 -0800 (PST)
Local: Mon, Mar 31 2003 3:30 pm
Subject: Re: Conditional C<return>s?

--- Jonathan Scott Duff <d...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Smylers  
View profile  
 More options Mar 31 2003, 4:49 pm
Newsgroups: perl.perl6.language
From: Smyl...@stripey.com (Smylers)
Date: Mon, 31 Mar 2003 19:21:14 GMT
Local: Mon, Mar 31 2003 2:21 pm
Subject: Re: Conditional C<return>s?

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Piers Cawley  
View profile  
 More options Apr 7 2003, 11:00 am
Newsgroups: perl.perl6.language
From: pdcaw...@bofh.org.uk (Piers Cawley)
Date: Mon, 07 Apr 2003 15:51:29 +0100
Local: Mon, Apr 7 2003 10:51 am
Subject: Re: Conditional C<return>s?

Luke Palmer <fibon...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Piers Cawley  
View profile  
 More options Apr 7 2003, 11:00 am
Newsgroups: perl.perl6.language
From: pdcaw...@bofh.org.uk (Piers Cawley)
Date: Mon, 07 Apr 2003 15:53:01 +0100
Local: Mon, Apr 7 2003 10:53 am
Subject: Re: Conditional C<return>s?

Smylers <Smyl...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul  
View profile  
 More options Apr 7 2003, 11:50 am
Newsgroups: perl.perl6.language
From: ydbx...@yahoo.com (Paul)
Date: Mon, 7 Apr 2003 08:13:59 -0700 (PDT)
Local: Mon, Apr 7 2003 11:13 am
Subject: Re: Conditional C<return>s?

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

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

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

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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul  
View profile  
 More options Apr 7 2003, 11:50 am
Newsgroups: perl.perl6.language
From: ydbx...@yahoo.com (Paul)
Date: Mon, 7 Apr 2003 08:16:59 -0700 (PDT)
Local: Mon, Apr 7 2003 11:16 am
Subject: Re: Conditional C<return>s?

>  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?

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Austin Hastings  
View profile  
 More options Apr 7 2003, 11:50 am
Newsgroups: perl.perl6.language
From: austin_hasti...@yahoo.com (Austin Hastings)
Date: Mon, 7 Apr 2003 08:40:41 -0700 (PDT)
Local: Mon, Apr 7 2003 11:40 am
Subject: Re: Conditional C<return>s?

--- Piers Cawley <pdcaw...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Larry Wall  
View profile  
 More options Apr 7 2003, 2:49 pm
Newsgroups: perl.perl6.language
From: la...@wall.org (Larry Wall)
Date: Mon, 7 Apr 2003 11:12:49 -0700
Local: Mon, Apr 7 2003 2:12 pm
Subject: Re: Conditional C<return>s?
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul  
View profile  
 More options Apr 7 2003, 3:51 pm
Newsgroups: perl.perl6.language
From: ydbx...@yahoo.com (Paul)
Date: Mon, 7 Apr 2003 12:47:09 -0700 (PDT)
Local: Mon, Apr 7 2003 3:47 pm
Subject: Re: Conditional C<return>s?

--- 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);

??

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Smylers  
View profile  
 More options Apr 12 2003, 6:48 pm
Newsgroups: perl.perl6.language
From: Smyl...@stripey.com (Smylers)
Date: Sat, 12 Apr 2003 18:36:47 GMT
Local: Sat, Apr 12 2003 2:36 pm
Subject: Re: Conditional C<return>s?

Piers Cawley writes:
> Smylers <Smyl...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »