Skip to content

Instantly share code, notes, and snippets.

@andrewsolomon
Created November 12, 2018 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewsolomon/7c17372f278d4540da1b69c88e4d0cf4 to your computer and use it in GitHub Desktop.
Save andrewsolomon/7c17372f278d4540da1b69c88e4d0cf4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use Modern::Perl;
use Data::Dump qw/pp/;
use Data::Rmap qw/rmap_hash/;
my $world = [
{
name => 'USA',
type => 'Country',
territories => [
{
name => 'Washington, D.C',
type => 'District',
streets => [
{
type => 'Avenue',
name => 'Pennsylvania',
properties => [
{
type => 'House',
colour => 'White',
inhabitants => [
{
name => 'Donald',
age => 3,
behaviour => 'naughty',
stocking => [],
},
],
},
],
},
],
},
],
},
{
name => 'Jungle',
type => 'Company',
staff => [
{
name => 'Jeff',
age => 7,
behaviour => 'naughty',
stocking => [],
role => 'CEO',
}
],
},
{
name => 'Monaco',
type => 'Tax Haven',
evaders => [
{
name => 'Ebenezer "Bono" Scrooge',
age => 12,
behaviour => 'nice',
stocking => [],
role => 'Philanthropist',
}
],
},
];
sub they_deserve_anything_at_all {
my $person = shift;
foreach my $key (qw/name age behaviour stocking/) {
return unless exists($person->{$key});
}
return unless ref($person->{stocking}) eq 'ARRAY';
return unless $person->{age} < 16;
return 1;
}
sub what_they_deserve {
my $person = shift;
return $person->{behaviour} eq 'nice' ? 'lollipop' : 'coal';
}
rmap_hash {
# Data::Rmap::cut if $_->{type} && $_->{type} =~ m{^(Company|Tax Haven)$};
push(@{$_->{stocking}}, what_they_deserve($_))
if they_deserve_anything_at_all($_)
} $world;
say pp($world);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment