Qqwy

Qqwy

TypeCheck Core Team

I am working on a small hobby project which uses ETS/(Am)nesia to learn more about how these systems work.

One thing I am struggling with, is the following:

A user might want to look up records by entering a postal code. Here in the Netherlands, postal codes have the format 1234AB that is, four (base-10) digits followed by two (always capital) letters. However, I want to allow users to be able to find all records having the postal code “1234AB”, “1234AC”, “1234DH”, etc… if they only enter “1234”. In more formal terms: I want users to find all postal codes that contain the prefix they entered.

In SQL this can be done by using a LIKE operation: SELECT * from `myTable` where `postalcode` LIKE '1234%'.
I was wondering if (how) something like this can be done using ETS’ Match Specs.

Any help would be greatly appreciated :blush:

Showing Posts 1 to 3

StefanHoutzager

StefanHoutzager

:ets.match_object(:your_table_name, {[numeric_part, :_]})
between is your unique key
the second part is the alphanumerical part

you create the table with f.e. :ets.new(:your_table_name, [:named_table, read_concurrency: true])

No idea if this is possible with the complete postcode in one field though.

sasajuric

sasajuric

Author of Elixir In Action

AFAIK, matching a binary prefix is not supported with matchspecs. A couple of options I see:

  1. Consider splitting postal code into two fields - the integer part and the letter suffiix. Then you could match on the exact integer part, since matching the exact binary value is supported with matchspecs.
  2. Use charlists instead for the postal code, because you can match the head of a charlist, which allows you to select arbitrary prefix.
  3. You could hack with comparison operators >= and < to test whether a value is greater than or equal to “12345” and less than “12346”. As far as I can tell, that might work.
  4. Iterate through table records, fetch the postal code for each record, then if the code matches the required prefix fetch the desired fields.

To test what can work with matchspecs and what isn’t supported, you can play with :ets.fun2ms from the iex shell:

iex(1)> :ets.fun2ms(fn({key, "12345"} = el) -> key end)
[{{:"$1", "12345"}, [], [:"$1"]}]

iex(2)> :ets.fun2ms(fn({key, "12345" <> _} = el) -> key end)
Error: fun head contains bit syntax matching of variable '_', which cannot be translated into match_spec
saleyn

saleyn

A bit late with this answer, but I ran into the same problem and came up with the following solution:

defmodule Matcher do
  def match_prefix(table, part) when is_binary(part) do
    prefix = String.downcase(part) |> String.to_charlist() |> to_improper_list()
    :ets.select(table, [{{prefix, :_}, [], [:"$_"]}])
  end

  defp to_improper_list([]), do: :_   # <--- this is the main trick that causes partial matching
  defp to_improper_list([h|t]), do: [h|to_improper_list(t)]
end

1> :ets.new(:t, [:public, :named_table, :ordered_set])
2> :ets.insert(:t, [{~c"1234AB", 1}, {~c"1234AC", 2}, {~c"2345BB", 3}])
true
3> Matcher.match_prefix(:t, "1234")
[{~c"1234AB", 1}, {~c"1234AC", 2}]
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
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
nseaSeb
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
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
velrest
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
samoloth
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews