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

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
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
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement