Created
April 13, 2016 13:13
-
-
Save padde/7cda9a8b9c7acb33ce942d9c4c3be4ad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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