Dusty

Dusty

I am trying to compare the first character in a very long input string (like the text of a book) to some arbitrary character in a shorter string. The algorithm has an index for a grapheme in the shorter string, and just wants to check equality: Is the first character of my long string equal to the grapheme or not? I am trying to avoid doing something expensive under the hood, like calling String.graphemes/1 on the longer string. But I am having trouble figuring out the best way to compare the codepoint I get from binary pattern matching to the grapheme I get from other String methods.

To give a simple example:

def my_func(<<first::utf8, _rest::binary>> = _long_string) do
  last = String.at("string", 5)
  first == last
end

my_func("green")
# => false

I understand that basically this function is doing:

?g == "g"

so it makes sense that it returns false. But I am struggling to find the most efficient way to make this comparison.

I can do something like:

def my_func(<<first::utf8, _rest::binary>> = _long_string) do
  [last] = String.to_charlist(String.at("string", 5))
  first == last
end

my_func("green")
# => true

It works - but it feels like I should be able to do better :slight_smile:. What I wanted to do was use the codepoint operator on the variable, like first == ?last, but it didn’t take long for me to understand that won’t fly.

My impression is that String.next_grapheme/1 and friends are going to process the whole string just to give me one value. Is that the case? Seems like using Stream won’t help if it requires me to process the long string into an enumerable first.

Should I stick with my second method above, or is there a better way?

Showing Posts 1 to 4

Eiji

Eiji

@Dusty How about replacing your function body to that code:

String.ends_with?("string", <<first::utf8>>)
Dusty

Dusty OP

OK, this is fascinating. It happens that I might need to match at any point in the string, so String.ends_with?/2 might not work, but I am much more interested in what you’ve done with the variable by wrapping it in <<>>.

iex(1)> <<first::utf8, rest::binary>> = "string"
"string"
iex(2)> first
115
iex(3)> <<first::utf8>>
"s"
iex(4)> first == "s"
false
iex(5)> <<first::utf8>> == "s"
true
iex(6)> ?s
115
iex(7)> <<?s>>
"s"

This is kind of crazy. It obviously solves my problem, but what is happening here? I clearly don’t fully understand what Elixir strings are. Is Elixir actually converting between a charlist and a binary when I add <<>>? Or is it simply that no conversion is needed, because a charlist wrapped in <<>> is all a binary is? Is there a difference between what I’ve done here and calling Kernel.to_string/1?

Eiji

Eiji

@Dusty Simply take a look at Kernel.SpecialForms.<<>>/1 documentation.

The utf8 , utf16 , and utf32 types are for Unicode code points. They can also be applied to literal strings and charlists:

iex> <<"foo"::utf16>>
<<0, 102, 0, 111, 0, 111>>
iex> <<"foo"::utf32>>
<<0, 0, 0, 102, 0, 0, 0, 111, 0, 0, 0, 111>>
LostKobrakai

LostKobrakai

Charlists and binaries don’t really have things in common. Your code just happens to extract the codepoint for ?s from the binary. If you’d segment by a different type things might look different:

iex(16)> IO.inspect("string", base: :binary)
<<0b1110011, 0b1110100, 0b1110010, 0b1101001, 0b1101110, 0b1100111>>
"string"
iex(17)> IO.inspect("string", base: :hex)
<<0x73, 0x74, 0x72, 0x69, 0x6E, 0x67>>
"string"

Binaries are just a list of bytes. To apply meaning you’ll need to know how to interpret the bytes to something higher level. Charlists on the other hand are always a list of intergers for the spec: [0..0x10FFFF].

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
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
nseaSeb
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
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
velrest
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
samoloth
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
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

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews