Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#transpose #222

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/literal/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,14 @@ def to_a

alias_method :to_ary, :to_a

def transpose
if @__type__ == Array || Literal::Array == @__type__
__with__(@__value__.transpose)
else
raise ArgumentError.new("#transpose expects Array type")
end
end

def uniq
__with__(@__value__.uniq)
end
Expand Down
19 changes: 19 additions & 0 deletions test/array.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,25 @@
expect { array.replace("not an array") }.to_raise(ArgumentError)
end

test "#transpose raises if type is not Array" do
array = Literal::Array(Integer).new(1, 2, 3)

expect { array.transpose }.to_raise(ArgumentError)
end

test "#transpose returns a new array with rows and columns transposed" do
array = Literal::Array(Array).new([1, 2], [3, 4], [5, 6])

transposed = array.transpose
expect(transposed.to_a) == [[1, 3, 5], [2, 4, 6]]
end

test "#transpose raises if the rows are not the same length" do
array = Literal::Array(Array).new([1, 2], [3, 4, 5])

expect { array.transpose }.to_raise(IndexError)
end

test "#values_at returns the values at the given indexes" do
array = Literal::Array(Integer).new(1, 2, 3)

Expand Down
Loading