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

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
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
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40082 209
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

We're in Beta

About us Mission Statement