Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re (tilly) 1: The zen of code blocks

by tilly (Archbishop)
on Aug 23, 2001 at 16:08 UTC ( [id://107295]=note: print w/replies, xml ) Need Help??


in reply to The zen of code blocks

sub zen { my $sub = shift; &$sub; } { my $answer = "Use a closure\n"; zen(sub{print $answer;}); }

Replies are listed 'Best First'.
Re: Re (tilly) 1: The zen of code blocks
by mdupont (Scribe) on Aug 23, 2001 at 18:18 UTC
    #
    # a perl koan designed for a true perl monk use warnings; use strict; my $this; # do I need this and that? my $that; # tell me oh perl monks! # a structured way of thinking, # the params is a way to pass the parameters # and the thought is what to do with them sub koan { my $params = shift; # the code ref is a parameter # that defines the parameters my $thought = shift; # the code ref that # defines the work return sub { &$params(@_); # Get the parameters, # from @_ and make them # available in thought &$thought; # Do the work using these parameters } }; # create a closure to think about the thought sub zen { my $thought = shift; my @params = @_; return sub { &$thought(@params); # call with the saved params }; }; sub integer_parm # called to get a simple integer { my $val= shift; # the value passes my $type = ref $val; # get the type of param return $val if(!$type); # return simple values if ($type eq "CODE") { $val = &$val; # eval the parameter } else { die "no refs in this"; # dont allow pointers } die unless $val =~ /^\d+$/; # the value of the param must be an integer return $val; } ############################################### # different thoughts to meditate on # types of thoughts my $binary_thought = sub { $this = shift; # now I want to have # this available in koan # and to the thought, $that = shift; # but not to be overwritten # if called recursivly $this=integer_parm($this); $that=integer_parm($that); }; my $add_func = sub { $this + $that; }; my $minus_func = sub { $this - $that; }; # different thoughts my $add_koan = koan($binary_thought,$add_func); my $minus_koan = koan($binary_thought,$minus_func); ## SOME EXAMPLES print &$add_koan(1,1); # direct thought my $thought = zen($add_koan,1,2); # indirect thought print &$thought . "\n"; print &$add_koan(1,$thought) . "\n"; # tree of thoughts #mdupont #
Re: Re (tilly) 1: The zen of code blocks
by graq (Curate) on Aug 23, 2001 at 17:20 UTC
    LMAO!
    I have no idea if it was intended to be funny at all, but I think that it was a brilliant question and a brilliant answer. It really made me laugh (in a happy way, not derogative at all). What can I say: ++ guys :)

    --
    Graq

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-23 17:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found