News for hackers. Stuff that matters.
Re:How Ruby Borrowed a Decades Old Idea From Lisp by draegtun on Tuesday September 18, 2012 @12:04PM (#4538825)
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!

There are no comments on this comment yet.