At7heb
Integer.to_string_with_underscores?
I would like to have integers converted to strings with underscores. So instead of “77777777”, I would have “77_777_777”. Do I have to write my own?
(I’m writing a user program simulator for the SDS 940, a 24 bit machine with all documentation in octal.)
Marked As Solved
Also Liked
adamu
Here’s a version that does it by calculating the offset and building up the string in a single pass.
def annotate(str) do
length = byte_size(str)
offset = rem(length, 3)
{acc, rest} = String.split_at(str, offset)
do_annotate(acc, rest)
end
defp do_annotate(acc, ""), do: acc
defp do_annotate("", <<next::binary-3, rest::binary>>), do: do_annotate(next, rest)
defp do_annotate(acc, <<next::binary-3, rest::binary>>),
do: do_annotate(<<acc::binary, ",", next::binary>>, rest)
In my benchmarks it’s about 3x faster than the formatter implementation for 7-digit strings (8x for 1kb strings but that’s probably not a realistic use-case). A fun little challenge but I know people aren’t a fan of the bitstring syntax ![]()
Name ips average deviation median 99th %
adamu 5.46 M 183.02 ns ±8688.03% 125 ns 250 ns
formatter 1.97 M 508.32 ns ±3909.15% 334 ns 542 ns
Comparison:
adamu 5.46 M
formatter 1.97 M - 2.78x slower +325.30 ns
Memory usage statistics:
Name Memory usage
adamu 0.40 KB
formatter 1.89 KB - 4.75x memory usage +1.49 KB
Operating System: macOS
CPU Information: Apple M1 Pro
Number of Available Cores: 10
Available memory: 16 GB
Elixir 1.15.4
Erlang 26.1
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 500 ms
reduction time: 0 ns
parallel: 1
inputs: none specified
Estimated total run time: 15 s
4
sabiwara
Elixir Core Team
Nice! I tried a spin-off based on binary comprehension out of curiosity, but it couldn’t beat recursion in the benchmarks (link) ![]()
length = byte_size(str)
offset = rem(length - 1, 3) + 1
{acc, rest} = String.split_at(str, offset)
for <<group::binary-3 <- rest>>, into: acc, do: <<"_", group::binary-3>>
Results:
##### With input 7 digits #####
Name ips average deviation median 99th %
Comprehension 3.95 M 253.43 ns ±12903.32% 167 ns 292 ns
Recursive 3.70 M 270.12 ns ±12392.13% 166 ns 292 ns
Comparison:
Comprehension 3.95 M
Recursive 3.70 M - 1.07x slower +16.69 ns
Memory usage statistics:
Name Memory usage
Comprehension 680 B
Recursive 488 B - 0.72x memory usage -192 B
##### With input 30 digits #####
Name ips average deviation median 99th %
Recursive 3.00 M 333.77 ns ±3331.98% 292 ns 458 ns
Comprehension 1.91 M 522.30 ns ±3697.19% 459 ns 584 ns
Comparison:
Recursive 3.00 M
Comprehension 1.91 M - 1.56x slower +188.53 ns
Memory usage statistics:
Name Memory usage
Recursive 0.95 KB
Comprehension 1.95 KB - 2.06x memory usage +1 KB
2
Popular in Questions
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
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
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
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
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
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> someth...
New
Other popular topics
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
Manning 2016 Halloween weekend sale via Deal of the Day
Friday, October 28 - Half off all MEAPs - code WM102816LT
Saturday, October 29 ...
New
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
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
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including.
What is Phoenix LiveV...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New








