ExPrintf – printf / sprintf for Elixir

https://github.com/parroty/exprintf

In Elixir (elixir-lang) or Erlang, io#format takes care of string-formatting. However, its format styling is somewhat different from the sprintf / printf ones.

Eventually, it would be expected to get accustomed to io#format style, but for alleviating the learning process, I tried to write a wrapper to do the conversion.

The following is an example, and more detail is in the GitHub repo.

Basic Usage

defmodule Sample do
  import ExPrintf

  def test do
    printf("number = %d\n", [10])  # -> number = 10
    IO.puts sprintf("string = %s", ["abc"])  # -> string = abc

    format = parse_printf("format = %d %.2f\n")
    IO.inspect format  # -> "format = ~w ~.2f\n"
    :io.format(format, [10, 10.153])  # -> format = 10 10.15
  end
end

Sample.test

Sample Execution

$ mix run sample.ex
number = 10
string = abc
"format = ~w ~.2f\n"
format = 10 10.15
Advertisements

Posted on August 24, 2013, in Elixir, Software Design. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: