dsounded

dsounded

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.

Most Liked

LostKobrakai

LostKobrakai

There‘s tap in elixir to do that since 1.12

Last Post!

dsounded

dsounded

Oh, this makes sense, so I can do:

|> tap(&insert/1)

Cool, thanks

Where Next?

Popular in Discussions Top

AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 20142 166
New
joeerl
I’m playing with Elixir - It’s fun. I think @rvirding does give Elixir courses these days. Re: files and database - when I given Erlang ...
New
pdgonzalez872
If this has been asked here before, please point me to where it was asked as I didn’t find it when I searched the forum. Maybe a mailing ...
New
chulkilee
Here are the list of HTTP client libraries/wrappers, and some thoughts on HTTP client in general. I’d like to hear from others how they w...
New
eteeselink
Hi all, In the last days, two things happened: A blog post titled “They might never tell you it’s broken” made the rounds. It’s about ...
New
fireproofsocks
I’ve been working on an Elixir project that has required a lot of scripting. I usually reach for Elixir because I like it more (and in th...
New
shishini
I think this twitter post and youtube video didn’t get as much attention as I hoped I am still new to Elixir, so can’t really judge ...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

We're in Beta

About us Mission Statement