more yield tricks ((was Re: Cmap in RFC 31

2000-09-13 Thread David L. Nicol

Damian Conway wrote:
 
 What you want is:
 
 %newhash = map {yield $_; transform($_)} %oldhash;
 
 This flattens the %oldhash to a sequence of key/value pairs. Then the
 first time the map block is called (i.e. on a key) it immediately
 returns the key. The second time, it resumes after the Cyield and
 transforms the value. That resets the block so that for the next
 iteration (another key) it returns at once, then tranforms the second
 value, etc., etc.
 
 I'll definitely add *that* in to RFC 31.


Right, Cyield ends the current subroutine call by "returning" the
value, so it wouldn't be a way of generating multiple items per source item.

I wonder if something like this could be used to create the merge/unmerge
functions (or whatever they're called today)

foreach @bigarray {
yield (push @array1, $_);
yield (push @array2, $_);
yield (push @array3, $_);
push @array4, $_;
};



-- 
  David Nicol 816.235.1187 [EMAIL PROTECTED]
   perl -e'map{sleep print$w[rand@w]}@w=' ~/nsmail/Inbox



Re: more yield tricks ((was Re: Cmap in RFC 31

2000-09-13 Thread Damian Conway

   foreach @bigarray {
   yield (push @array1, $_);
   yield (push @array2, $_);
   yield (push @array3, $_);
   push @array4, $_;
   };

Except that Cyield is like Creturn and breaks out of the current
*subroutine*, not the current block.

Damian



Re: more yield tricks ((was Re: Cmap in RFC 31

2000-09-13 Thread Randal L. Schwartz

 "Damian" == Damian Conway [EMAIL PROTECTED] writes:

 foreach @bigarray {
 yield (push @array1, $_);
 yield (push @array2, $_);
 yield (push @array3, $_);
 push @array4, $_;
 };

Damian Except that Cyield is like Creturn and breaks out of the current
Damian *subroutine*, not the current block.

Damian Damian

Well, OK then

foreach @bigarray {
  sub {
yield (push @array1, $_);
yield (push @array2, $_);
yield (push @array3, $_);
push @array4, $_;
  }-();
};

:-)

Which of course won't work, unless there's a lot more magic going on.
(Which coderef gets the yield state attached to it, and would this properly
be recognized as a reason to clone the coderef?)

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
[EMAIL PROTECTED] URL:http://www.stonehenge.com/merlyn/
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!



Re: more yield tricks ((was Re: Cmap in RFC 31

2000-09-13 Thread Damian Conway

foreach @bigarray {
  sub {
yield (push @array1, $_);
yield (push @array2, $_);
yield (push @array3, $_);
push @array4, $_;
  }-();
};

:-)

As I've said before, Randal, you're a bad, bad man.

Of course, the following *would* work as desired:

 my $unmerge = sub {
yield  (push @array1, $_);
yield  (push @array2, $_);
yield  (push @array3, $_);
return (push @array4, $_);
};

foreach @bigarray {
  $unmerge-();
}

It's only drawback being that it's appalling. ;-)

Damian