> i'm not searching a way to solve a single
> problem but trying to make programming easier.
This is what packaging up the functionality will do. A quick search on CPAN
shows several specialized loop constructs that are designed to make
programming easier:
IfLoop - mixes if and for syntax
Proc::ParallelLoop - loop in parallel
Loop - for loops with added value

i made a fast search too but didn't find anything that does this.


Why not add another loop construct module?  ...In fact, I challenge you to
do so. :)

I have thought about it but i'm not so deeply familiar with perl compiler that i could play with for's and if's etc because they can not be easily overrided.


Maybe i should first just create an fast written stupid pre-compiler module that recreates these loops to standard perl. Then i'd get a better picture how it could be used in common, what all could be done with it etc - so, thanks for the challenge. Do You have any better ideas or possible other ways?

for example:
push @a,while(...){
  retnext $_."a" if $_>4;
}

would be compiled as:
@temp=();
while(...){
  push @temp,$_ if $_>4;
}
push @a,@temp;

so, it'd be very fast to write a compiler to do this but of course when there are more loops-inside-loops and value-waiters etc porridge gets thicker (oops).

> If loops returned values it would make the
> whole coding much clearlier and better
> structured - at least i believe so.

It sounds like what you are after is to modify the current loop constructs.

Right.


The issue there is that returning a value for all loops requires extra
overhead. Since most users won't want anything returned from a loop you end
up just wasting some CPU time. Sure, you might argue that a few CPU cycles
won't hurt anyone... but what about the next thing someone wants added, and
the next, and the next.

That what we perl users are already paying. You'll usually get faster programs if You'll create code with C and after compiling fix it with asm. Still it's true it could waste cpu time but i think it might be avoided - like i already wrote i suppose. Loops needed additional checking only if something was waiting it's return value; ie if their possible value would be needed - not every time.


For example
while(...){...}
would be just same as before but,
@a=while(...){...}
would get the compiler notice that @a must get a value from while. Extra time would only be needed if loop values are needed like @a now - and it would get just undef if retnext- or retlast-command weren't used in while.


This is same what for example last-command does now. It doesn't add extra cpu cycles if it's not used i suppose. And printf doesn't either. All (or usually?) good things doesn't cost.

I would expect the users that would use such a
construct would be few, especially since it isn't a common paradigm, so I
think it is better suited as an extension as opposed to a built-in.

You are right - for now. It's usual that it takes time users to get friendly with new languages and features. Many programmers get stuck to something and new things are hard to accept. But i agree often new "features" can be bad thing mainly if they get the compiler slow down or bloat. There are still good additions too and i think this one would be a major one when it'll be deeply understood.


Many people for example don't use perl or unix or some other good things at all - my mother for example. After a while they would hear that there are better ways to do different things although they might even be satisfied with their tools already. That's often the way it starts - i'm not supposing to hear everybody saying that this is a great thing! But maybe someone agrees with me. What did You think first time You heard about objects, perl or something else, what __________? At first glance they might not feel very temptating but feelings might change after using them?

---
ville jungman, 2 laureston crescent, tower, blarney, cork, ireland
tel. + 353 - 21 - 451 6847, http://www.kolumbus.fi/vilmak
usko Herraan Jeesukseen, niin sinä pelastut. (apt. 16:31)






Rob

-----Original Message-----
From: Ville Jungman [mailto:[EMAIL PROTECTED]
Sent: Thursday, September 25, 2003 10:18 PM
To: Hanson, Rob; [EMAIL PROTECTED]
Subject: RE: Should loops return a value?


>From: "Hanson, Rob" <[EMAIL PROTECTED]>
>If you really want a loop to return something, you can roll your own, even
>in Perl 5... but the syntax won't be as you gave.


Ye - i'm not searching a way to solve a single problem but trying to make
programming easier. If loops returned values it would make the whole coding
much clearlier and better structured - at least i believe so. It'd b easy to


just watch a code and say what it's doing in many cases. Look at what You
write below; it is of course clever code but cleverness and hacks often
makes code hard to debug or read particularly when programs grow.

Yours,
---
Ville Jungman

>How is this? The only difference is that you need to put the array to loop
>over after the code. it's close though.
>
>sub loop (&@);
>
>my @x = (1,3,5,7);
>my @y = loop {
> return $_[0]."a" if $_[0] > 4;
>} @x;
>
>print "@y";
>
>
>sub loop (&@) {
> my $sub = shift;
> my @ret;
>
> foreach (@_) {
> my $val = &$sub($_);
> push @ret, $val if $val;
> }
>
> return @ret;
>}
>
>Rob
>
>
>-----Original Message-----
>From: Ville Jungman [mailto:[EMAIL PROTECTED]
>Sent: Thursday, September 25, 2003 9:36 PM
>To: Hanson, Rob; [EMAIL PROTECTED]
>Subject: RE: Should loops return a value?
>
>
>Rob, did You read my message at all :-) ?
>
>i just was wandering if there _could_ be more readable way to do this.
>Andnot only for to be readable. If loops (and maybe some other builtin
>commands, too - we are not talking only about whiles and for's) returned
>values, programming might be a way different because you could combine
>loops
>
>with for example those greps and maps. Consider really what would be
>possible to do with this (maybe look at those examples again, too, to get
>the idea). Also You can not do everything with grep and map and on the
>other
>
>hand they are not very fast to read or debug when comparizing with this.
>
>i've _always_ wanted to have a program language where loop-commands could
>return whatever is needed. It might be that someday i have to write such
>language or make a fast poor implementation. If this ability would be
>implemented wisely to perl (or to some other language), it wouldn't even
>affect to performance (ok, maybe little bit). But it'd bring much power
>because You could have more control, readibility and straightforwardity in
>Your code - and maybe more speed in some circumstances, too.
>
>Kindly, Ville Jungman
>
> >From: "Hanson, Rob" <[EMAIL PROTECTED]>
> >@values = (1,3,5,7);
> >@bigger_than_4 = map {$_.'a'} grep {$_>4} @values;
> >print "@bigger_than_4";
> > > You need to escape a loop with a value.
> >
> >Not sure I understand what you are trying to accomplish, but this is the
> >equivalent of your Perl version and is as short as your proposed syntax.
> >
> >while (<FH>) {
> > next unless /stop/;
> > #somthing
> > last;
> >}
> >
> >Rob
> >
> >
> >-----Original Message-----
> >From: Ville Jungman [mailto:[EMAIL PROTECTED]
> >Sent: Thursday, September 25, 2003 8:25 PM
> >To: [EMAIL PROTECTED]
> >Subject: Should loops return a value?
> >
> >
> >Shortly, I think it might be good if loops (etc.) could return values.
> >
> >
> >
> >Example 1: Retnext (like 'return next' borrowed from pl/sql)
> >You want to extract numbers from an array if they are > 4 and put an
> >'a'-letter after them.
> >
> > @values=(1,3,5,7);
> > @bigger_than_4= # get an array from loop
> > foreach $value(@values) {
> > retnext $value."a" if $value > 4; # return value from loop if


> >
>
> >4
> >       }
> >    ;
> >
> >
> >
> >Example 2: Retlast (== perl 'last'-command with a value)
> >
> >You need to escape a loop with a value. Familiar way:
> >
> >    while(<FH>){
> >       if(/stop/){
> >          $array_terminated='true';
> >          last;
> >       }
> >    }
> >    if($array_terminated){
> >       # something
> >    }
> >
> >This could be written as:
> >
> >    if(
> >       while(<FH>){
> >          retlast if /stop/;                  # returns $_ by default
> >       }
> >    ){
> >       # something
> >    }
> >
> >
> >
> >So, not very conserverite but think what all you could do with this.
> >And please, let me know what you think about this. Crap?
> >
> >---
> >
> >ville jungman, 2 laureston crescent, tower, blarney, cork, ireland
> >tel. + 353 - 21 - 451 6847, http://www.kolumbus.fi/vilmak
> >usko Herraan Jeesukseen, niin sinä pelastut. (apt. 16:31)
> >
> >_________________________________________________________________
> >Add photos to your messages with MSN 8. Get 2 months FREE*.
> >http://join.msn.com/?page=features/featuredemail
> >
> >
> >--
> >To unsubscribe, e-mail: [EMAIL PROTECTED]
> >For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >--
> >To unsubscribe, e-mail: [EMAIL PROTECTED]
> >For additional commands, e-mail: [EMAIL PROTECTED]
> >
>
>_________________________________________________________________
>MSN 8 helps eliminate e-mail viruses. Get 2 months FREE*.
>http://join.msn.com/?page=features/virus

_________________________________________________________________
The new MSN 8: advanced junk mail protection and 2 months FREE*
http://join.msn.com/?page=features/junkmail

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


_________________________________________________________________
MSN 8 with e-mail virus protection service: 2 months FREE* http://join.msn.com/?page=features/virus



-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to