Skip to content

Commit

Permalink
Don't rotate lazily
Browse files Browse the repository at this point in the history
- highly inefficient when done repeatedly
  • Loading branch information
monkeygroover committed Mar 17, 2022
1 parent 9a57b6a commit 63e5ece
Showing 1 changed file with 4 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

0 comments on commit 63e5ece

Please sign in to comment.