Skip to content

Commit

Permalink
Literal array transpose with array (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper authored Dec 19, 2024
1 parent afcad66 commit c35c2eb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/literal/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,12 @@ def transpose
)
end
)
when Literal::Array::Generic
__with__(
@__value__.map(&:to_a).transpose.map! { |it| @__type__.new(*it) }
)
else
raise ArgumentError.new("Not implemented")
@__value__.transpose
end
end

Expand Down
41 changes: 40 additions & 1 deletion test/array.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,9 @@
end

test "#transpose with a nested literal tuple" do
array = Literal::Array(Literal::Tuple(Integer, String)).new(
array = Literal::Array(
Literal::Tuple(Integer, String)
).new(
Literal::Tuple(Integer, String).new(1, "a"),
Literal::Tuple(Integer, String).new(2, "b"),
)
Expand All @@ -774,3 +776,40 @@
Literal::Array(String).new("a", "b"),
)
end

test "#transpose with a nested literal array" do
array = Literal::Array(Literal::Array(Integer)).new(
Literal::Array(Integer).new(1, 2),
Literal::Array(Integer).new(3, 4),
)

assert_equal array.transpose, Literal::Array(
Literal::Array(Integer)
).new(
Literal::Array(Integer).new(1, 3),
Literal::Array(Integer).new(2, 4),
)
end

test "#transpose with a nested literal array with different lengths raises an IndexError" do
array = Literal::Array(Literal::Array(Integer)).new(
Literal::Array(Integer).new(1, 2),
Literal::Array(Integer).new(3, 4, 5),
)

assert_raises(IndexError) do
array.transpose
end
end

test "#transpose with a nested regular array" do
array = Literal::Array(_Array(Integer)).new(
[1, 2],
[3, 4],
)

assert_equal array.transpose, [
[1, 3],
[2, 4],
]
end

0 comments on commit c35c2eb

Please sign in to comment.