stryrckt
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
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 1- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Eiji
First of all
Enumis module inElixircore code - not library, so topic should be closed.ok, but seriously now …
seemsis good word here. It would be definitelyuseful, but also definitely noteasy.Let’s take a look at simplest example
Enum.count/1. Do you want to countgraphemes,bytesor maybebites?Enumdoes not support options in its functions, so it would be hard to write anEnumerableimplementation which satisfies everyone.<<…>>syntax have lots of useful types likebits,bytes,utf8and 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.