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 working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
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
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
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
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
In the previous part of this series, we built a Worker Pool from scratch, exploring point-to-point communication where a coordinator assi...
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
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










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.