Hacker News new | past | comments | ask | show | jobs | submit login
Ask HN: Pipes in Programming Languages
3 points by account-5 68 days ago | hide | past | favorite | 5 comments
Two questions really.

What programming languages use the pipe operator? I'm only really aware of shell scripting and R that use it.

Why don't more programming languages use the pipe operator? I write a lot of bash, powershell, and recently Nushell. I think piping things together is really powerful. There must be reasons why it's not in languages like python etc.




I'm assuming that by 'pipe operator' you mean the semantics, not specifically '|'. Of the programming languages I've used F# ('|>', https://camilotk.github.io/fsharp-by-example/chapters/pipe/), Elixir ('|>', https://elixirschool.com/en/lessons/basics/pipe_operator), and Clojure ('->' and '->>', https://clojure.org/guides/threading_macros), have pipe operators. The last two implement them as macros.


Aside of mentioned above languages, many others have similar concept but does not use any particular operator, e.g.: C# - LINQ https://learn.microsoft.com/en-us/dotnet/csharp/linq/ Java - Streams https://docs.oracle.com/en/java/javase/24/docs/api/java.base... Rust - Iterator trait https://doc.rust-lang.org/std/iter/trait.Iterator.html


Only programming languages that parse `|' have a pipe operator.

In other cases, Node.js has .pipe() function.

Other than that, if every class follows the same interface of strings for input and output, the program could pipe internally (and itself participate externally).

Data types let us specialize from plain strings to have better guarantees (safety, performance, specification), but now they are more specific in turn.

Functional programming wise, Python could map over a collection of things, as long as the function knows how to process elements.

Other ideas: fluent interfaces, method chaining, builder design pattern.


For this to be properly answerable you need to define your terms more clearly. Do you mean the | symbol, or do you mean a specific operation? In the latter case, exactly what semantics do you have in mind? How is the "pipe operator" in R, in your mind, related to the one in shell scripting? What does it do, fundamentally - and why would that have meaning in other programming languages?

I'll try to guess what you mean and answer as best I can; the summary is that I don't think you properly understand what's out there already, so it doesn't make sense to ask "why" questions yet.

If you simply mean the | operator, many programming languages - including Python - define | as a bitwise OR on some numeric types; and many - including Python - allow the user to define its semantics for user-defined types.

That said, it seems more like you really do have specific semantics in mind, rather than that symbol. I don't know R, but based on what my search query turned up first (https://www.statology.org/pipe-in-r/), it seems that by "pipe operator in R" you mean %>%, which allows for chaining operations on an input. In languages that support an object-oriented style with method calls, you do this by just... repeatedly calling methods, and having each return an instance of the type needed for the next step (in common cases, another instance of the same type). There's no need for any special syntax because it's an automatic consequence of how method calls work.

Code is written like that all the time in JavaScript, in fact. In the Python culture, though, there is a relatively strong expectation of command-query separation (https://en.wikipedia.org/wiki/Command%E2%80%93query_separati...). It's still possible to connect operations together this way, but there's an expectation that each operation creates a new result object (even if it's of the same type) and leaves the original input object unmodified. Thus, you're expected to do something with the overall result of such a chain, because your input data hasn't been modified. (This can also be viewed as a functional programming - https://en.wikipedia.org/wiki/Functional_programming - idiom.)


Great answer. Just wanted to add that JavaScript is also moving towards a similar immutable, functional style. The old style of chaining together a lot of calls is not as common anymore.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: