Created
January 23, 2010 02:13
-
-
Save tommorris/284380 to your computer and use it in GitHub Desktop.
map/filter an array: spot the odd one out
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
[1, 2, 3, 4, 5].map {|i| i * 2 }.find_all {|i| i > 5 } | |
# I have started monkey-patching `alias_method :filter, :find_all` in Array. |
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
List(1, 2, 3, 4, 5).map(_ * 2).filter(_ > 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 | |
array_filter( | |
array_map( function($x) { return $x * 2; }, array(1, 2, 3, 4, 5)), | |
function($x) { if ($x > 5) { return true; } else { return false; } } | |
); | |
/* Here's the result: | |
Array | |
( | |
[2] => 6 | |
[3] => 8 | |
[4] => 10 | |
) | |
What's going on here? Oh yeah, it's called PHP being shit. Having mapped and filtered | |
our array, it now returns us an array with all the key values being wrong. To access | |
the first value of this new array, instead of using [0], one must now use [2]. | |
Gotta love the lack of distinction between associative arrays and lists in this | |
failure of a language. | |
Still, good on the PHP team for adding closures to PHP 5.3. */ | |
?> |
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
[x for x in [x * 2 for x in 1, 2, 3, 4, 5] if x > 5] | |
# Clever but fugly. Not at all Zen-like. Oh well. | |
# You can write Perlisms in Python if you use | |
# enough nested list comprehensions... ;) | |
# | |
# See: | |
# http://wordaligned.org/articles/are-list-comprehensions-the-wrong-way-round |
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
(filter (fn [x] (> x 5)) | |
(map (fn [x] (* x 2)) (list 1 2 3 4 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
[1, 2, 3, 4, 5].map(function(x) { return x * 2; }).filter(function(x) { return (x > 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
new[] {1, 2, 3, 4, 5}.Select(x => x * 2).Where(x => x > 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
grep { $_ > 5 } map { $_ * 2 } ( 1 .. 5 ); | |
# thanks to @dorward for this. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment