Preface
Goal: Continue Part One
4: Map And Grep
Almost List Comprehension.
Raku has very similar feature with perl5.
It natively has support for map, and filtering with grep.
But further, raku support list comprehension.
Simple Example
Consider examine map, applying to our songs records.
my @songs_title = @songs.map: *<title>;
say join(':', @songs_title);- With the result as below:
 
Cantaloupe Island:Let It Be:Knockin' on Heaven's Door:Emotion:The RiverAnd then go further with grep.
my @songs_tags = (@songs
  .grep: *.keys.grep(/tags/))
  .map: *<tags>;
for (@songs_tags) {
  say join(":", @$_);
}- With the result as below:
 
60s:jazz
60s:rock
70s:rock
70s:popwe can examine the object shown in output result above.

Unique
With just a little modification, we can flatten the array using built in function, and also get the unique array using built in function.
use MySongs;
my @songs_tags = ((@songs
  .grep: 'tags' ∈ *.keys)
  .map: *<tags>);
my @tags = @songs_tags.List.flat.unique;
join(":", @tags).say;- With the result similar as below record:
 
❯ ./13-unique.raku
60s:jazz:rock:70s:pop
5: List Comprehension
The True One
As I said before, raku support list comprehension.
Simple Example
Consider examine map, applying to our songs records.
my @songs_title =
  ( $_<title> for @songs );
say join(':', @songs_title);- With the result as below:
 
Cantaloupe Island:Let It Be:Knockin' on Heaven's Door:Emotion:The RiverAnd then go further with grep.
my @songs_tags = (
  $_<tags>
  if $_.keys.grep(/tags/)
  for @songs
);
for (@songs_tags) {
  say join(":", @$_);
}- With the result as below:
 
60s:jazz
60s:rock
70s:rock
70s:popwe can examine the object shown in output result above.

Unique
With just a little modification, we can flatten the array using built in function, and also get the unique array using built in function.
use MySongs;
my @songs_tags = (
  $_<tags>
  if 'tags' ∈ $_.keys
  for @songs
);
my @tags = @songs_tags
  .List.flat.unique;
join(":", @tags).say;- With the result similar as below record:
 
❯ ./17-unique.raku
60s:jazz:rock:70s:pop
Preview
As a summary, we can compose all in just one script.
#!/usr/bin/raku
my @songs =
  { title => 'Cantaloupe Island',
    tags  => ('60s', 'jazz') },
  { title => 'Let It Be',
    tags  => ('60s', 'rock') },
  { title => 'Knockin\' on Heaven\'s Door',
    tags  => ('70s', 'rock') },
  { title => 'Emotion',
    tags  => ('70s', 'pop') },
  { title => 'The River'}
;
my @songs_tags = (
  $_<tags>
  if 'tags' ∈ $_.keys
  for @songs
);
my @tags = @songs_tags
  .List.flat.unique;
join(":", @tags).say;
What is Next 🤔?
We have alternative way to do get unique list. We will also discuss about concurrency.
Consider continue reading [ Raku - Playing with Records - Part Three ].