mercurio
I have this function for parsing a string into a tuple. "-<number>" should return {:lat, -<number>}, "+<number>" should return {:lat, <number>}, and "<number>" should return {:gridNum, }`, plus some other simpler matches:
defp parseIndex(s) do
cond do
String.match?(s, ~r/^-\d+$/) ->
[_, n] = Regex.run(~r/^-(\d+)$/, s)
{:lat, -1 * String.to_integer(n)}
String.match?(s, ~r/^\+\d+$/) ->
[_, n] = Regex.run(~r/^\+(\d+)$/, s)
{:lat, String.to_integer(n)}
s == "--" ->
{:latFrame, -1}
s == "++" ->
{:latFrame, 1}
s == "-*" ->
{:latEnd, -1}
s == "+*" ->
{:latEnd, 1}
match?({_n, ""}, Integer.parse(s)) ->
{:gridNum, String.to_integer(s)}
true ->
{:gridText, s}
end
end
This works, but is there a better way to do this without running the regex twice, once to match a pattern like -<number> and then again to extract the numerical portion? Same for the second-to-last clause, which ends up parsing the integer twice.
Thanks!
Phil
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
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
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
So i have been using ash framework for a while and i love it. However currently the issue im having with ash framework is the error handl...
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
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
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
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex











Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kip
Noting that:
Regex.run/2returnsnilif there is no match and thatnilisfalsyfor the purposes of boolean evaluation andFor an optimisation I would likely put the explicit equality checks first since a
condproceeds in lexical order. Also I think you can collapse the:latparsing into a single clause. And one last one, you can match and bind on the integer parsing too (the second last clause):mercurio
Thanks! I’d tried
But when the Regex fails
[_, n]doesn’t match, causing an error. Your use of thematchvariable solves that beautifully.However, the second-to-last clause doesn’t work, because variables within the
match?/2call aren’t available outside. I guess I could use Regex to detect a string of only digits and then useString.to_integer/1, that’s probably slightly faster than parsing the string to an integer twice.And thanks, also, for pointing out the simple optimization of moving the static clauses to the top.
Phil
code-shoily
This is how I’d do it:
By adding groups to the sign part and the remaining number and inspecting the captured sign (or absence of it), we can remove the need to a check to
Integer.parse.[Update: I had missed the
-1multiplier]edisonywh
What about doing binary matching directly?
code-shoily
Lol, I was just thinking about that! I love a set of declarative one-liners where you can scan from top to bottom and pin point the right clause when you see it.
code-shoily
This would raise an error in case of a string though, never matching a
{:gridText, text}result.mercurio
I like how this avoids the Regex (not that I have anything against Regexes) but
<<"+", number::binary>>doesn’t constrainnumberto be a sequence of digits. It would try to parse something like"+q", which should result in{:gridText, "+q"}.mercurio
I like the solution with multiple functions, with a single Regex in the last one after handling all the simple cases. This is the best solution so far, thanks!
hauleth
Here you have it with error handling