Skip to content

Instantly share code, notes, and snippets.

@mklbtz
Created August 12, 2016 20:59
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 mklbtz/d2c5abb0b5be5c980d4486595ade0256 to your computer and use it in GitHub Desktop.
Save mklbtz/d2c5abb0b5be5c980d4486595ade0256 to your computer and use it in GitHub Desktop.
# O(nk)
class Array
def unzip ary
return [] if ary.empty?
count = ary.first.count
(0...count).map { |i|
ary.map { |tuple|
raise 'inconsistent tuple size' if tuple.count != count
tuple[i]
}
}
end
end
zipped = [[:a, 1], [:b, 2]]
zipped.unzip
#=> [[a, b], [1, 2]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment