diff --git a/lib/commands/list_commands.ex b/lib/commands/list_commands.ex index 36f8f18..fc2d3cb 100644 --- a/lib/commands/list_commands.ex +++ b/lib/commands/list_commands.ex @@ -326,19 +326,19 @@ defmodule Commands.ListCommands do def rotate(value, shift) when shift == 0, do: value def rotate(value, shift) when not Functions.is_iterable(value), do: Enum.join(rotate(String.graphemes(to_string(value)), shift), "") def rotate(value, shift) when shift > 0 do - case length(Enum.to_list value) do + case Enum.count value do 0 -> [] x -> shift = rem(shift, x) - Stream.concat(value |> Stream.drop(shift), value |> Stream.take(shift)) |> Stream.map(fn x -> x end) + Stream.concat(value |> Stream.drop(shift), value |> Stream.take(shift)) |> Enum.to_list end end def rotate(value, shift) when shift < 0 do - case length(Enum.to_list value) do + case Enum.count value do 0 -> [] x -> shift = rem(shift, x) - Stream.concat(value |> Stream.take(shift), value |> Stream.drop(shift)) |> Stream.map(fn x -> x end) + Stream.concat(value |> Stream.take(shift), value |> Stream.drop(shift)) |> Enum.to_list end end diff --git a/test/commands/special_test.exs b/test/commands/special_test.exs index 1010bf0..ef89fa6 100644 --- a/test/commands/special_test.exs +++ b/test/commands/special_test.exs @@ -285,4 +285,8 @@ defmodule SpecialOpsTest do assert evaluate("\"abcdef\" 1101S ÅÏu}") == "ABcDef" assert evaluate("∞ 10Þ ÅÏ5+} 10£") == [6, 2, 8, 4, 10, 6, 12, 8, 14, 10] end + + test "performance of repeated rotation" do + assert evaluate("123456789S256FÀ") == ["5", "6", "7", "8", "9", "1", "2", "3", "4"] + end end