Sebb

Sebb

Any chance we get format for string interpolation?

sth like string — Common string operations — Python 3.10.4 documentation?

I really fear erlang’s ~F.P.PadModC (Erlang -- io)

Most Liked

al2o3cr

al2o3cr

For future readers, here’s the format needed:

:io.fwrite("#~2.16.0B~2.16.0B~2.16.0B", [r, g, b])

# prints the string "#F27B04"

Values smaller than zero or larger than 255 will be replaced with **:

:io.fwrite("#~2.16.0B~2.16.0B~2.16.0B", [-r, g, b])

# prints "#**7B04"

:io.fwrite("#~2.16.0B~2.16.0B~2.16.0B", [10*r, g, b])

# prints "#**7B04"

Breakdown in detail:

~2.16.0B
  • 2 is the field width. Without it, values less than 16 will only output a single character
  • 16 is called “precision” in the API, but for the B control sequence it’s the base to use
  • 0 is the padding character, because we want 4 to print as 04 not <space>4
hauleth

hauleth

That topic gave me an idea for a library. I will try to draft something this weekend.

al2o3cr

al2o3cr

IMO that’s too big of a change - one of the nice things about Elixir is the regularity of the syntax, and that would make #{} magical in a way that’s not available elsewhere. A more “Elixir-y” solution would be to make the conversion explicit:

"##{to_hex(r)}#{to_hex(g)}#{to_hex(b)}"
...
defp to_hex(v), do: :io_lib.format("~2.16.0B", [v])

Some other gotchas that come to mind:

  • making runtime-determined values work (* in the various positions) means more tricky syntax
  • what would "#{some_calculation():i}" do? “Ignore one thing from the input” makes a lot more sense when the format and the data are fully separated (as in :io)
  • the term-handling codes don’t output Elixir-shaped strings:
:io.fwrite("~w", [DateTime.utc_now()])

prints

#{'__struct__' => 'Elixir.DateTime',calendar => 'Elixir.Calendar.ISO',day => 1,hour => 22,microsecond => {937508,6},minute => 47,month => 5,second => 18,std_offset => 0,time_zone => <<69,116,99,47,85,84,67>>,utc_offset => 0,year => 2022,zone_abbr => <<85,84,67>>}

Where Next?

Popular in Questions Top

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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New

Other popular topics Top

stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
RisingFromAshes
I've read in another post that it may be possible with a router helper - but I couldn't find an appropriate one, and tbh, I'm still just ...
New
romenigld
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
AstonJ
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
openscript
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement