Skip to content

Instantly share code, notes, and snippets.

@padde
Created April 13, 2016 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save padde/7cda9a8b9c7acb33ce942d9c4c3be4ad to your computer and use it in GitHub Desktop.
Save padde/7cda9a8b9c7acb33ce942d9c4c3be4ad to your computer and use it in GitHub Desktop.
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