From 45f6d6c259e8d5e78e35508eb3d7730f86091f9e Mon Sep 17 00:00:00 2001 From: Frank Hunleth Date: Sat, 9 May 2026 22:50:35 -0500 Subject: [PATCH] Remove unneeded list concat on reduce The original reduce implementation simply converted the buffer to a list and used the normal list reduce implementation. Conversion to a list involves a list concatenation which can be removed by manually iterating through the a and b lists. --- lib/circular_buffer.ex | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/circular_buffer.ex b/lib/circular_buffer.ex index ae2ae46..d0d045d 100644 --- a/lib/circular_buffer.ex +++ b/lib/circular_buffer.ex @@ -127,9 +127,15 @@ defmodule CircularBuffer do end def reduce(cb, acc, fun) do - Enumerable.List.reduce(CB.to_list(cb), acc, fun) + do_reduce(cb.b, Enum.reverse(cb.a), acc, fun) end + defp do_reduce(_b, _a, {:halt, acc}, _fun), do: {:halted, acc} + defp do_reduce(b, a, {:suspend, acc}, fun), do: {:suspended, acc, &do_reduce(b, a, &1, fun)} + defp do_reduce([], [], {:cont, acc}, _fun), do: {:done, acc} + defp do_reduce([], [h | t], {:cont, acc}, fun), do: do_reduce([], t, fun.(h, acc), fun) + defp do_reduce([h | t], a, {:cont, acc}, fun), do: do_reduce(t, a, fun.(h, acc), fun) + def slice(_cb) do {:error, __MODULE__} end