Skip to content

Commit

Permalink
Merge pull request #190 from monkeygroover/rotate-performance
Browse files Browse the repository at this point in the history
Don't rotate lazily
  • Loading branch information
Adriandmen authored Mar 23, 2022
2 parents e622c65 + 63e5ece commit 1d532f3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/commands/list_commands.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions test/commands/special_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1d532f3

Please sign in to comment.