fix Geef.Tree enumerable

This commit is contained in:
Ramon Snir
2015-07-11 09:55:51 +03:00
committed by Carlos Martín Nieto
parent 1e48686aca
commit 8c39463095

View File

@@ -66,30 +66,31 @@ defmodule Geef.TreeError do
end end
end end
defimpl Enumerable, for: Geef.Tree do defimpl Enumerable, for: Geef.Object do
alias Geef.Object
alias Geef.Tree alias Geef.Tree
alias Geef.TreeError alias Geef.TreeError
def count(tree) do def count(tree = %Object{type: :tree}) do
{:ok, Tree.count(tree)} {:ok, Tree.count(tree)}
end end
def member?(tree, key) do def member?(tree = %Object{type: :tree}, key) do
case Tree.get(tree, key) do case Tree.get(tree, key) do
{:ok, _} -> {:ok, true} {:ok, _} -> {:ok, true}
_ -> {:ok, false} _ -> {:ok, false}
end end
end end
def reduce(tree, acc, fun) do def reduce(tree = %Object{type: :tree}, acc, fun) do
reduce(tree, 0, Tree.count(tree), acc, fun) reduce(tree, 0, Tree.count(tree), acc, fun)
end end
# We're done when the index is equal to the number of entries # We're done when the index is equal to the number of entries
defp reduce(_, idx, idx, acc, _), do: acc defp reduce(_, idx, idx, {:cont, acc}, _), do: {:done, acc}
# Call the user-passed function and recurse with the next index # Call the user-passed function and recurse with the next index
defp reduce(tree, idx, count, acc, fun) do defp reduce(tree, idx, count, {:cont, acc}, fun) do
case Tree.nth(tree, idx) do case Tree.nth(tree, idx) do
{:ok, entry} -> {:ok, entry} ->
reduce(tree, idx + 1, count, fun.(entry, acc), fun) reduce(tree, idx + 1, count, fun.(entry, acc), fun)
@@ -98,4 +99,9 @@ defimpl Enumerable, for: Geef.Tree do
end end
end end
# Almost default Enumerable implementation
defp reduce(_, _, _, {:halt, acc}, _), do: {:halted, acc}
defp reduce(tree, idx, count, {:suspend, acc}, fun) do
{:suspended, acc, &reduce(tree, idx, count, &1, fun)}
end
end end