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

Re: Ways to implement a closure

by tmoertel (Chaplain)
on Oct 15, 2004 at 21:07 UTC ( #399664=note: print w/ replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
        sub make_suffix_appender {
            my $suffix = shift;
    ...
                return $_[0] . $suffix;
            }
        }
    
  2. or download this
        my $bang_appender = make_suffix_appender("!");
        print $bang_appender->("Hello");  # Hello!
        print $bang_appender->("Dude");   # Dude!
    
  3. or download this
        sub apply {
            my $fn = shift;
    ...
                map { $fn->($_) } @_;
            }
        }
    
  4. or download this
        my $upcase = apply( sub { uc("@_") } );
        print $upcase->("Hello");           # HELLO
    ...
        my $parenify_each = apply_to_each( $parenify_all );
        print $parenify_all ->("Hello", "world");  # (Hello world)
        print $parenify_each->("Hello", "world");  # (Hello)(world)
    
  5. or download this
        sub make_regex_matcher {
            my $regex = shift;
    ...
                /$regex/g;
            }
        }
    
  6. or download this
        my $digit_matcher = make_regex_matcher( qr/(\d+)/ );
        my @digits = $digit_matcher->( "123-blah-45-6789" );
    ...
        my $word_matcher = make_regex_matcher( qr/(\w+)/ );
        my @words = $word_matcher->( "123-blah-45-6789" );
        print "@words";  # 123 blah 45 6789
    
  7. or download this
        sub compose2 {
            my ($f, $g) = @_;
    ...
            no warnings qw( once );
            reduce { compose2($a,$b) } @_;
        }
    
  8. or download this
        my $up_banger = compose($bang_appender, $upcase);
        print $up_banger->("Hello");  # HELLO!
    ...
        my $parenify_words = compose($parenify_each, $word_matcher);
        print $parenify_words->( "123-blah-45-6789" );
            # (123)(blah)(45)(6789)
    
  9. or download this
        print compose($parenify_each, $word_matcher)->("a b c");
            # (a)(b)(c)
    
  10. or download this
        print compose($word_matcher, $parenify_each)->("a b c");
            # abc
    
  11. or download this
        my $id = sub { wantarray ? @_ : $_[0] };
    
  12. or download this
        my $fs = 0;
        for my $f ($id, $parenify_all, $parenify_each) {
    ...
                }
            }
        }
    
  13. or download this
        id--id--id-: Eat more 3.14
        id--id--up-: EAT MORE 3.14
    ...
        pEa-dig-id-: (3)(14)
        pEa-dig-up-: (3)(14)
        pEa-dig-bng: (3)(14)
    

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2014-12-25 16:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls