Skip to content

Commit 1d532f3

Browse files
authored
Merge pull request #190 from monkeygroover/rotate-performance
Don't rotate lazily
2 parents e622c65 + 63e5ece commit 1d532f3

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

lib/commands/list_commands.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,19 +326,19 @@ defmodule Commands.ListCommands do
326326
def rotate(value, shift) when shift == 0, do: value
327327
def rotate(value, shift) when not Functions.is_iterable(value), do: Enum.join(rotate(String.graphemes(to_string(value)), shift), "")
328328
def rotate(value, shift) when shift > 0 do
329-
case length(Enum.to_list value) do
329+
case Enum.count value do
330330
0 -> []
331331
x ->
332332
shift = rem(shift, x)
333-
Stream.concat(value |> Stream.drop(shift), value |> Stream.take(shift)) |> Stream.map(fn x -> x end)
333+
Stream.concat(value |> Stream.drop(shift), value |> Stream.take(shift)) |> Enum.to_list
334334
end
335335
end
336336
def rotate(value, shift) when shift < 0 do
337-
case length(Enum.to_list value) do
337+
case Enum.count value do
338338
0 -> []
339339
x ->
340340
shift = rem(shift, x)
341-
Stream.concat(value |> Stream.take(shift), value |> Stream.drop(shift)) |> Stream.map(fn x -> x end)
341+
Stream.concat(value |> Stream.take(shift), value |> Stream.drop(shift)) |> Enum.to_list
342342
end
343343
end
344344

test/commands/special_test.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,8 @@ defmodule SpecialOpsTest do
285285
assert evaluate("\"abcdef\" 1101S ÅÏu}") == "ABcDef"
286286
assert evaluate("∞ 10Þ ÅÏ5+} 10£") == [6, 2, 8, 4, 10, 6, 12, 8, 14, 10]
287287
end
288+
289+
test "performance of repeated rotation" do
290+
assert evaluate("123456789S256FÀ") == ["5", "6", "7", "8", "9", "1", "2", "3", "4"]
291+
end
288292
end

0 commit comments

Comments
 (0)