Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Real life uses for closures. (update: disambiguation)

by LanX (Canon)
on Feb 12, 2013 at 03:30 UTC ( #1018284=note: print w/ replies, xml ) Need Help??


in reply to Real life uses for closures.

What are "delegates"? Talking about C#?

Most things which can be done with closures can be done with OOP and vice versa.

I normally prefer closures because it's light weighted and works in different languages alike.

just a very simple example of encapsulation.

{ my $var; sub getter { $var }; sub setter { $var = shift }; }

(EDIT: Applied in Perl OO one gets private class-variables in package scope instead of public package variables!)

In JS which "only" has prototype-kind of OO, it's possible to simulate many "classical" OO-models just by applying closure extensions. (That is having a "real" class as pattern to construct object instances.)

TIMTOWTDI.

Cheers Rolf

UPDATE

OK people, please lets get the terminology right:

Closures are NOT synonymous for

  • anonymous functions,
  • nor for currying
  • nor do they need to be produced dynamically by a generator function!

Closures are functions which access closed over variables from an (normally restricted) outer scope at definition time.

Look at the example I gave, getter and setter are closures, $var is a closed over lexical variable, and because of the surrounding curlies the variable is only accessible by those closure functions.

No currying, no anonymous functions no dynamic generation!!!

Most of you use them every day!!!

Whenever you define a file-scoped lexical variable then all functions within this file accessing this variable are closures!


Comment on Re: Real life uses for closures. (update: disambiguation)
Select or Download Code
Re^2: Real life uses for closures. (update: disambiguation)
by BrowserUk (Pope) on Feb 12, 2013 at 17:17 UTC
    What are "delegates"? Talking about C#?

    I don't know C#. My use of the term delegates comes from The D programming language and is best described as a pointer to a method.

    That is, it is a pointer to a function that carries an implicit reference to an object instance. Ie. Equivalent to this in perl:

    use Some::Class; my $inst = new Some::Class(); my $delegate = sub{ Some::Class::methodName( $inst, @_ ); };
    Closures are NOT synonymous for: ... nor for currying

    Agreed. But currying is one use for closures not easily achieved without them.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      So did you get enough RL examples of closures or did you mean to ask about functional programming in general?

      Cheers Rolf

        I've been reading a lot about constructing interpreters and VMs lately, and one thing that seems common to most new ones is the seemingly obligatory inclusion of closures; and the almost equally obligatory tales of woe, sleepless nights and extreme programming hardship that are required in order to implement them in an efficient and elegant manner.

        Then the thought that formed in my mind was that whilst I am quite happy to make extensive use of them, I was also quite happily solving my programming problems in languages that don't support them for 15+ years before I became acquainted with them.

        So then the questions that came to me were: a) how much use do people make of them in Perl? b) How many of those uses are actually 'killer applications' of closures, rather than trivial or convenient uses of up-values in anonymous subroutines that could just as easily be done using some other mechanism that doesn't impose such demands upon the VM?

        One of the 'killer applications' of closures often cited in FP circles, is their links to continuation passing. But continuation passing is just one method of solving a problem that can be solved in various other ways. And most of those other ways seem to impose far less runtime burdens than CP whilst achieving the same goals.

        Hence my very open question to try and get a feel for what people do with closures in real-world code.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
      Oh, I thought you meant "callback" by "delegate". They account for many closures.
Re^2: Real life uses for closures. (real life?)
by LanX (Canon) on Feb 12, 2013 at 17:45 UTC
    > (EDIT: Applied in Perl OO one gets private class-variables in package scope instead of public package variables!)

    lets elaborate, in the following code you can see a simplified standard Perl class with private and public variables and methods.

    The private data is only visible and accessible within the methods because of Perl's closure mechanism.

    { package TEST_CLASS; my $private_class_var; our $public_class_var; my $private_method = sub { my ($self,@args)=@_; print "-- private_method(@args)\n"; }; sub public_method { my ($self,@args)=@_; print "- public_method(@args)\n"; $self->$private_method(7..9); } sub new { my ($class,%args)=@_; bless \%args, $class; return \%args; } } my $obj = TEST_CLASS->new(a=>1,b=>2); $obj->public_method(1..3); $obj->private_method(4..6);

    prints

    - public_method(1 2 3) -- private_method(7 8 9) Can't locate object method "private_method" via package "TEST_CLASS" a +t /tmp/tst_pkg.pl line 32.

    So, does this count as real life example?

    Cheers Rolf

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (12)
As of 2014-10-21 17:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (106 votes), past polls