Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Local for lexicals

by JadeNB (Chaplain)
on Aug 10, 2009 at 23:06 UTC ( [id://787432]=note: print w/replies, xml ) Need Help??


in reply to Re: Local for lexicals
in thread Local for lexicals

Of course, in Perl, one doesn't really need "lambda". Hence a lambda which just returns the sub.
Yes, I agree, if one is willing to set the variables ‘outside’—but I'd like to set them on the function call, so that I can write
my $f = lambda $x => sub { $x**2 }; say $f->(2); # => 4 say $f->(3); # => 9
(without affecting the value of $x). Of course you're right that Perl doesn't need lambda, in the sense that the imaginary code above functions exactly the same as the real code
my $f = sub { $_[0]**2 }; say $f->(2); # => 4 say $f->(3); # => 9
I just wanted to see if it could be done. (I have other examples that aren't just syntactic sugar, but the margin is too small to contain them they're not pretty.)

Replies are listed 'Best First'.
Re^3: Local for lexicals
by JavaFan (Canon) on Aug 10, 2009 at 23:33 UTC
    Seems you just want a complicated way of not writing $_[0] in the sub. That can be done of course. But why would you? And what has this to do with localizing a lexical, which you were asking about in your earlier posts? If you're passing in the parameter to the sub, there's no need to localize anything, is there?
    my $f = sub {my $x = shift; sub {$x ** 2}->()}; say $f->(2); # => 4 say $f->(3); # => 9
      because $_[0] is an alias and $x is only a copy, he wants $x to be an alias of the first arg!

      see explanation of second code example in Re^3: Local for lexicals

      JadeNB should really post better test cases to avoid confusion!

      Cheers Rolf

      Seems you just want a complicated way of not writing $_[0] in the sub. That can be done of course. But why would you? And what has this to do with localizing a lexical, which you were asking about in your earlier posts? If you're passing in the parameter to the sub, there's no need to localize anything, is there?
      In order:
      1. Yes, that's right (for the lambda example).
      2. Skipped.
      3. Why not?
      4. Only that localising a lexical was my first thought for how to implement it. (Example code is at Local for lexicals.) Once I thought of the problem in that context, it immediately seemed to crop up many other places.
      5. You are certainly right that there are ways to do it without localisation. If I write out the sub—as you've done here:
        my $f = sub {my $x = shift; sub {$x ** 2}->()};
        (formatted, I think, specifically to be faithful to my original attempt), or even more briefly as just:
        my $f = sub { my $x = shift; $x**2 }
        then it certainly works. I would just like to avoid the slight extra verbiage and write instead
        my $f = lambda $x => sub { $x**2 }
        as syntactic sugar (well, for some definition of ‘sugar’) for the same thing.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-18 05:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found