sb logoToday I Learned

Inspecting Pipelines

When debugging a pipeline, you can inspect any of the intermediate steps using IO.inspect/2:

"Sphinx of black quartz, judge my vow."
|> String.downcase()
|> IO.inspect(label: "Downcased")
|> String.replace(~r/[[:punct:]]/, "")
|> IO.inspect(label: "Punctuation Removed")
|> String.split(" ")
|> IO.inspect(label: "Words")
|> Enum.reduce(%{}, fn word, acc ->
  Map.update(acc, word, 1, &(&1 + 1))
end)
|> IO.inspect(label: "Counts")

The label option formats the output to make it clear which step is being inspected:

Downcased: "sphinx of black quartz, judge my vow."
Punctuation Removed: "sphinx of black quartz judge my vow"
Words: ["sphinx", "of", "black", "quartz", "judge", "my", "vow"]
Counts: %{
  "black" => 1,
  "judge" => 1,
  "my" => 1,
  "of" => 1,
  "quartz" => 1,
  "sphinx" => 1,
  "vow" => 1
}

This works because IO.inspect/2 always returns the first argument passed to it!