Leo-Nogismus

Leo-Nogismus

I was wondering if there is a way to return both the variables (e.g., :“$1”), and its matched value with a single call to :ets.select(table, match_spec).

Assuming, for example, that we have an ets table called :foo, which is filled with entries of, for example, {:a, %{b: :c}}, and a match specification like [{{:a, %{b: :"$1"}, [], [something_here]}]. Is there a way to define some functions in the match specification’s body such that I get a map of “variable-atom” to “matched-value”, or similar?

So, for example:

iex(1)> :ets.new(:foo, [:bag, :public, :named_table])
iex(2)> :ets.insert(:foo, {:a, %{b: :c}})
iex(3)> :ets.select(:foo, [{{:a, %{b: :"$1"}}, [], [:"$1"]}])

returns

[:c]

but what I’m hoping to find is a way to have :ets.select return for this example

%{"$1": :c}

or anything like that.

I know that there is the match body variable :"$$" that returns a list of all the variables’ values. The issue is just that the match_spec is dynamically created, and I don’t know which variables (i.e., which integer numbers after the dollar sign) are in the match spec at the time of the select call. I need those to construct guards for a second select call.

Of course, this is a very small example. In my case, the terms stored in the ets table are much more complex, making it very hard to iterate over them to check for variable-value matches.

Any help would be appreciated.

Showing Posts 1 to 3

mudasobwa

mudasobwa

Creator of Cure

The last element in MatchSpec might be whatever, so you might go with something like

iex|💧|1 ▸ :ets.select(:foo, [{{:a, %{b: :"$1"}}, [], [{{:foo, :"$1"}}]}])
[foo: :c]

if I understood what you are after properly.

Leo-Nogismus

Leo-Nogismus OP

Thanks for the reply, but I’m in particular searching for a solution to get the variable (e.g., :"$1" out of the select call. But I don’t know which variable it is in advance, so I don’t know how I could specify this in the body. I was hoping for something like [:"$$"], but instead of getting a list with the bound values, I’d get a list of the bound values and the variable that it was bound to.

christhekeele

christhekeele

Atoms of the form :"$N" are pretty much the only literals you cannot return out of a match spec verbatim, so I suggest you simply come up with your own tagging convention and inject them into your matchspecs when selecting values; for example, raw integers.

It’s hard to envision quite what problem you are describing without example code of how you are composing your MSs dynamically. Maybe you need to pass an integer accumulator throughout your piece-wise composition of each spec clause, and increment it when you generate a new binding, so you know what values to take out and tag afterwards?

— 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