James_E
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]; }.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #iex
- #elixirconf-us
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
The bit/binary syntax usable with
foris a feature offorwith it being a special form. Binaries/bitstrings do not implement theEnumerableprotocol. The issue is that there’s no clear cadance as to where a binary or bitstring should be split at.forknowing what syntax is on the matching side can adjust that automatically. WithEnumerableyou however don’t know anything about the consumer of the individual pieces. You therefore need to manually split the binary or bitstring up.James_E
I see. So it’s not only syntactic sugar; in certain cases it actually does new things?
Is there another way to get at the enumeration-of-bitstrings? (Of course, I understand I’ll need to specify the desired element “width” in any case.)
The function I’m trying to build will need early-exit; I want to iterate through the octet string only until I find a non-
0octet, then return. Is there any “lazy” way to go iterate through an octet-string — that is, without first “greedily” slurping the entire string up into a List likeforseems to do?James_E
Hmm, I think I figured it out, now.
LostKobrakai
You can use
Stream.unfoldto turn a binary into an enumerable of “pieces”:benwilson512
You can, but it’s worth noting that
foris going to produce more optimal code than wrapping this in a stream will do.gregvaughn
If the early exit really makes a difference, you could also do explicit recursion.