daviaws
I’m writing a module to test ets match.
It would to store client consume by month/year.
I had try to follow match spec… I even tried to do automagic translate of elixir functions with GitHub - ericmj/ex2ms: :ets.fun2ms for Elixir, translate functions to match specifications · GitHub
nonetheless I always end up with an empty list []
Please… someone help me with list_period method…
defmodule ETS.Consume do
@moduledoc """
Consume tasks
r ETS.Consume
ETS.Consume.scenario()
table = ETS.Consume.start()
ETS.Consume.populate(table)
ETS.Consume.list(table, "12345678901")
ETS.Consume.list_period(table, "12345678901", 2, 2021, 10, 2021)
"""
import Ex2ms
@table :cons
@opts [:ordered_set, :protected]
def start do
:ets.new(@table, @opts)
end
def insert(ref, cpf_cnpj, mes, ano, consumo) do
key = {cpf_cnpj, mes, ano}
:ets.insert(ref, {key, consumo})
end
def list(table, cpf_cnpj) do
:ets.match(table, {{cpf_cnpj, :"$1", :"$2"}, :"$3"})
end
def list_period(table, cpf_cnpj, mes_inicio, ano_inicio, mes_fim, ano_fim) do
pattern = [{{{:"$1", :"$2", :"$3"}, :"$4"}, [{:<, :"$2", 2}], [true]}]
:ets.match(table, pattern)
end
def populate(table) do
insert(table, "12345678901", 1, 2021, 100)
insert(table, "12345678901", 2, 2021, 200)
insert(table, "12345678901", 3, 2021, 300)
insert(table, "12345678901", 4, 2021, 200)
insert(table, "12345678901", 5, 2021, 300)
insert(table, "12345678901", 6, 2021, 350)
insert(table, "12345678901", 7, 2021, 200)
insert(table, "12345678901", 8, 2021, 100)
insert(table, "12345678901", 9, 2021, 500)
insert(table, "12345678901", 10, 2021, 300)
insert(table, "12345678901", 11, 2021, 200)
insert(table, "12345678901", 12, 2021, 310)
insert(table, "12345678902", 1, 2021, 310)
end
def scenario do
table = ETS.Consume.start()
ETS.Consume.populate(table)
# ETS.Consume.list(table, "12345678901")
ETS.Consume.list_period(table, "12345678901", 2, 2021, 10, 2021)
end
def test() do
fun do {{ cpf_cnpj, mes, ano }, consumo} = arg when mes < 2 -> true end
end
end
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
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
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
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
daviaws
My workaround will be match by unique key and manually filter…
But this is clearly naive.
daviaws
returns
even this returns an empty list. This is annoying me… if only there is something I can do to know whats wrong, but this is it, even the official mechanisms are giving unexpected results.
daviaws
Some gentle and helpful soul, please enlighten me?
My next steps would be:
daviaws
erl
And so…
daviaws
### [ETS] Does :ets.match spec really works?
daviaws
Following the official docs: Match Specifications in Erlang — OTP 29.0.2 (erts 17.0.2)
al2o3cr
There’s an unfortunate overlap in terminology:
:ets.matchtakes a “match pattern”, not a match specification. The documentation for:ets.select/2demonstrates this:Demo:
daviaws
Thank you so much!!! I’ve been researching the topic for a while.
Thank you thank you thank you!!
It worked

ityonemo
(Self promotion) you might find this library to be helpful for you to go either way:
daviaws
Hello @ityonemo
whats the advantage over using :ets.fun2ms/1 ?