Last active
January 16, 2019 20:26
-
-
Save skids/7cc60e21ddcb3dfad1e119af236cc1d2 to your computer and use it in GitHub Desktop.
Neat Perl6 DRY idiom for unpacking named data into locally scoped variables of same name.
This file contains hidden or 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
# Neat Perl6 DRY idiom | |
# | |
# suppose you want to work with named values in a string | |
# or data structure, and you want to keep the variable | |
# names the same as the field/key names. | |
# sample data using a hash just to kep things simple | |
my %data = :fee(42), :fie<FIE>, :foo<FOO>, :fum(True); | |
# DRY idiom: | |
for my ($fee, $fie, $foo, $fum) { | |
my $fieldname = $_.VAR.name.substr(1,*); | |
$_ = %data{$fieldname}; # Or whatever to unpack them from a string | |
} | |
say "The fee for this is $fee dollars"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For something even DRYer, add it as a function or an operator.