Showing Posts 1 to 9

hauleth

hauleth

What is there to fear? It is almost the same as Python instead of % you use ~. If anything we will get wrapper over io_lib:format/2 as it is handy for report_cb in logger handlers.

Sebb

Sebb OP

For example I just had to format a CSS compatible hex color. (#RRGGBB, eg (0,0,255) -> #0000FF)
Could not figure that out from the documentation.

Also it is actually two things.

  • We got io_lib:format which I do not like (maybe I’m just too stupid, but not all coders geniuses, and Elixir is mostly a very easy language, so I might be spoiled also)
  • We got no format embedded in interpolations which is just very handy
kasperkronborg

kasperkronborg

I actually agree with you @Sebb.

I generally like the Erlang documentation and I no problems in translating that into Elixir, but I had a really hard time with the documentation for io_lib:format and the understanding of this. I believe it’s because I’ve got so used to everything else in Elixir is so intuitive and understandable that most times I just need to skim through the documentation to get back into the groove of things. Which I think is super important in a dynamic language where the language server isn’t always there to help you with introspection.

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
Sebb

Sebb OP

I admit, with some patience the docs are understandable.

One thing that confused me are the ., but I now understand, that they are only needed when one of P, Pad,or Mod right to it is used, here the dot right to 16 is wrong, only needed when Pad is used.

iex(23)> :io.fwrite("~.16.B", [15])
** (ArgumentError) errors were found at the given arguments:

  * 1st argument: format string invalid (truncated)

Interpolation

So what about

iex> "##{r:2.16.0B}{g:2.16.0B}{b:2.16.0B}"

I think that would be useful.

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>>}
hauleth

hauleth

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

Sebb

Sebb OP

That’s true. But maybe inside a sigil?

Really? Does not seem too hard, but I dont know too much about parsers.

Just take the output and pass it to the formatter?

# python 3.6
>>> def foo(): return 255
... 
>>> f"#{foo():0{2}x}"
'#ff'

may raise

# python 3.6
>>> def foo(): return None
... 
>>> f"#{foo():0{2}x}"
TypeError: unsupported format string passed to NoneType.__format__

I do not understand what you mean.

Yes, that’s not nice in this case. So we would be responsible to call sth like to_string().

:star_struck:

RemyXRenard

RemyXRenard

This is somewhat of a thread necro, and I already posted this in the Livebook category, but if someone comes here looking for some :io_lib.format examples, I put together a Livebook.

Fun with formatting:

Run in Livebook

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
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
RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
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
RemyXRenard
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
velrest
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
samoloth
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
FlyingNoodle
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 Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews