Last active
March 11, 2017 21:59
-
-
Save jbranchaud/4dadb61c212f2c86fe2b26bbe97e2f4b to your computer and use it in GitHub Desktop.
easily inspecting a thing that is being piped through functions in Elixir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule PipeInspect do | |
# | |
# Usage: | |
# | |
# Instead of temporarily breaking up a series of pipes to inspect the values: | |
# ``` | |
# partial_result = | |
# 1..100_000 | |
# |> Enum.map(&(&1 * 3)) | |
# | |
# IO.inspect partial_result | |
# | |
# partial_result | |
# |> Enum.filter(odd?) | |
# |> Enum.sum | |
# ``` | |
# | |
# We can just inline an inspector that plays well with our pipe. | |
# We can even include a message to be printed with it. | |
# | |
# ``` | |
# 1..100_000 | |
# |> Enum.map(&(&1 * 3)) | |
# |> pi() | |
# |> Enum.filter(odd?) | |
# |> pi("just the odd ones") | |
# |> Enum.sum | |
# ``` | |
# | |
def pi(thing) do | |
IO.inspect thing | |
thing | |
end | |
def pi(thing, msg) do | |
IO.puts msg | |
pi(thing) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment