Created
February 6, 2017 13:46
-
-
Save keithbro/dc2e64092c9ffd5c1078e5894c429010 to your computer and use it in GitHub Desktop.
transduce.t
This file contains hidden or 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
use Test::Most; | |
use Yoda qw(append compose divide filter flip take transduce); | |
my $is_even = sub { $_[0] % 2 == 0 }; | |
my $dividend = 100; | |
my $divisors = [ -25, -20, -10, -5, -2, 0 ]; | |
throws_ok( | |
sub { Yoda::map(divide($dividend), $divisors) }, | |
qr/Illegal division by zero/, | |
'dies when dividing 100 by 0', | |
); | |
my $transducer = compose( | |
Yoda::map(divide($dividend)), | |
filter($is_even), | |
take(3), | |
); | |
# map(divide()) => [ -4 ] => [ -4, -5 ] => [ -4, -10 ] => [ -4, -10, -20 ] | |
# filter() => [ -4 ] => [ -4 ] => [ -4, -10 ] => [ -4, -10, -20 ] | |
# take(3) => [ -4 ] => [ -4 ] => [ -4, -10 ] => [ -4, -10, -20 ] | |
eq_or_diff( | |
transduce($transducer, flip(append()), [], $divisors), | |
[ -4, -10, -20 ], | |
'transduced divisors left to right', | |
); | |
done_testing; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment