TL;DR
In the previous post, we explained how to pattern-match Elixir Maps. Today we explain how to pattern-match Elixir Bitstrings and Binaries. This post is part of the functional language series, and it is based on the remarkable book Elixir In Action by Sasa Juric.
Bitstrings And Binaries
First, we need to recap on bitstrings and binaries. Bitstrings are a chunk of bits with configurable size, while binary is a bitstring with a byte size (eight bits). Remember that Elixir Strings are binaries.
Using binary syntax and pattern-matching operator, we first pattern-match each byte of three-byte binary into separate variables in the above screenshot.
Then we forced one MatchError
.
We can pattern-match just what is interesting for us.
It is also possible to pattern-match binary data in smaller chunks of bits. 156 in binary is "10011100"
. When we split it into two four-bit values, we split 156 into 9 and 12 because 9 => "1001"
and 12 => "1100"
. Note that we used Integer.to_string
it because Elixir Strings are binaries.
Let’s pattern match Croatian letters. Š Đ and Č
. For them, we need 16 bytes because those are encoded using. Decimal UTF8 value for Š
is 50592.
We could also do something like regular expressions with the help of operator. Here we store the testers name into name
variable.
When To Use It
When should you use binaries pattern-matching in Elixir? When you are receiving a stream of binary data (zeros and ones) from a file, device, or URL, extracting bits from those streams based on various patterns is best done using binary pattern-matching.
Remember
- binaries pattern matching
- string pattern matching
- pattern matching instead of regex
Comments are closed.