Created
January 7, 2015 21:45
-
-
Save jacoby/5527589e5b7e8e539b35 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use feature qw'say state' ; | |
use strict ; | |
use utf8 ; | |
use warnings ; | |
use Data::Dumper ; | |
# https://plus.google.com/u/0/+PaulBennett/posts/EvLLxiAMjKR | |
# http://ramdajs.com/docs/R.html | |
# SAMPLE CODE | |
# var gt10 = function(x) { return x > 10; }; | |
# var even = function(x) { return x % 2 === 0}; | |
# var f = R.allPredicates([gt10, even]); | |
# f(11); //=> false | |
# f(12); //=> true | |
my $gt10 = sub { my $i = shift ; return $i > 10 } ; | |
my $even = sub { my $i = shift ; return $i % 2 == 0 } ; | |
my $odd = sub { my $i = shift ; return $i % 2 == 1 } ; | |
my $test = sub { say Dumper \@_ } ; | |
# my $f = allPredicates( $test ) ; | |
my $f = allPredicates( $gt10 , $even ) ; | |
my $g = allPredicates( $gt10 , $odd ) ; | |
for my $i ( 9 .. 20 ) { | |
if ( &$f( $i ) ) { say $i . ' big and even' } | |
} | |
for my $i ( map { $_ * 3 } 1 .. 10 ) { | |
if ( &$g( $i ) ) { say $i . ' big and odd' } | |
} | |
# say &$gt10( 1 ); | |
# say &$gt10( 12 ); | |
# say &$even( 1 ); | |
# say &$even( 12 ); | |
# say &$odd( 1 ); | |
# say &$odd( 12 ); | |
sub allPredicates { | |
my @functions = @_ ; | |
my $function = sub { | |
my $val = shift ; | |
my @tests = @functions ; | |
my @results ; | |
for my $t ( @tests ) { | |
my $r = &$t( $val ) ; | |
push @results, $r if $r ; | |
} | |
return 1 if scalar @tests == scalar @results ; | |
return 0 ; | |
} ; | |
return $function ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment