DimWell
Hello Experts,
Could you please advise what would be the way to match the bitstring ?
Lets say I have and input and I want to pattern part of it as bitstsing:
@input <<116, 114, 117, 101, 32, 105, 115, 32, 110, 111, 116, 32, 102, 97, 108, 115, 101>>
@true_ <<116, 114, 117, 101>>
@false_ <<102, 97, 108, 115, 101>>
def match_bitsting( <<true_ ::size(32) , rest :: bitstring>>= input) when trues_ == @true_ do
IO.puts "I got: true"
end
def match_bitsting( <<false_ ::size(40) , rest :: bitstring>>= input) when trues_ == @false_ do
IO.puts "I got: false"
end
How should I rewrite @true_ | @false_ or the pattern matching to make it work ?
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
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
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
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
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
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
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
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
grych
If I understood the problem properly, you want to match the beginning of the input string? In this case, it is easy:
DimWell
Not really, probably I have to change description a bit…
I have an input that looks like:
I want to do pattern match on:
<<116, 114, 117, 101>> # which is "true" and the rest data loop froward and match according other principlesFurthermore, this just dummy input just to explain the idea, the input can be any text…
idi527
Would this work for you?
Why are you using
bitstrings anyway? What you have in@inputis a (printable)binary.DimWell
Yes, this works and so clean code

Somehow I overcomplicated…
DimWell
Trying to understand how the pattern matching works on bitstring level…
I mean how to use pattern matching in this type of scenario…
I am still in learning and and trying to understanding Elixir / Erlang / Pattern Matching
Thank you very much for help !
NobbZ
I think what @idi527 meant here, why do you have
bitstringinstead ofbinary. The first one matches arbitrary length sequences of bits while the latter matches arbitrary length sequences of bytes, and since your provided patterns all seem to stick to a byte boundary the latter might perform better then the first.DimWell
Hi Noobz
Hm… When I read some online examples it was mentioned that if you are not sure about provided input then it is better to specify
bitstringrather thanbinary…In my case I assume the input data comes from a some API that comes as stream data.
I thought this is the way to do it.
Am I wrong ?

NobbZ
If it is streamed data you will probably get your chunks at byte boundary (as this is the level at which your interface transfers the data) and unless the protocol specifies something that is not aligned at byte boundaries, you can safely assume
binary.BUT If the data is streamed in, you can’t safely use binary pattern matching at all but need to use a different mean of parsing/deserializing the incomming data.
What happens if your current chunk is
"tru"? This does not match"true", but might after the next chunk has been received.DimWell
Yes, you are right. Thank you for a good point.
DimWell
I thought to give a try with Genstage & Flow.
Producer:
Something like receives chunks and analyses it according to some logic.
It would split a chunk into invalid and valid parts.
ConsumerProducer:
Consumer: