[Proposal] Double pipeline operator ||>

75 views
Skip to first unread message

t...@scalpel.com

unread,
Mar 10, 2019, 5:37:23 AM3/10/19
to elixir-lang-core
The pipeline operator is one of the best features of the language, however there are some instances where you want to continue a chain of pipelines for clarity but can't because the argument order is not conducive to piping. For example here is an example when working with files:

defmodule Chess.PGN do
  def unique_checkmates(input_path, output_path)
    path
    |> File.stream!()
    |> Stream.chunk_every(21)
    |> Stream.filter(fn chunk -> checkmate?(Enum.at(chunk, 17)) end)
    |> Stream.uniq_by(fn chunk -> Enum.at(chunk, 19) end)
    |> Stream.map(fn chunk -> Enum.join(chunk, "") end)
    |> Enum.join("")
    |> write_file(output_path)
  end
  
  def write_file(content, output_path) do
    File.write!(output_path, content)
  end
end

Instead of having to add a dummy method I would like to double pipe into File.write which would be cleaner.

  ...
  ||> File.write!(output_path)

I think this could also be extended to triple, and quadruple pipes. Five seems like overkill, but I think you could make the argument for arbitrary levels of piping.

José Valim

unread,
Mar 10, 2019, 5:46:12 AM3/10/19
to elixir-l...@googlegroups.com
Hi Tom,

Extensions to the pipe operator have been proposed multiple times and always rejected. I recommend reading previous discussions for more information.

Have a good one,


José Valim
Skype: jv.ptec
Founder and Director of R&D


--
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 elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/c6f02f27-fca4-4d5b-9c88-e966faaf9c0c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Louis Pilfold

unread,
Mar 10, 2019, 6:33:05 AM3/10/19
to elixir-l...@googlegroups.com
Hi Tom!

While it will not be added to core there is no reason why you couldn't implement this functionality in your application or as a library.

Elixir does allow custom operators, though the ones you can use is limited so you won't be able to use `||>` `|||>` etc. Perhaps instead your macro could use a placeholder to indicate where the value should be inserted:

   data |> File.write!(path, _)

or

   data >>> File.write!(path, _)

Cheers,
Louis

Ricardo Harari

unread,
Mar 10, 2019, 5:40:43 PM3/10/19
to elixir-l...@googlegroups.com
Hi Tom

the order of the argument breaks sometimes and define an auxiliary function makes the code more readable.

for this sample you can simplify

...
|> (content fn -> File.write! (output_path, content) end).

but it will be more readable if we can write something like:

|> File.write! (Output_path, &1)

with macros you can invent your own pipe
see this project as an inspiration: https://github.com/ruby2elixir/plumber_girl

have fun!


--

Ricardo Harari

unread,
Mar 10, 2019, 5:44:07 PM3/10/19
to elixir-l...@googlegroups.com
oops...little mistake

...
|> (fn content -> File.write! (output_path, content) end).




Reply all
Reply to author
Forward
0 new messages