Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Perl 6 and Ruby on Rails

by rg0now (Chaplain)
on Apr 05, 2005 at 13:23 UTC ( [id://444976]=note: print w/replies, xml ) Need Help??


in reply to Perl 6 and Ruby on Rails

Here is my strike at "Passing code blocks around as macros":
macro statement_control:<my_while>($expr, &whileblock) { while $expr &whileblock(); # do we need while $expr do &whileblock(); ? } my $i = 0; my_while {$i < 10} { say "i: $i"; my $j = 0; my_while {$j < 10} { say "j: $j"; $j++; } $i++; }
I think you could do that without the macro-magic with simple subs like
sub my_while(Code &expr, Code &whileblock) { while &expr() &whileblock(); }
This is because (from S4):
Every block is a closure. (That is, in the abstract, they're all anonymous subroutines that take a snapshot of their lexical scope.) How any block is invoked and how its results are used is a matter of context, but closures all work the same on the inside.

Digging around the Perl 6 specs, I begin to see that perhaps S4 holds some of the nicest things in the design of Perl 6. And, quite frankly, the more I read the more I feel how much I miss a working Perl 6 compiler...

Update: hey, we can, with a slightly more convoluted syntax, do this with Perl 5 too:

use strict; use warnings; sub my_while(&&){ my($expr, $whileblock) = @_; while(&$expr()){ &$whileblock(); } } my $i = 0; my_while sub {$i < 10}, sub { print "i: $i\n"; my $j = 0; my_while sub {$j < 10}, sub { print "j: $j\n"; $j++; }; $i++; };
Then, what's the great deal?

rg0now

Replies are listed 'Best First'.
Re^2: Perl 6 and Ruby on Rails
by duff (Parson) on Apr 05, 2005 at 18:09 UTC

    the more I read the more I feel how much I miss a working Perl 6 compiler

    What's pugs? Chopped liver? :-)

      Now guess what! I managed to make the damned thing work in Pugs! Here is the slightly modified version that compiles and runs with current Pugs:
      use v6; sub my_while(Code $expr, Code $whileblock) { while ($expr()) { $whileblock() }; } my $i = 0; my_while {$i < 10}, { say "i: $i"; my $j = 0; my_while {$j < 10}, { say "j: $j"; $j++; }; $i++; };
      What really surprized me was the fact that Pugs appears to do the right thing on closing closures, which I did not think should work right now. Also, do not really expect Pugs to verify whether my_while is called with actual blocks or not, because it does not work. Moreover, it is astonishingly slow. But I keep on loving it...:-)

      rg0now

Re^2: Perl 6 and Ruby on Rails
by dragonchild (Archbishop) on Apr 05, 2005 at 16:23 UTC
    hey, we can, with a slightly mlre convoluted syntax, do this with Perl 5 too:

    That's how Error works, and it has memory-leak issues. :-)

Re^2: Perl 6 and Ruby on Rails
by jdporter (Paladin) on Apr 05, 2005 at 19:02 UTC
    It doesn't need to be quite as bad as that... though still, bad enough:
    my_while { $i < 10 } sub { print "i: $i\n"; my $j = 0; my_while { $j < 10 } sub { print "j: $j\n"; $j++; }; $i++; };
Re^2: Perl 6 and Ruby on Rails
by mattr (Curate) on Apr 05, 2005 at 17:34 UTC
    Hmm. Definitely I will do some more reading. Thanks for the experimentation.

    I was thinking about a way to state business rules for a system I'm building now, but to be honest I always wondered when we would get to the point that programming looked more like talking to the "Smart Girl" spaceship Gay Deceiver in Robert A. Heinlein's The Number of the Beast. Between Cyc and Perl6 we seem to be getting closer..

    "Program, Gay. Add running news retrieval. Area, Arizona Strip north of Grand Canyon plus Utah . Persons: all persons listed in current running news retrieval programs plus rangers, Federal rangers, forest rangers, park rangers, state rangers. End of added program."

    The above story uses a verbally programmed autopilot a bit smarter than what we have now, but English constructs like verb-object-prepositional_phrase-conditional_clause might be possible with Perl 6.. Anyway thank you very much and I will certainly do some more reading on it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://444976]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-19 22:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found