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
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
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
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
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
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
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #elixirconf-us
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










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