amnu3387

amnu3387

Hi, does anyone know how I can create a matchspec for this condition?

matchspec = :ets.fun2ms(fn({key, test}) when key != :counter -> test end)

[error] exited in: :ets.fun2ms(:function, :called, :with, :real, :fun, :should, :be, :transformed, :with, :parse_transform, :or, :called, :with, :a, :fun, :generated, :in, :the, :shell)
        ** (EXIT) :badarg

I can get the result I want with:

test = :ets.foldl(fn({key, obj}, acc) -> case key != :counter do
                                           true -> [obj | acc]
                                           _ -> acc
                                         end
 end, [], :queue_cache)

or

 test = :ets.foldl(fn({key, obj}, acc) when key != :counter -> [obj | acc]
                   (_, acc) -> acc
 end, [], :queue_cache)

But fun2ms seems a bit cleaner to write - I’m not sure I understand the restraints in fun2ms, perhaps is my when condition that’s not valid?

Showing Posts 1 to 10

kokolegorille

kokolegorille

I had a similar question…

I think it’s safer to use ex2ms (Elixir to match spec) for the moment.

amnu3387

amnu3387 OP

thank you! I ended up going to the console and see what match specification it output and I got this:

[{{:"$1", :"$2"}, [{:"/=", :"$1", :counter}], [:"$2"]}]

Which seems to be correct to my very thin knowledge of Erlang,
So I tried:
test = :ets.match(:queue_cache, [{{:"$1", :"$2"}, [{:"/=", :"$1", :counter}], [:"$2"]}])

but it won’t match anything, I’ve tried with :"counter" as well

Nicd

Nicd

In your second example, /= has turned into \=?

amnu3387

amnu3387 OP

ups, that was after trying a couple variations, it’s :"/="

rvirding

rvirding

Creator of Erlang

The reason :ets.fun2ms/1 works like this is because it is basically a macro. To use this in Erlang you need to include a -include_lib("stdlib/include/ms_transform.hrl"). in your Erlang file. (it’s in the docs) This runs a parse transform which at compile-time transforms the fun into the match spec. If this transformation is not done at compile time then the :ets.fun2ms/1 function is called at run-time and it gives that rather clumsy error message.

This include also the :dbg.fun2ms/1 “macro” which is used to build match specs for tracing. They are very similar to the ones for tables but not quite the same.

Parse transforms are like macros on steroids. The parse transform’s module/2 function is called with the whole Erlang module AST as an argument with which it can do whatever it likes. It just has to return a new complete module AST. Quite fun once you get the hand of it.

amnu3387

amnu3387 OP

Thank you for replying Robert - I did read the other thread and the docs as well. The error message was quite explicit (although presented in sort of a lispy fashion) I was just unsure how to include the matchspecs transform file when in elixir (actually I thought it would be somehow baked in).

I definitively need to dig a bit more into erlang.

Is that why you named these functions as “fun2…”? (j/k)

I still haven’t figured out why the match spec that I got from the terminal won’t work? I imagine that the one we retrieve in the shell will be equal to the one that is parse-transformed at compile-time right? Or am I missing something? I did also check the match specification in the erlangs docs

[{{:"$1", :"$2"}, [{:"/=", :"$1", :counter}], [:"$2"]}]

[{ { MatchHead }, [{ Guard Function }], [ MatchBody#$2_bound_in_matchhead ] }]

But it doesn’t return me any records and I haven’t figured yet what I’m doing wrong (I haven’t looked much more into it because :ets.foldl solves it right now)

rvirding

rvirding

Creator of Erlang

Well it should only return the test value. I get it to work from the shell:

iex(8)> :ets.new(:qc, [:named_table,:bag])
:qc
iex(9)> :ets.insert(:qc, [counter: 1, foo: 2, counter: 3, bar: 4])
true
iex(10)> :ets.tab2list(:qc)                                        
[counter: 1, counter: 3, foo: 2, bar: 4]
iex(11)> ms = :ets.fun2ms(fn({k,t}) when k != :counter -> t end)   
[{{:"$1", :"$2"}, [{:"/=", :"$1", :counter}], [:"$2"]}]
iex(12)> :ets.select(:qc, ms)
[2, 4]

The name isn’t a joke, unfortunately. He who wrote the module like the shorter 2 instead of _to_. It does crop up in a few other places in the libraries as well. :anguished:

Nicd

Nicd

Maybe the problem is that you’re giving the match spec to :ets.match, when it needs to be given to :ets.select? match accepts a pattern instead of match spec, if I’m not mistaken.

amnu3387

amnu3387 OP

Thank you @rvirding and @Nicd - indeed the problem was simply using a matchspec with match instead of with select (also known as "overlooking the obvious"TM)

for my defence though a match spec would seem like the right fit for a match function

Nicd

Nicd

Maybe it should be called a select spec. :slight_smile:

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
FlyingNoodle
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 Top

pckrishnadas88
In the previous part of this series, we built a Worker Pool from scratch, exploring point-to-point communication where a coordinator assi...
New
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews