Even though I have used Elixir for several years now it still
amazes me how much you can achieve with a single pipeline in Elixir language. The other day I had to find the total size of the files in a given dir. I thought of just parsing the output of the ls -l
command and then adding up all the file sizes to get the total. Here is what I ended up with.
"ls -l /Users/parth/temp/github_events/2015/"
|> to_charlist()
|> :os.cmd()
|> to_string()
|> String.split("\n")
|> Enum.reject(&String.starts_with?(&1, "total"))
|> Enum.map(&String.split(&1, ~r/\s+/))
|> Enum.map(&Enum.at(&1, 4))
|> Enum.reject(&(is_nil &1))
|> Enum.map(&(String.to_integer &1))
|> Enum.sum()
|> then(&(&1/:math.pow(10, 9)))
|> Float.round(2)
|> IO.inspect(label: "Size in GBs => ")
P.S In practise this code might be shortened by using the standard library functions associated with file handling. But it was fun coming up with this!
Top comments (0)