James_E

James_E

Trouble understanding octet string (binary) iteration

The Comprehensions article says:

In Elixir, it is common to loop over an Enumerable … Comprehensions are syntactic sugar for such constructs

However, this simple example of Enumerable manipulation:

# [11, 22, 33]
[1, 2, 3] |> Enum.map(fn i -> i+10*i end)

fails when the input is instead an octet string:

# (Protocol.UndefinedError) protocol Enumerable not implemented for <<1, 2, 3>> of type BitString.
<<1, 2, 3::8>> |> Enum.map(fn i -> i+10*i end)

Instead it only seems to work with the allegedly “syntactic sugar” for comprehension:

# [11, 22, 33]
<<1, 2, 3>> |> (fn s -> (for <<i::8 <- s>>, do: i+10*i) end).()

What am I doing wrong?

What do I need to do to Enumerate a BitString by chunks of a given interval, especially 8?


EDIT [X-Y Problem]:

I’m trying to do this as a component of a larger function which counts the “leading zeroes” of an octet string in a rather perverse way — the octets are counted in something like big-endian*, while the bits within each octet are counted little-endian. So I’d require <<0, 255, 0, 0>> -> 8 but <<0, 254, 0, 0>> -> 9.

*That is, in the “usual” iteration order one gets from Python’s for octet in buf or C’s for ( i=0 ; i<buflen ; i++ ){ octet = buf[i]; }.

First Post!

LostKobrakai

LostKobrakai

The bit/binary syntax usable with for is a feature of for with it being a special form. Binaries/bitstrings do not implement the Enumerable protocol. The issue is that there’s no clear cadance as to where a binary or bitstring should be split at. for knowing what syntax is on the matching side can adjust that automatically. With Enumerable you however don’t know anything about the consumer of the individual pieces. You therefore need to manually split the binary or bitstring up.

Most Liked

James_E

James_E

Hmm, I think I figured it out, now.

def binary_to_stream(s) do
  Stream.unfold(s, fn
    <<>> -> nil
    <<next::8, rem::binary>> -> {next, rem}
  end)
end
gregvaughn

gregvaughn

If the early exit really makes a difference, you could also do explicit recursion.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New