10 ms·
Why isn't the pipe a construct that has caught on in 'proper' languages?
by benj111 8y ago
Why isn't the pipe a construct that has caught on in 'proper' languages?
- arianvanp 8y agoIt is! It's the main way of programming in lazy functional programming languages like Haskell And many programming languages have libraries for something similar:. iterators in rust / c++, streams in java/c#, thinks like reactive
- benj111 8y agoHaskell was the only possible I found. Iterators don't really fully capture what a pipe is though? Theres no parallelism. And streams don't have the conceptual simplicity of a pipe?
- masklinn 8y ago> Iterators don't really fully capture what a pipe is though? Theres no parallelism. Pipes are concurrent, not necessarily parallel. Iterators are concurrent, and can be parallel (https://docs.rs/rayon/0.6.0/rayon/par_iter/index.html https://docs.rs/rayon/0.6.0/rayon/par_iter/index.html).
- lccarrasco 8y agoF# has a pipe operator
- Ndymium 8y agoElixir also has a pipe construct: f |> g(1) would be equivalent to g(f, 1)
- duckerude 8y agoMany functional languages have |> for piping, but chained method calls are also a lot like pipelines. Data goes from left to right. This javascript expression: [1, 2, 3].map(n => n + 1).join(',').length Is basically like this shell command: seq 3 | awk '{ print $1 + 1 }' | tr '\n' , | wc -c (the shell version gives 6 instead of 5 because of a trailing newline, but close enough)
- OJFord 8y agoBut each successive 'command' is a method on what's constructed so far; not an entirely different command to which we delegate processing of what we have so far. The Python: length(','.join(map(lambda n: n+1, range(1, 4))) is a bit closer, but the order's now reversed, and then jumbled by the map/lambda. (Though I suppose arguably awk does that too.)
- duckerude 8y agoThat's true. It's far from being generally applicable. But it might be the most "mainstream" pipe-like processing notation around. Nim has an interesting synthesis where a.f(b) is only another way to spell f(a, b), which (I think) matches the usual behavior of |> while still allowing familiar-looking method-style syntax. These are equivalent: [1, 2, 3].map(proc (n: int): int = n + 1).map(proc (n: int): string = $n).join(",").len len(join(map(map([1, 2, 3], proc (n: int): int = n + 1), proc (n: int): string = $n), ",")) The difference is purely cosmetic, but readability matters. It's easier to read from left to right than to have to jump around.
- ticklemyelmo 8y agoC# extension methods provide the same syntax, and it is used for all of its LINQ pipeline methods. It's amazing how effective syntactic sugar can be for readability.
- deleted 8y ago[deleted]
- msravi 8y agoIn Julia, this would be: 1:3 |> x->x+1 |> x->join(x,",") |> length
- duckerude 8y agoSmall correction (or it won't run on my system): 1:3 |> x -> map(y -> y+1, x) |> x -> join(x, ",") |> length All those anonymous functions seem a bit distracting, though https://github.com/JuliaLang/julia/pull/24990 https://github.com/JuliaLang/julia/pull/24990 could help with that.
- rakoo 8y agoIt doesn't look the same, bug go's up.Reader and io.Writer are the interfaces you implement if you want the equivalent of "reading from stdin"/"writing to stdout". Once implemented io.Copy is the actual piping operation.
- xaduha 8y agoTo take any significant advantage of it you need to use data-driven, transformational approach of solving something. But funny thing is once you have that it's not really a big deal even if you don't have a pipe operator.
- barrkel 8y agoMonads are effectively pipes; the monad controls how data flows through the functions you put into the monad, but the functions individually are like individual programs in a pipe.
- chriswarbo 8y agoI'm not sure I agree with this. Function composition is more directly comparable to pipes, whereas I tend to think of monads as collapsing structure (i.e. `join :: m (m a) -> m a`)
- barrkel 8y agoSome of the most common monads (especially lists and other collection-like things) feel very much like pipes. list.select { |x| x.foo > 10 }.map { |x| x.bar }... Etc. I wouldn't make the same argument about the IO monad, which I think more in terms of a functional program which evaluates to an imperative program. But most monads are not like the IO monad, in my experience at least.
- chriswarbo 8y ago> list.select { |x| x.foo > 10 }.map { |x| x.bar }... Forgive me if I'm misreading this syntax, but to me this looks like plain old function composition: a call to `select` (I assume that's like Haskell's `filter`?) composed with a call to `map`. No monad in sight. As I mentioned, monads are more about collapsing structure. In the case of lists this could be done with `concat` (which is the list implementation of monad's `join`) or `concatMap` (which is the list implementation of monad's `bind` AKA `>>=`).
- barrkel 8y agoNope, it's not. It's Ruby, and the list could be an eager iterator, an actual list, a lazy iterator, a Mabye (though it would be clumsy in Ruby), etc. And monads are not "more about collapsing structure". They are just a design pattern that follows a handful of laws. It seems like you're mistaking their usefulness in Haskell for what they are. A lot of other languages have monads either baked in or an element of the design of libraries. Expand your mind out of the Haskell box :)
- mamcx 8y agoOthers have show languages have some kind of pipe support, but not exactly as the shell. The shell have the weird(?) behavior of ONE input and TWO outputs (stdout, stderr). Also, can redirect in both directions. I think a language to be alike pipes, it need each function to be alike: fun open(...) -> Result(Ok,Err) and have the option of not only chain the OK side but the ERR: open("file.txt") |> print !!> raise |> print exist something like this???
- dpwm 8y agoOCaml has had the pipe operator |> since 4.01 [0] It actually somewhat changes the way you write code, because it enables chaining of calls. It's worth noting there's nothing preventing this being done before the pipe operator using function calls. x |> f |> g is by definition the same as (g (f x)). In non-performance-sensitive code, I've found that what would be quite a complicated monolithic function in an imperative language often ends up as a composition of more modular functions piped together. As others have mentioned, there are similarities with the method chaining style in OO languages. Also, I believe Clojure has piping in the form of the -> thread-first macro. [0] https://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html https://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasive...
- masklinn 8y agoIMO the really useful part of pipes is less the operator and more the lazy, streaming, concurrent processing model. So lazy collections / iterators, and HoFs working on those. The pipe operator itself is mostly a way to denote the composition in reading order (left to right instead of right to left / inside to outside), which is convenient for readability but not exactly world-breaking.
- beaconstudios 8y agoWhat you're describing sounds a lot like reactiveX - http://reactivex.io/ http://reactivex.io/ I only have experience using RxJS, but it's incredibly powerful.
- msravi 8y agoJulia has a pipe operator, which applies a function to the preceding argument: julia> 1:5 |> x->x.^2 |> x->2x 5-element Array{Int64,1}: 2 8 18 32 50
- agumonkey 8y agobecause a | b | c is equivalent to the very tradtional c(b(a())) and in FP it was infixed as `.`
- nicwilson 8y agoIt has, D's uniform functional syntax makes this as easy as auto foo = some_array.filter!(predicate).array.sort.uniq; for the unique elements of the sorted array that satisfy the predicate pred.
- deleted 8y ago[deleted]
- gpderetta 8y agoThere is a tradition in C++ of overloading operator| for pipelining range operations. Your mileage may vary.
- diggan 8y agoClojure has something that you could call a pipe almost. `->` passes the output from one form to the next one. This example has a nested hash map where we try to get the "You got me!" string. We can either use `:a` (keyword) as a function to get the value. Then we have to nest the function calls a bit unnaturally. Or we can use the thread-first macro `->`, which is basically a unix pipe. user=> (def res {:a {:b {:c "You got me!"}}}) #'user/res user=> res {:a {:b {:c "You got me!"}}} user=> (:c (:b (:a res))) "You got me!" user=> (-> res :a :b :c) "You got me!" Thinking about it, Clojure advocates having small functions (similar to unix's "small programs / do one thing well") that you compose together to build bigger things.
- lincpa 8y agoClojure: The Pure Function Pipeline Data Flow https://github.com/linpengcheng/PurefunctionPipelineDataflow https://github.com/linpengcheng/PurefunctionPipelineDataflow
- demirev 8y agoTo add to the list, pipes are also heavily used in modern R
- VladimirGolovin 8y agoIt has, in the form of function composition, as other replies show. However, the Unix pipe demonstrates a more interesting idea: composable programs on the level of the OS. Nowadays, most of the user-facing desktop programs have GUIs, so the 'pipe' operator that composes programs is the user himself. Users compose programs by saving files from one program and opening them in another. The data being 'piped' through such program composition is sort-of typed, with the file types (PNG, TXT, etc) being the types and the loading modules of the programs being 'runtime typecheckers' that reject files with invalid format. On the first sight, GUIs prevent program composition by requiring the user to serve as the 'pipe'. However, if GUIs were reflections / manifestations of some rich typed data (expressible in some really powerful type system, such as that of Idris), one could imagine the possibility of directly composing the programs together, bypassing the GUI or file-saving stages.
- sideeffffect 8y agomaybe I'm being overly pedantic, but people seem to be confused about this: the pipes in your typical functional language (`|>`) is not a form of function composition, like ``` f >> g === x -> g(f(x)) ``` but function application, like ``` f x |> g === g(f(x)) x |> f |> g // also works, has the same meaning f |> g // just doesn't work, sorry :( ```
- chriswarbo 8y ago> the pipes in your typical functional language (`|>`) is not a form of function composition What is a "typical functional language" in this case? I don't think I've come across this `|>` notation, or anything explicitly referred to as a "pipe", in the functional languages I tend to use (Haskell, Scheme, StandardML, Idris, Coq, Agda, ...); other than the Haskell "pipes" library, which I think is more elaborate than what you're talking about.
- jaster 8y agoAs others have mentioned, the pipe construct is present in many languages (or can be added). A small additional bit of information: this style is called "tacit" (or "point-free") programming. See https://en.wikipedia.org/wiki/Tacit_programming https://en.wikipedia.org/wiki/Tacit_programming (Unix pipes are even explicitly mentioned in the articles as an example)
- jfhufl 8y agoHmmm, well that's why I like Ruby and other languages with functional approaches. Method chaining and blocks are very similar to pipes to me. cat /etc/passwd | grep root | awk -F: '{print $3}' ruby -e 'puts File.read("/etc/passwd").lines.select { |line| line.match(/root/) }.first.split(":")[2]' A little more verbose, but the idea is the same. https://alvinalexander.com/scala/fp-book/how-functional-programming-is-like-unix-pipelines https://alvinalexander.com/scala/fp-book/how-functional-prog...
- anthk 8y agoawk -F: '/root/ {print $3}' < /etc/passwd
- cryptonector 8y agoHaskell has the Conduit library. There's jq's notion of pipes (which isn't the same as Unix pipes, but still).