Created
August 18, 2016 01:32
-
-
Save thomasboyt/787644bf29f9be74102cd451bcf4937b to your computer and use it in GitHub Desktop.
why is js's implementation of reverse() so awful
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
> var a = [1,2,3] | |
undefined | |
> a.reverse() | |
[ 3, 2, 1 ] | |
> a | |
[ 3, 2, 1 ] | |
// [insert price is right losing horn here] |
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
>>> arr = [1, 2, 3] | |
>>> ret = arr.reverse() # mutates arr | |
>>> print ret | |
None | |
>>> arr | |
[3, 2, 1] | |
>>> arr = [1, 2, 3] | |
>>> reversed(arr) # does not mutate arr | |
<listreverseiterator object at 0x101763610> | |
>>> [x for x in reversed(arr)] | |
[3, 2, 1] | |
>>> arr | |
[1, 2, 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
irb(main):001:0> a = [1,2,3] | |
=> [1, 2, 3] | |
irb(main):002:0> a.reverse | |
=> [3, 2, 1] | |
irb(main):003:0> a | |
=> [1, 2, 3] | |
irb(main):004:0> a.reverse! | |
=> [3, 2, 1] | |
irb(main):005:0> a | |
=> [3, 2, 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment