diff --git a/lib/literal/array.rb b/lib/literal/array.rb index 8acdc77..de85a23 100644 --- a/lib/literal/array.rb +++ b/lib/literal/array.rb @@ -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 diff --git a/test/array.test.rb b/test/array.test.rb index bca6149..bac1639 100644 --- a/test/array.test.rb +++ b/test/array.test.rb @@ -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"), ) @@ -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