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?
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kokolegorille
I had a similar question…
I think it’s safer to use ex2ms (Elixir to match spec) for the moment.
amnu3387
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 wellNicd
In your second example,
/=has turned into\=?amnu3387
ups, that was after trying a couple variations, it’s
:"/="rvirding
The reason
:ets.fun2ms/1works 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/1function 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/2function 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
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.foldlsolves it right now)rvirding
Well it should only return the
testvalue. I get it to work from the shell:The name isn’t a joke, unfortunately. He who wrote the module like the shorter
2instead of_to_. It does crop up in a few other places in the libraries as well.Nicd
Maybe the problem is that you’re giving the match spec to
:ets.match, when it needs to be given to:ets.select?matchaccepts a pattern instead of match spec, if I’m not mistaken.amnu3387
Thank you @rvirding and @Nicd - indeed the problem was simply using a matchspec with
matchinstead of withselect(also known as "overlooking the obvious"TM)for my defence though a
match specwould seem like the right fit for amatchfunctionNicd
Maybe it should be called a select spec.