amnu3387

amnu3387

Elixir with Erlang fun2ms -> ETS

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?

Marked As Solved

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.

Also Liked

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.

Nicd

Nicd

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

rvirding

rvirding

Creator of Erlang

Well, how tracing uses match specs is a bit different so calling them a select spec would not fit in there. And they are so close that giving them different names would be stupid. The main difference is what you can do in the body: in tables you can basically just return data while in tracing you can control how tracing works.

amnu3387

amnu3387

I definitively need to dig through the docs as I’ll be using quite a bit of ETS (btw I really like them, but until now I have only used them for unique key->value, with no need to do other stuff with them).

kokolegorille

kokolegorille

I had a similar question…

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

Where Next?

Popular in Questions Top

LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement