Pipe second tuple member

In Dave Thoma’s programming book there is the following line of code:

IO.puts File.read!(​ *"* ​​ */usr/share/dict/words"* ​)
|> String.split

The exclamation mark ! returns the result, which would otherwise be returned in the {:ok, result} tuple.
This made me wonder if there is a method of piping only selected parameters to the next function in the pipe.

If I have:

{:ok, some_value} |> OI.puts      #Is there any way of printing the value of some_value?

There are some hex-packages available that claim to be able to deal with piping :ok tuples, but personally I tend to do one of the following (in order of preference):

  • Use with rather than piping
  • write a function that extracts the value and also deals with error
  • Use an anonymous function in the pipe
  • Pipe into elem/2

edit Add elem/2 to the list.

File.read(​ *"* ​​ */usr/share/dict/words"* ​)
|> elem(1)
|> String.split()

No error checking or anything then, you might pipe an error value for example, with, the ok library, the exceptional library are all better at that.



Seems using elem as an intermediary is the easiest and dirtiest method of passing specific tuple elements.