15 Jul 2013, 03:42
Dave_gnome_head_isolated_pragsmall

Dave Thomas (338 posts)

  • Find the library functions to do the following, and then use each in iex. (If there’s the word Elixir or Erlang at the end of the challenge, then you’ll find the answer in that set of libraries.)

    • Convert a float to a string with 2 decimal digits. (Erlang)
    • Get the value of an operating system environment variable. (Elixir)
    • Return the extension component of a file name (so return .exs if given "dave/test.exs" (Elixir)
    • Return the current working directory of the process. (Elixir)
    • Convert a string containing JSON into Elixir data structures. (Just find, don’t install)
    • Execute an command in your operating system’s shell
A Possible Solution
# Convert a float to a string with 2 decimal digits.
iex> :io.format("~.2f~n", [2.0/3.0])
0.67
:ok

# Get the value of an operating system environment variable.
iex> System.get_env("HOME")
"/Users/dave"

# Return the extension component of a file name
iex> Path.extname("dave/test.exs")
".exs"

# Return the current working directory of the process
iex> System.cwd
"/Users/dave/BS2/titles/elixir/Book/yourturn/ModulesAndFunctions"

# Convert a string containing JSON into Elixir data structures
# There are many options. Some, such as https://github.com/guedes/exjson,
# are specifically for Elixir. Others, such as https://github.com/hio/erlang-json
# are Elnag libraries that are usable from Elixir.

# Execute an command in your operating system's shell
iex> System.cmd("date")
"Sun Jul 14 15:04:06 CDT 2013\n"
04 Sep 2013, 14:22
Generic-user-small

Fred Hsu (2 posts)

Typo on the last line of the comment about JSON libraries.. “Elnag libraries”

21 Sep 2013, 03:37
Ac_simpsonized_pragsmall

Antonio Carlos da Graça Mota Durão de Souza (5 posts)

The worse answer:

# Return the extension component of a file name

String.split("dave/test.exs", ".") |> tl |> hd

AC

30 Nov 2013, 08:35
Generic-user-small

Kit Ko (2 posts)

# Return the extension component of a file name

The search function from elixir-lang.org/docs is so limited that I have to resort to Google.

site:elixir-lang.org/docs extension

10 Mar 2014, 23:45
Generic-user-small

Nathan Feaver (3 posts)

In the latest version of Elixir (0.12.4), using a shell command is done through the Mix module:

IO.write "Number of files in the current directory: "
Mix.Shell.cmd("ls -v | wc -l", &IO.write(&1))

# This method should be a shorthand but didn't work for me:
Mix.Shell.IO.cmd("ls -v | wc -l")
11 May 2014, 02:30
Generic-user-small

Thomas Chik (1 post)

erlang:float_to_list also converts float to string

iex> :erlang.float_to_list(2.0/3.0, [{:decimals, 2}])
'0.67'
15 Jun 2014, 11:32
Generic-user-small

Gary Rennie (1 post)

Return the extension component of a file name

pwd worked in IEX for me.

iex(5)> pwd
/home/gazler/development/elixir/programming-elixir/ch06

Docs

08 Jul 2014, 19:58
Generic-user-small

Patrick Oscity (5 posts)

Technically, the first task asks us to “Convert a float to a string with 2 decimal digits”, but your solution prints it:

 :io.format("~.2f~n", [2.0/3.0])

to obtain an actual string you can use

:io_lib.format("~.2f~n", [2.0/3.0]) |> hd

since the pipe does not appear in the book before, maybe you should write

[str] = :io_lib.format("~.2f~n", [2.0/3.0])

I think you should either update the task in the book to “print a float” or change the proposed solution here.

  You must be logged in to comment