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
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
2is the field width. Without it, values less than 16 will only output a single character16is called “precision” in the API, but for theBcontrol sequence it’s the base to use0is the padding character, because we want4to print as04not<space>4
6
hauleth
That topic gave me an idea for a library. I will try to draft something this weekend.
4
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>>}
3
Popular in Questions
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
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
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
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
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
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
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
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
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
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
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
Hello, how can I check the Phoenix version ?
Thanks !
New
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
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
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
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
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
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New







