Corso di Raku / Avanzato / Altro sui tipi predefiniti / Set, bag e mix / Exercises / Contare in un bag

Soluzione: Contare in un bag

Ecco una possibile soluzione.

Codice

my $words = bag('the cat sat on the mat'.words);

say $words<the>;
say $words.total;

🦋 Puoi trovare il codice sorgente nel file bag-count.raku.

Output

2
6

Commenti

  1. 'the cat sat on the mat'.words produce le sei parole e bag conta ciascuna di esse. Il subscript <the> restituisce quante volte the è stato trovato — due.

  2. Il metodo total somma tutti i conteggi, che corrisponde al numero di parole inserite nel bag — 6.

Contare in un bag   |   Sequenze