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’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
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
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Other Trending Topics
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
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
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
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #performance











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.