Skip to content

Instantly share code, notes, and snippets.

@padde

padde/codepoints.exs

Created Apr 13, 2016
Embed
What would you like to do?
defmodule Codepoints do
defstruct str: ""
def new(str) when is_list(str) do
new(to_string(str))
end
def new(str) when is_binary(str) do
%__MODULE__{str: str}
end
defimpl Enumerable do
def reduce(_, {:halt, acc}, _fun) do
{:halted, acc}
end
def reduce(%Codepoints{str: str}, {:suspend, acc}, fun) do
{:suspended, acc, &reduce(str, &1, fun)}
end
def reduce(%Codepoints{str: ""}, {:cont, acc}, _fun) do
{:done, acc}
end
def reduce(%Codepoints{str: <<h::utf8, t::binary>>}, {:cont, acc}, fun) do
reduce(%Codepoints{str: t}, fun.(<<h::utf8>>, acc), fun)
end
def member?(_str, _value) do
{:error, __MODULE__}
end
def count(_str) do
{:error, __MODULE__}
end
end
end
IO.inspect "abc" |> Codepoints.new |> Enum.map(&{&1, &1}) |> Enum.into(%{})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment