kip

kip

ex_cldr Core Team

String transliteration - best strategy?

One of my projects involves transliterating digits from one number system to another. For example:

iex> roman_digits = "0123456789"
iex> arab_digits = "٠١٢٣٤٥٦٧٨٩"
iex> transliterate("123", from: roman_digits, to: arab_digits)
"١٢٣"

What is the fastest transliteration strategy? Currently I generate a set of functions that translate each code point from one number system to another and then join them. Is there a faster/better method? Would using map lookups be faster? These would all be “small maps” (< 32 entries) since the set of digits is defined to be 0..9, +, -, \s, ., E

Most Liked

Schultzer

Schultzer

dimitarvp

dimitarvp

I am very late here but kept this thread open since this interested me.

This is what I came up with (~4.82 μs per function call with the parameter "01xyz234abc567def890!"):

defmodule Translit do
  def western_to_eastern(string, acc \\ "")

  def western_to_eastern(<<>>, acc), do: acc

  ~c[0123456789]
  |> Enum.zip(~c[٠١٢٣٤٥٦٧٨٩])
  |> Enum.each(fn {western, eastern} ->
    def western_to_eastern(<<unquote(western)::utf8, rest::binary>>, acc), do: western_to_eastern(rest, acc <> unquote(to_string([eastern])))
  end)

  def western_to_eastern(<<any::utf8, rest::binary>>, acc), do: western_to_eastern(rest, acc <> to_string([any]))
end

Why does unquote work without a quote block though, I have no idea. :003:

OvermindDL1

OvermindDL1

def’s are implicit quote blocks is why. :slight_smile:

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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
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
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement