Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2015/01/21

Perl Implementations of anyPass() and allPass()

allPass() takes an array of code refs containing tests, and returns a function which takes a value, runs the tests on that value, returns false if any test fails, and returns true after all the tests.

anyPass() takes an array of code refs containing tests, and returns a function which takes a value, runs the tests on that value, returns true if any test passes, and returns false after all the tests.

Thanks to Michael Hurley of Ramdas.js for letting me borrow his names and ideas, and for everyone who told me how to make things better.

sub allPass {
    return 0 if !@_ ;
    my @f = @_ ;
    return sub {
        for my $t ( @f ) {
            my $r = &$t( @_ ) ;
            return 0 unless $r >= 1;
            }
        return 1 ;
        }
    }

sub anyPass {
    return 0 if !@_ ;
    my @f = @_ ;
    return sub {
        for my $t ( @f ) {
            my $r = &$t( @_ ) ;
            return 1 if $r >= 1 ;
            }
        return 0 ;
        }
    }