Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^5: RFC: Simulating Ruby's "yield" and "blocks" in Perl (Python)

by MonkOfAnotherSect (Sexton)
on Apr 26, 2013 at 10:40 UTC ( #1030806=note: print w/replies, xml ) Need Help??


in reply to Re^4: RFC: Simulating Ruby's "yield" and "blocks" in Perl (Python)
in thread RFC: Simulating Ruby's "yield" and "blocks" in Perl

v3 is current, though str.format() was added in late v2 releases for compatibility (printf style interpolation is to be deprecated some time but I'll use it below as an example...). Here's the decorated version as suggested:
Edit: trimmed some code
def rubyyielder(gen): def wrapped_gen(block): for elem in gen(): block(elem) return wrapped_gen @rubyyielder def test(): print("In test") yield 1 print("back in test") yield 2 @test def _(a): print("You are in block %s" % a)
I don't really see a win with this style, though, over giving test a parameter, and just using standard function calls (I had a little play with coroutines on the way but that's really overkill for this).
  • Comment on Re^5: RFC: Simulating Ruby's "yield" and "blocks" in Perl (Python)
  • Download Code

Replies are listed 'Best First'.
Re^6: RFC: Simulating Ruby's "yield" and "blocks" in Perl (Python)
by LanX (Sage) on Apr 26, 2013 at 14:00 UTC
    > I don't really see a win with this style, though, over giving test a parameter, and just using standard function calls (I had a little play with coroutines on the way but that's really overkill for this).

    Well I expected decorators to be more flexible, i.e. to redefine test() by putting a wrapper around it and not to built a new decorator @test (!?!)

    But I'm not too proficient with Python, and I'm misunderstanding the code.

    (well, there must be a reason why I prefer Perl's syntactic flexibility. ;-)

    I suppose def _(a) is a way to workaround the limitations of lamda?

    Is the name _ special (magic) or is it just a convention?

    Cheers Rolf

    ( addicted to the Perl Programming Language)

        Thanx, it's clear now.

        I was just surprised that you need two decorators!

        I thought you could simply do something like

         test(def _(x): print("You are in block %s" % x))

        (like  test sub { print("You are in block $_[0]") } in Perl)

        Just to get closer to the Ruby feeling and to achieve this

        >>> def _(x): ... print("You are in block %s" % x) ... >>> test(_) In test You are in block 1 back in test You are in block 2 test lambda a: print("You are in block %s" % a)

        But one needs decorators to manipulate a literal function.

        Obviously, there is only this way to do it.

        FWIW Python decorators could quite easily be simulated in Perl with attributes.

        I will post this soon (if I can't find it already on CPAN) =)

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2022-08-17 20:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?