Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the item with the biggest value in a Bag collection in Raku

Tags:

raku

my $coll=bag <1 2 2 3 2 4 4 2 2>;
say $coll; # => Bag(1, 2(5), 3, 4(2))

How to get the item (key) with the biggest value, i.e. the 2(5) from this Bag collection?

(Optional) Is there a way to tell the Bag to count only those elements which are successive and disregard all which are not successive so that the result would be Bag(2(4), 4(2)) ?

like image 791
Lars Malmsteen Avatar asked Apr 14 '20 18:04

Lars Malmsteen


1 Answers

To answer your first question, there's a method for that:

say $coll.maxpairs; # 2 => 5

To answer your second question: no, you cannot. You can only devise some logic that will pre-process the values before getting to the Bag.

 <1 2 2 3 2 4 4 2 2>.map( *somelogic* ).Bag

Leaving the *somelogic* part as an exercise to the reader.

like image 56
Elizabeth Mattijsen Avatar answered Nov 03 '22 02:11

Elizabeth Mattijsen