Hello alchemists,

I have a simple yet powerful proposal for an inspection function that can 
apply a desired function in a value and return the value itself.

For example, if today I want to inspect the keys of a given map inside a 
chain of pipes, I need to do the following:

...
|> map
|> inspect_keys()
|> ...

defp inspect_keys(map) do
  IO.inspect(Map.keys(map))
  map
end

Another example: if I want to count an Enumerable inside a function:

defp my_function(param) do
  list = build_list(param)
  IO.inspect(Enum.count(list))
  list
end

With a function that can inspect using a function AND return the value 
itself, both examples can be simplified:

...
|> map
|> IO.inspect_with(&Map.keys/1)
|> ...

defp my_function(param) do
  param
  |> build_list()
  |> IO.inspect_with(&Enum.count/1)
end

Since the second parameter from IO.inspect/2 is a keyword list of options, 
@v0idpwn (from Elixir Brasil Telegram group) suggested a new option called 
:apply. For example:

IO.inspect([1, 2, 3], apply: &Enum.count/1)

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/b0a390d1-9530-492c-b1fb-8ead8c9b938an%40googlegroups.com.

Reply via email to