- or download this
sub make_suffix_appender {
my $suffix = shift;
...
return $_[0] . $suffix;
}
}
- or download this
my $bang_appender = make_suffix_appender("!");
print $bang_appender->("Hello"); # Hello!
print $bang_appender->("Dude"); # Dude!
- or download this
sub apply {
my $fn = shift;
...
map { $fn->($_) } @_;
}
}
- 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)
- or download this
sub make_regex_matcher {
my $regex = shift;
...
/$regex/g;
}
}
- 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
- or download this
sub compose2 {
my ($f, $g) = @_;
...
no warnings qw( once );
reduce { compose2($a,$b) } @_;
}
- 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)
- or download this
print compose($parenify_each, $word_matcher)->("a b c");
# (a)(b)(c)
- or download this
print compose($word_matcher, $parenify_each)->("a b c");
# abc
- or download this
my $id = sub { wantarray ? @_ : $_[0] };
- or download this
my $fs = 0;
for my $f ($id, $parenify_all, $parenify_each) {
...
}
}
}
- 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)