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
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
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
Maybe it should be called a select spec. ![]()
rvirding
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








