Another pipe enhancement topc: |>> (Ruby Object#tap like pipe)

Hello dear community!

So it came from my daily tasks, it’s just a proposal which once approved I’d gladly try to implement.

Before creating the topic I tried to search if something like that was already asked, so seems like not.

The idea is to have a pipe which returns the value that was passed as a first argument from the previous pipe back (even though the function itself can return something else) eg:

defmodule Test do
  def call do
    "users.csv"
    |> File.stream!() # raw data
    |> prepare() # list of maps
    |>> insert() # the same list of maps
    |>> send_emails() # the same list of maps
    |> log()
  end
  
  ...
  
  defp insert(records) do
    Repo.insert(User, records)
  end

  defp send_emails(records) do
    MailService.send(to: records)
  end

  defp log(records) do
    # do some logs related things
  end
end

Right now it’s possible to design it in a way:

defp insert(records) do
  Repo.insert(User, records)
  records
end

defp send_emails(records) do
  MailService.send(to: records)
  records
end

So I’d like to hear your thoughts on this. The benefits I can see are: you can still test in isolation (it’s original returning value) and also compose related things into one pipe. (|>> shows it goes one step further (like to pass to the one after the next pipe)

It’s pretty subjective though that’s why I want to ask your opinion on this before I start investigating the pull request.

There‘s tap in elixir to do that since 1.12

4 Likes

Oh, this makes sense, so I can do:

|> tap(&insert/1)

Cool, thanks