home > 比較編 > Perl, PHP, Python, Ruby > functions >

ForNext

Only Do What Only You Can Do

152. closure

VBScript

JScript

Perl

sub make_counter {
  my $i = 0;
  return sub { ++$i };
}
my $nays = make_counter;
print $nays->() . "\n";




PHP

function make_counter() {
  $i = 0;
  return function () use (&$i) {
    return ++$i;
  };
}
$nays = make_counter();
echo $nays();


Python

# Python 3:
def make_counter():
  i = 0
  def counter():
    nonlocal i
    i += 1
    return i
  return counter

nays = make_counter()

Ruby

def make_counter
  i = 0
  return lambda { i +=1; i }
end
nays = make_counter
puts nays.call




PowerShell

Scala

F#

C

C++

C++Builder

VC++

C#

Java

Objective-C

D

VB

VB.NET

Delphi

Ada

PL/SQL

T-SQL

関数型

inserted by FC2 system