15 Jul 2013, 03:42
Dave_gnome_head_isolated_pragsmall

Dave Thomas (337 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")
  You must be logged in to comment