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
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
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
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
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
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #elixirconf-us
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










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.