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]; }.

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

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Tee
can someone please explain to me how Enum.reduce works with maps
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
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
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
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
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 126479 1222
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement