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.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

We're in Beta

About us Mission Statement