CharlesO

CharlesO

Partial matching ETS data

Please is there a way to do the equivalent of the following with ETS.

:ets.fun2ms(fn {_id, m} 
when String.starts_with?(m.name, "charles") 
or String.starts_with?(m.mobile, "0809133") 
or String.starts_with?(m.email, "charles") -> 
m end)

I know we cannot have String.starts_with, or similar functions in a when clause, but is there a way to do the equivalent of this?


iex > :ets.fun2ms(fn {_id, m} when m.name == "charles" or m.mobile == "0809133" or m.email == "charles" -> m end)

[{{:"$1", :"$2"}, [{:orelse, {:orelse, {:==, {:map_get, :name, :"$2"}, "charles"}, {:==, {:map_get, :mobile, :"$2"}, "0809133"}}, {:==, {:map_get, :email, :"$2"}, "charles"}}], [:"$2"]}]

The above attempt will only handle exact string matches, and not partial matches like the LIKE clause

Thanks.

Most Liked

entone

entone

I found this on the mailing list, [erlang-questions] Binary pattern matching on ETS

It doesn’t look like ETS supports binary pattern matching.

It’s a bit dense, but you can also review the match spec, Match Specifications in Erlang — OTP 29.0.2 (erts 17.0.2)

mpope

mpope

If you are feeling ambitious, you could create a probabilistic datastructure like a Bloom filter or xor_filter for each entry’s possible matches, like adding ['c', 'ch', 'char', 'charl', charle', 'charles'], then iterate over each filter to check if the partial match is in the filter. This would be for very simple matching, but if your use case doesn’t support complex filters or ranking matches based on the ‘best match’ then it could work. There is a small chance of false positives. I’ve used this before for very simple first + last name matches for ~2k static values and it worked well enough.

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

We're in Beta

About us Mission Statement