Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Near-free function currying in Perl

by tmoertel (Chaplain)
on Nov 17, 2004 at 02:50 UTC ( #408304=perlmeditation: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
        my $curried_foo = curry( \&foo, args... );
    
  2. or download this
        sub log_to_handle {
            my ($fh, $heading, $message) = @_;
            print $fh "$heading: $message", $/;
        }
    
  3. or download this
        my $app_server = AppServer->new( logger => $mylogger, ... );
    
  4. or download this
        sub app_server_diagnostic_logger {
            log_to_handle( *STDERR, "app server", @_ );
    ...
        $app_server = AppServer->new(
            logger => \&app_server_diagnostic_logger, ... 
        );
    
  5. or download this
        $app_server = AppServer->new(
            logger => sub {
                log_to_handle( *STDERR, "app-server", @_ )
            }, ... 
        );
    
  6. or download this
        $app_server = AppServer->new(
            logger => curry( \&log_to_handle, *STDERR, "app-server" ),
            ...
        );
    
  7. or download this
        $app_server = AppServer->new(
            logger => log_to_handle( *STDERR, "app-server" ),
            ...
        );
    
  8. or download this
        $app_server = AppServer->new(
            logger => log_to_handle_c( *STDERR, "app-server" ),
            ...
        );
    
  9. or download this
        sub curry {
            my $f = shift;
            my $args = \@_;
            sub { $f->(@$args, @_) };
        }
    
  10. or download this
        *log_to_handle_c = curry( \&curry, \&log_to_handle );
    
  11. or download this
        sub get_function_names_from_package {
            no strict 'refs';
    ...
                grep !/^_|^[_A-Z]$/, # drop _underscored & ALL_CAPS
                keys %$symtab;       # get all symbols for package
        }
    
  12. or download this
        {   package Test;
            sub one { }
    ...
        my @names = get_function_names_from_package("Test");
        print "@names", $/;
        # Test::three Test::one Test::two
    
  13. or download this
        for (@names) {
            no strict 'refs';
            my $curried_name = $_ . "_c";
            *$curried_name = curry( \&curry, \&$_ );
        }
    
  14. or download this
        use AutoCurry qw( foo );  # pass ':all' to curry all functions
    
        sub foo { print "@_$/"; }
        # currying variant, foo_c, is created automatically
    
  15. or download this
    package AutoCurry;
    
    ...
    modify it under the same terms as Perl itself.
    
    =cut
    

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://408304]
Approved by ysth
Front-paged by diotalevi
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (10)
As of 2020-11-24 21:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?