-
-
Save andrewsolomon/7c17372f278d4540da1b69c88e4d0cf4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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