small medium large xlarge

Dave_gnome_head_isolated_pragsmall
15 Jul 2013, 03:42
Dave Thomas (344 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</summary>

# 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"

</details>

Generic-user-small
04 Sep 2013, 14:22
Fred Hsu (3 posts)

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

Generic-user-small
30 Nov 2013, 08:35
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

Nathan-6kites_pragsmall
10 Mar 2014, 23:45
Nathan Feaver (8 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")
Generic-user-small
11 May 2014, 02:30
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'
Generic-user-small
15 Jun 2014, 11:32
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

Patrick_pragsmall
08 Jul 2014, 19:58
Patrick Oscity (13 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.

Profile_pragsmall
19 Nov 2014, 13:23
Michele Costa (2 posts)

The System.cmd/1 function is gone in Elixir version >1, replaced by System.cmd/3.

You now have to call Erlang :os.cmd/1 function instead.

Generic-user-small
24 Dec 2014, 12:08
Pierre Sugar (56 posts)

I especially had problems with System.cmd. I was using ls like System.cmd(ls, ["-l"]). The directory content was listed but I always got a weird error. Finally I realized that ls is a command itself and in course of System.cmd the command has to be called like so `System.cmd(“ls”, [“-l”]).


IO.puts "* Float to String"
IO.puts :erlang.float_to_list(1.2356, [{:decimals, 2}])
IO.puts "\n* Retrieve system variable"
IO.puts System.get_env["LANG"]
IO.puts "\n* Retrieve file extension"
IO.puts Path.extname("guess.exs")
IO.puts "\n* Print current working directory"
IO.puts System.cwd
IO.puts "\n* Find JSON library"
IO.puts "There are a lot of JSON libraries:"
IO.puts "exjson, exjsx, jaz, joken, jsex, json, jsx, jsxn, poison"
IO.puts "They all can be found at https://github.com/h4cc/awesome-elixir#json"
IO.puts "\n* Call a system command"
IO.puts inspect System.cmd("ls", ["-la"])

Generic-user-small
27 Jan 2015, 15:16
Andres Gallego (2 posts)

System.cmd(“date”) for execute a command in my operating system’s shell doesn’t work for me, instead I used

System.cmd("date", [])
Generic-user-small
05 May 2015, 11:34
Eric Roland (1 post)

I believe that this would work for the first answer as well:

Float.to_string(2.0/3.0, [decimals: 2])

  You must be logged in to comment