-
-
Save mogetutu/2e69c675815dc5a5deb0b60985c7ccb2 to your computer and use it in GitHub Desktop.
Closure examples.
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
// This works on Rhino. | |
var counter = (function () { | |
var i = 1; | |
return function () { | |
return i++; | |
}; | |
})(); | |
print(counter()); // 1 | |
print(counter()); // 2 | |
print(counter()); // 3 | |
print(counter()); // 4 | |
print(counter()); // 5 |
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
<?php | |
$i = 1; | |
$counter = function () use (&$i) { | |
return $i++; | |
}; | |
echo $counter() . PHP_EOL; // 1 | |
echo $counter() . PHP_EOL; // 2 | |
echo $counter() . PHP_EOL; // 3 | |
echo $counter() . PHP_EOL; // 4 | |
echo $counter() . PHP_EOL; // 5 |
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
use strict; | |
use warnings; | |
use feature qw(say); | |
my $counter = sub { | |
my $i = 1; | |
return sub { | |
return $i++; | |
}; | |
}->(); | |
say $counter->(); # 1 | |
say $counter->(); # 2 | |
say $counter->(); # 3 | |
say $counter->(); # 4 | |
say $counter->(); # 5 |
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
# Not closure but generator. | |
def counter(): | |
i = 0 | |
while (True): | |
i += 1 | |
yield i | |
counter = counter() | |
print counter.next() # 1 | |
print counter.next() # 2 | |
print counter.next() # 3 | |
print counter.next() # 4 | |
print counter.next() # 5 |
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
counter = lambda do | |
i = 0 | |
lambda { i += 1 } | |
end.call | |
puts counter.call # 1 | |
puts counter.call # 2 | |
puts counter.call # 3 | |
puts counter.call # 4 | |
puts counter.call # 5 |
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
<?php | |
// This can't work because of parse error... | |
$counter = (function () { | |
$i = 0; | |
return function () use (&$i) { | |
return $i++; | |
}; | |
})(); | |
echo $counter() . PHP_EOL; | |
echo $counter() . PHP_EOL; | |
echo $counter() . PHP_EOL; | |
echo $counter() . PHP_EOL; | |
echo $counter() . PHP_EOL; |
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
<?php | |
class Counter | |
{ | |
protected $_i = 1; | |
public function __invoke() | |
{ | |
return $this->_i++; | |
} | |
} | |
$counter = new Counter; | |
echo $counter() . PHP_EOL; // 1 | |
echo $counter() . PHP_EOL; // 2 | |
echo $counter() . PHP_EOL; // 3 | |
echo $counter() . PHP_EOL; // 4 | |
echo $counter() . PHP_EOL; // 5 |
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
<?php | |
class Counter | |
{ | |
protected $_i = 1; | |
public function __toString() | |
{ | |
return (string) $this->_i++; | |
} | |
} | |
$counter = new Counter; | |
echo $counter . PHP_EOL; // 1 | |
echo $counter . PHP_EOL; // 2 | |
echo $counter . PHP_EOL; // 3 | |
echo $counter . PHP_EOL; // 4 | |
echo $counter . PHP_EOL; // 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment