stryrckt

stryrckt

Why aren't binaries enumerable?

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

Most Liked

Eiji

Eiji

First of all Enum is module in Elixir core code - not library, so topic should be closed. :077:

ok, but seriously now …

seems is good word here. It would be definitely useful, but also definitely not easy.

Let’s take a look at simplest example Enum.count/1. Do you want to count graphemes, bytes or maybe bites? Enum does not support options in its functions, so it would be hard to write an Enumerable implementation which satisfies everyone. <<…>> syntax have lots of useful types like bits, bytes, utf8 and many more. Such protocol implementation can’t guess based on which type you want to enumerate.

There are probably also some internal reasons for doing that, but I can’t give example here as I’m not a member of lots core discussions. I think that people from Elixir Core Team could say more about it.

Where Next?

Popular in Questions Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New