Skip to content

Commit

Permalink
Add generic Enumerable and Collectable impls
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Bärenz committed Jun 17, 2021
1 parent 4ee336a commit cdb4192
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions lib/hallux/internal/finger_tree.ex
Original file line number Diff line number Diff line change
Expand Up @@ -958,4 +958,40 @@ defmodule Hallux.Internal.FingerTree do

defp maybe(default, _proj_fn, nil), do: default
defp maybe(_default, proj_fn, m), do: proj_fn.(m)

defimpl Enumerable do
alias Hallux.Internal.FingerTree

def count(_), do: {:error, __MODULE__}

def member?(_, _), do: {:error, __MODULE__}

def slice(_), do: {:error, __MODULE__}

def reduce(_finger_tree, {:halt, acc}, _fun), do: {:halted, acc}
def reduce(finger_tree, {:suspend, acc}, fun), do: {:suspended, acc, &reduce(finger_tree, &1, fun)}

def reduce(finger_tree, {:cont, acc}, fun) do
case FingerTree.view_l(finger_tree) do
nil -> {:done, acc}
{x, rest} -> reduce(rest, fun.(x, acc), fun)
end
end

end

defimpl Collectable do
alias Hallux.Internal.FingerTree

def into(original) do
collector_fn = fn
finger_tree, {:cont, elem} -> FingerTree.snoc(finger_tree, elem)
finger_tree, :done -> finger_tree
_finger_tree, :halt -> :ok
end

{original, collector_fn}
end
end

end

0 comments on commit cdb4192

Please sign in to comment.