Why don’t Binary types implement the Enumerable protocol so that they can be used with the Enum library? It seems like it would be useful and easy enough to write the reduce, slice, count, and member? functions for binaries.
I tried doing so, but it only seems to work if I define the implementation for BitString and use guards or pattern matching to make sure that binaries are being passed.
defmodule BinaryEnumerable do
defimpl Enumerable, for: BitString do
def slice(_string), do: {:error, __MODULE__}
def slice(string, range) when is_binary(string),
do: String.slice(string, range)
def slice("", _start, _count), do: ""
def slice(_string, _start, 0), do: ""
def slice(string, start, len) when is_binary(string),
do: String.slice(string, start, len)
def count(string) when is_binary(string),
do: {:ok, String.length(string)}
def member?(string, <<char::binary-1>>) when is_binary(string),
do: {:ok, String.contains?(string, char)}
def member?(_string, _char), do: {:error, __MODULE__}
def reduce(_string, {:halt, acc}, _fun), do: {:halted, acc}
def reduce(string, {:suspend, acc}, fun), do: {:suspended, acc, &reduce(string, &1, fun)}
def reduce(<<>>, {:cont, acc}, _fun), do: {:done, acc}
def reduce(<<head::binary-1, tail::binary>>, {:cont, acc}, fun),
do: reduce(tail, fun.(head, acc), fun)
end
end