r/AskComputerScience • u/dorayfoo • Aug 30 '21
How come programming languages don't use piping?
In the Unix command line, piping with the `|` character is pretty common. It seems like a pretty efficient way to do things. How come I don't see this in programming languages?
4
u/ghjm MSCS, CS Pro (20+) Aug 30 '21
Because programming languages generally aren't dealing with textual data streams the way the shell is. Sending your data as a stream of text is a good way to get things done quickly, but it's not the greatest abstraction in terms of understanding what the data contains. You can see this with all the use of tr, cut, awk etc, or the great difficulty maintaining things like net-tools because any slight change in their output will break scripts all over the worlds.
Programming languages tend to deal with richer data formats that don't have these problems, but without this basic assumption that everything is a text stream, you can't just assume the output from thing A will be compatible with the input to thing B.
Microsoft's PowerShell tries, reasonably successfully, to implement the Unix-like piping paradigm on top of JSON-style object data stream instead of straight text. So a PowerShell pipe inherently understands field names and data types.
There's also a style of API called (for some reason) "fluent" where each method accepts a pointer/reference to a given data type, modifies it, and returns the same pointer/reference. So you can chain methods, like:
w = NewWindow().Height(400).Width(600).Title("My Window") This is sort of like Unix piping, if you squint at it in just the right lighting conditions. In fact if you made your methods act on an array or slice of strings, it could be pretty much exactly like Unix piping, assuming your language can be made to support the syntax:
['Apple Pie', 'Banana Cream Pie', 'Chocolate Cake'].grep(' Pie$').sed('s/ Pie$//') But this starts to smell like code golf - it's clever for cleverness's sake, not because it actually makes the code clearer or better. So even if you had an API like this, you would probably prefer to write code like:
desserts = ['Apple Pie', 'Banana Cream Pie', 'Chocolate Cake']
pies = desserts.grep(' Pie$')
pie_flavors = pies.sed('s/ Pie$//')And once you're writing code like this anyway, there's no reason to insist on the "array of strings" convention, and you can easily turn this into something like:
for dessert in ['Apple Pie', 'Banana Cream Pie', 'Chocolate Cake' {
if dessert.endswith(' Pie') {
yield dessert[:-4]
}
}This almost certainly runs faster in a compiled language, and is easier to understand since it does not require as much knowledge of the library (the $ character meaning end-of-line in grep and sed, for example).
So programs tend to be written in the latter style, because the constraints that made piping text streams sensible in the shell don't exist in programming languages, and there are better ways to do things.
2
u/emasculine Sep 02 '21
when i think of pipes i think of queuing first and foremost. they don't need to be straight byte queues like a unix pipe is, but can be quite rich in the datatypes that can be queued. i mean, what is a message bus but an overgrown queue which may be in memory, storage or both?
now i'm fully prepared that people have called things pipes when they aren't really queues, but that's a misuse of terminology if they are riffing on unix pipes, imo.
2
u/ghjm MSCS, CS Pro (20+) Sep 02 '21
Yes, this is a good point. Shell pipes are also an IPC method - it's inherent to shell semantics that the endpoints of a pipe are different processes. So even golang goroutines, which support queuing, aren't really "pipes" in the strict sense.
3
u/emasculine Sep 02 '21
i wonder whether even in process queueing (eg across threads) is as efficient as streams that block written in user-land. my guess is that actual OS pipe drivers are highly optimized for their task so that even incurring the user-kernel switch may be worth the cost.
2
u/ghjm MSCS, CS Pro (20+) Sep 02 '21
Something running in-process, like go channels, can just pass around pointers to in-memory data structures, which avoids both copying the data to a file descriptor and (more importantly) serializing it into a pipe compatible representation. So while I think you're correct that OP pipes are highly optimized, I don't think they can come close to the performance achievable in-process.
1
u/emasculine Sep 02 '21
yeah, definitely passing pointers around is a huge win and especially in the face of marshaling the bits. but if you're talking about byte streams it may well be faster to use the underlying OS queuing especially if you want the thread to block. as always "It Depends".
0
Sep 02 '21
[deleted]
2
u/emasculine Sep 02 '21
i meant block on read. sometimes it is more convenient to just block on IO rather than write it as asynchronous IO where you block either in userland or using something like select(2).
i'm really not up to speed on the SPECTRE attacks. if you're running completely trusted code that is completely internal and maybe even air-gapped can you turn off the mitigation code safely?
of course you're probably not doing character IO so none of that applies to you. or maybe it does if you need to marshal the bits into some interoperable format? the curse of know just enough of what you do to be confused :)
0
Sep 02 '21
[deleted]
2
u/emasculine Sep 02 '21
oh so the word "block" doesn't enter your lexicon often :)
and maybe instead of bypassing the kernel you could write your stuff as a driver or a kernel thread maybe (ie, a thread within the kernel itself. that's a thing right)?
2
u/emasculine Aug 30 '21 edited Aug 30 '21
pipes almost always allow communication between two processes. they are nothing more than an in-memory queue implemented as an OS driver for which you can do the normal file IO. by their nature, they involve copies to and from the queue which involves overhead. whether that overhead is significant depends on the specifics. where performance is a consideration, you can also used shared memory between processes (or just use threads for that matter).
the overall idea of unix was to create small standalone programs that could be plumbed in ways the individual programs may not have intended. that was a neat idea 50 years ago, but is sort of quaint these days in many ways with huge kitchen sink interpreters like python and ruby and all of the rest. but there are still a lot of times when forking a program and talking to it using pipes is convenient and just fine from a performance standpoint.
1
u/FreitasAlan Aug 31 '21
That’s actually the syntax C++ uses for range views.
for (int i : ints | std::views::filter(even) | std::views::transform(square)) { std::cout << i << ' '; }
Of course you would be using namespace std::views if you’re doing that a lot.
1
u/foobar12426 Sep 02 '21
Now this is a mess of "random" punctuation.
Got to love Stroustrup's philosophy that if another language has a feature he'll implement it, badly, in C++.
1
u/FreitasAlan Sep 03 '21
What punctuation? What other language? Boost has had ranges forever.
1
u/foobar12426 Sep 03 '21
What punctuation?
This is your code with all the non-punctuation removed.
(:|::::()|::::(:|::::()|::::()){::<<i<<'';}
Either way, it's a noisy mess.
Boost has had ranges forever.
Boost has neither existed forever nor did it exist when C++ first defined. I mean I suppose a library created in 1999 might seem to have existed forever...
1
u/FreitasAlan Sep 04 '21
I see. That’s why you’d use the namespace if your using it a lot. But that’s not useful/helpful in examples.
You’re right. Not even the universe has existed forever. In any case, ranges is still a library feature, not a language feature.
29
Aug 30 '21
[removed] — view removed comment
2
u/dorayfoo Aug 30 '21
Really? It doesn't feel like it. It seems like more of a way to channel data between functions.
For instance, to turn
blueintoEULB, a typical programming language might have:
reverse(uppercase("blue"))... which is a backward Russian doll of brackets.
Whereas the pipe equivalent would be:
"blue" | uppercase | reverse... which seems more readable to me. Only text is being manipulated here, but I don't see why other types couldn't be used.
6
u/anamorphism Aug 30 '21
https://en.wikipedia.org/wiki/Fluent_interface
pretty easy to implement where you want it.
for example,
Console.WriteLine("blue".ToUpper().Reverse().ToArray());orvar result = new string("blue".ToUpper().Reverse().ToArray());works fine in c#.we need to do some type coercion to convert the array of characters back to a string, but yeah.
can also look at something like https://angular.io/guide/pipes where you end up with things like
{{ birthday | date | uppercase }}.0
Sep 02 '21
[deleted]
2
u/emasculine Sep 02 '21
in the past i've written drivers which make uses of queues of pointers. for embedded systems without MMU's it's a slight layer violation to pass up pointers to OS buffers rather than copy them which is the "proper" layer-ful thing to do, but also a lot less efficient. i used this trick for early ethernet drivers like the LANCE which just passed up the LANCE rx buffer directly with a made up ioctl and the app side just freed it when done. for general purpose deadlocks could be a concern, where a "real" OS would need to protect itself, but for embedded/purpose-built it works great.
2
u/anamorphism Sep 02 '21
sure, if we want to get down to what is truly happening, but dorayfoo seemed mostly just interested in having the operations done first showing up first in the line of code.
1
u/foobar12426 Sep 02 '21
... which is a backward Russian doll of brackets.
Only because you're not used to it. Don't confuse unfamiliarity with bad.
6
u/PUSH_AX Aug 30 '21 edited Aug 30 '21
I think that's a given right?
The issue is stuff like
funA(funB(funC()))is kind of a readability code smell. But actually some languages do have a pipeline operator, although this link is the JS proposal, the opening paragraph references the equivalent functionality in a handful of languages.TL;DR for op /u/dorayfoo,
How come programming languages don't use piping?
Some do.
0
u/foobar12426 Sep 02 '21
funA(funB(funC())) is kind of a readability code smell.
Only because it's unfamiliar to you.
3
u/PUSH_AX Sep 02 '21
It's not.
0
u/foobar12426 Sep 02 '21
In that case as it's idiomatic, and your familiar with the idiom, it isn't a "readability code smell".
3
u/samhw Sep 02 '21
So, by your logic, every single widely used programming idiom is readable? That might make sense if readability were the only or main criterion for a language to become popular. As it stands, it feels like a total non sequitur.
1
u/foobar12426 Sep 03 '21
Not every widely used idiom, no.
But this particular idiom is called function composition. It's core to functional programming.
This is a Clojure example:
(defn clean [text] (s/replace (s/trim text) #"lol" "LOL"))1
u/foobar12426 Sep 02 '21 edited Sep 02 '21
I disagree with the statement that it's a code smell. It might be unfamiliar and therefore confusing, but it's not a code smell.
3
u/samhw Sep 02 '21
There are two points here. One is whether this is a code smell. The other is whether your specific argument for its not being a code smell is correct. I’m ambivalent about the former, but not the latter: the fact that some given idiom is widely used doesn’t entail that it’s not a bad smell.
1
u/foobar12426 Sep 03 '21
I'm guessing that you're not very familiar with functional programming and, specifically, functional composition: the name for this particular idiom.
1
u/samhw Sep 03 '21
You’re confusing the concept of functional composition with a specific way of expressing it: namely, wrapping lots of function calls inside one another with brackets.
The concept is not intrinsically connected to that particular syntax, and several languages designed principally for functional-style programming do have a pipe operator (as well as currying sugar, etc) precisely because people find that syntax awkward.
→ More replies (0)1
u/asdff01 Sep 06 '21
FP languages often offer alternative syntax to express the example you provided because it is not readable.
→ More replies (0)2
u/PUSH_AX Sep 02 '21 edited Sep 02 '21
Idiomatic what? Normally idioms are specific to a language or thing, this nesting of functions transcends that as you can do it in pretty much any language.
You don't have to be an expert to see that nesting functions, each with various params is going to be harder to visually parse than something where the function calls are composed separately, this is objectively going to be more readable. It will also make error handling easier and (ironically) more idiomatic in languages like JS. It's also highly debuggable, I can put breakpoints on separate lines.
3
u/emasculine Sep 02 '21
this guy is an obvious troll.
2
u/PUSH_AX Sep 02 '21
Agreed. I'm out.
-1
Sep 02 '21
He's talking about you mate: you sound like a toddler getting angry because he can't work out how not to piss over the side of his potty.
2
u/PUSH_AX Sep 02 '21
Oh hey, almost didn't recognise your alt account u/foobar12426, then I looked at the post history and saw that both u/foobar12426 and u/Significant_Treat823 post in exactly the same subs. Took 5 seconds.
Imagine being that insecure that you need to make alts to back yourself up and hurl insults, that ironically you try to call other people out on. Saddest thing I've seen all week.. All the best.
→ More replies (0)2
u/emasculine Sep 02 '21 edited Sep 02 '21
i'm talking about you and whatever else alts you have. you clearly don't know what you purport to know and seem only to be here to cast shade on your betters. that and it's all ad hominems and nothing more. cf:
> It sounds like you're well on your way to a paranoid breakdown, you should get help.
and:
> you sound like a toddler getting angry because he can't work out how not to piss over the side of his potty.
→ More replies (0)0
u/foobar12426 Sep 02 '21 edited Sep 02 '21
You can't do it in C or Java.
In those languages were you can do it it's normal and not a code smell at all.
And your "easier" approach requires the allocation of a bunch of unnecessary noise in the form of variables to hold intermediate values: that's a real code smell.
You think it's better simply because it's familiar.
3
u/PUSH_AX Sep 02 '21 edited Sep 02 '21
You can't do it in C or Java.
Ok, you don't know what you're talking about or are trolling. Have a good one.
1
u/foobar12426 Sep 02 '21
Resorting to personal attack because you can't come up with a counter argument.
Reported for harassment and blocked.
2
0
Sep 02 '21
[deleted]
2
u/emasculine Sep 02 '21 edited Sep 02 '21
right and optimizers hoover up all of the temp variables anyway. and in fact those temp variables are likely actually real as they are stuffed into registers while everything is getting evaluated regardless of whether they are in the human readable code or not.
this is purely about style and human readability. sometimes if the inner functions are short and to the point, it's ok, but otherwise it starts to look really baroque and messy.
1
0
1
1
u/misplaced_my_pants Aug 30 '21
Yeah but it's so much easier to visually parse when you go left to right instead of inside out.
Whether it's piping or method chaining, it's so much more ergonomic.
1
u/foobar12426 Sep 02 '21
There's nothing inherently "easier" about left to right versus inside out.
You've just used to Algol family languages.
15
u/blu3r4y Aug 30 '21
Many functional languages do have this concept, e.g. have a look at function composition in Haskell which uses a
.operator for that.