Created
March 26, 2010 12:12
-
-
Save tomohiro/344814 to your computer and use it in GitHub Desktop.
クロージャ/ラムダを Ruby,PHP5.2,PHP5.3 で使用した場合と使用しない場合の単純な比較
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
#!/usr/bin/env php | |
<?php | |
# for PHP 5.3 use "Closure" | |
$strtoupper = function($s) { return strtoupper($s); }; | |
$fruits = array('apple', 'orange', 'cherry'); | |
$stars = array('sun', 'moon', 'earch'); | |
echo implode('-', array_map($strtoupper, $fruits)) . "\r\n"; // APPLE-ORANGE-CHERRY | |
echo implode('-', array_map($strtoupper, $stars)) . "\r\n"; // SUN-MOON-EARCH |
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
#!/usr/bin/env php | |
<?php | |
# for PHP 5.2 use "create_funciton" | |
$strtoupper = create_function('$s', 'return strtoupper($s);'); | |
$fruits = array('apple', 'orange', 'cherry'); | |
$stars = array('sun', 'moon', 'earch'); | |
echo implode('-', array_map('strtoupper', $fruits)) . "\r\n"; // APPLE-ORANGE-CHERRY | |
echo implode('-', array_map('strtoupper', $stars)) . "\r\n"; // SUN-MOON-EARCH |
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
#!/usr/bin/env ruby | |
upcase = lambda { |s| s.upcase! } | |
fruits = ['apple', 'orange', 'cherry'] | |
stars = ['sun', 'moon', 'earth'] | |
p fruits.each(&upcase).join('-') # "APPLE-ORANGE-CHERRY" | |
p stars.each(&upcase).join('-') # "SUN-MOON-EARTH" |
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
#!/usr/bin/env php | |
<?php | |
function strtouppers($array) | |
{ | |
foreach($array as &$string) { | |
$string = strtoupper($string); | |
} | |
return $array; | |
} | |
$fruits = array('apple', 'orange', 'cherry'); | |
$stars = array('sun', 'moon', 'earch'); | |
echo implode('-', strtouppers($fruits)) . "\r\n"; // APPLE-ORANGE-CHERRY | |
echo implode('-', strtouppers($stars)) . "\r\n"; // SUN-MOON-EARCH |
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
#!/usr/bin/env ruby | |
def upcases(array) | |
array.each { |s| s.upcase! } | |
end | |
fruits = ['apple', 'orange', 'cherry'] | |
stars = ['sun', 'moon', 'earth'] | |
p upcases(fruits).join('-') # "APPLE-ORANGE-CHERRY" | |
p upcases(stars).join('-') # "SUN-MOON-EARTH" | |
# or | |
p fruits.each { |s| s.upcase } .join('-') # "APPLE-ORANGE-CHERRY" | |
p stars.each { |s| s.upcase } .join('-') # "SUN-MOON-EARTH" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment