3 ms·
I've always thought the func block list syntax in Perl [1] also had a strong influence on Ruby's blocks. Here's an example of map in both languages: # ruby
by draegtun 14y ago
I've always thought the func block list syntax in Perl [1] also had a strong influence on Ruby's blocks. Here's an example of map in both languages:
# ruby
double = [1..10].map { |x| x * 2 }
# perl
my @double = map { $_ * 2 } 1..10;
NB. For more examples of the func block list syntax in Perl see grep and List::Util / List::MoreUtils modules.
And it's easy create new ones as well:
sub loop (&) {
my $block = shift;
$block->() while 1;
}
loop { say "Nuts" };
And you can also chain them together to build more complex constructs:
sub ifTrue (&@) {
my ($block, $cond) = @_;
$block->() if $cond;
$cond;
}
sub ifFalse (&@) {
my ($block, $cond) = @_;
$block->() unless $cond;
$cond;
}
ifTrue { say "True" } ifFalse { say "False" } 1 == 1;
I once wrote a lazy functional syntax in Perl using this approach :)
[1] - Not sure what the proper name for this syntax is!