Skip to content

Instantly share code, notes, and snippets.

@keithbro
Created February 6, 2017 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keithbro/dc2e64092c9ffd5c1078e5894c429010 to your computer and use it in GitHub Desktop.
Save keithbro/dc2e64092c9ffd5c1078e5894c429010 to your computer and use it in GitHub Desktop.
transduce.t
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