Skip to content

Instantly share code, notes, and snippets.

@thomasboyt
Created August 18, 2016 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasboyt/787644bf29f9be74102cd451bcf4937b to your computer and use it in GitHub Desktop.
Save thomasboyt/787644bf29f9be74102cd451bcf4937b to your computer and use it in GitHub Desktop.
why is js's implementation of reverse() so awful
> var a = [1,2,3]
undefined
> a.reverse()
[ 3, 2, 1 ]
> a
[ 3, 2, 1 ]
// [insert price is right losing horn here]
>>> 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]
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