use Test;
sub target-index(@array, Int $k) {
@array.sort.grep: $k, :k
}
is-deeply target-index((1, 5, 3, 2, 4, 2), 2), (1, 2);
is-deeply target-index((1, 5, 3, 2, 4, 2), 6), ();
is-deeply target-index((5, 3, 2, 4, 2, 1), 4), (4,);
sub merge-items(@items1, @items2) {
my %bag1 is Bag = @items1.map: { |(.head xx .tail) };
my %bag2 is Bag = @items2.map: { |(.head xx .tail) };
my %merged := %bag1 (+) %bag2;
[ |%merged.keys.sort.map: { [$_, %merged{$_}] } ]
}
is-deeply merge-items([[1,1], [2,1], [3,2]], [ [2,2], [1,3] ]), [ [1,4], [2,3], [3,2] ];
is-deeply merge-items([ [1,2], [2,3], [1,3], [3,2] ], [ [3,1], [1,3] ]),[ [1,8], [2,3], [3,3] ];
is-deeply merge-items([ [1,1], [2,2], [3,3] ], [ [2,3], [2,4] ]), [ [1,1], [2,9], [3,3] ];