Skip to content

Commit

Permalink
Test infix:<∘> and infix:<o> keep RHS's .arity and .count
Browse files Browse the repository at this point in the history
  • Loading branch information
zoffixznet committed Mar 9, 2017
1 parent 0ea6c6b commit 270dcf8
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion S03-operators/composition.t
Expand Up @@ -2,7 +2,7 @@ use v6;

use Test;

plan 10;
plan 12;

sub double ($x) { $x * 2 };
sub invert ($x) { 1 / $x };
Expand All @@ -23,3 +23,46 @@ is (* o *)(* + 1, * * 2)(3), 7, "can autocurry with Whatever on both sides";

is ((* + *) o { $_ + 7, $_ * 6 })(5), 42, "can compose functions that pass two arguments";
is ({ [+] @_ } o *.map(* * 2))(1..10), 110, "can compose functions that pass multiple arguments";

# RT #130891
{
subtest 'infix:<∘> preserves .count and .arity of RHS' => {
plan 4;
my &one = sub { $^a.uc } ∘ sub { "$^a:$^b" };
is-deeply <a b c d e f g h>.map(&one).List,
("A:B", "C:D", "E:F", "G:H"),
'can 2-at-a-time map with a composed routine';

my &two = sub { $^a.flip } ∘ &one;
is-deeply <a b c d e f g h>.map(&two).List,
("B:A", "D:C", "F:E", "H:G"),
'can 2-at-a-time map with a double-composed routine';

my &grepper = sub { $^a.not } ∘ sub { ($^a + $^b) %% 2 }
is-deeply (1, 1, 2, 3, 4, 5).grep(&grepper).List, ((2, 3), (4, 5)),
'can 2-at-a-time grep with a composed routine';

is-deeply ([∘] {$_ xx 2} xx 2)(3).List, ((3, 3), (3, 3)),
'Can use infix:<∘> as a meta';
}

subtest 'infix:<o> preserves .count and .arity of RHS' => {
plan 4;
my &one = sub { $^a.uc } o sub { "$^a:$^b" };
is-deeply <a b c d e f g h>.map(&one).List,
("A:B", "C:D", "E:F", "G:H"),
'can 2-at-a-time map with a composed routine';

my &two = sub { $^a.flip } o &one;
is-deeply <a b c d e f g h>.map(&two).List,
("B:A", "D:C", "F:E", "H:G"),
'can 2-at-a-time map with a double-composed routine';

my &grepper = sub { $^a.not } o sub { ($^a + $^b) %% 2 }
is-deeply (1, 1, 2, 3, 4, 5).grep(&grepper).List, ((2, 3), (4, 5)),
'can 2-at-a-time grep with a composed routine';

is-deeply ([o] {$_ xx 2} xx 2)(3).List, ((3, 3), (3, 3)),
'Can use infix:<o> as a meta';
}
}

0 comments on commit 270dcf8

Please sign in to comment.