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

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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 131117 1222
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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

We're in Beta

About us Mission Statement