Skip to content

Commit

Permalink
add varargs form of pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Feb 18, 2015
1 parent 11cd2dc commit 7d2d250
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 2 additions & 0 deletions base/process.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ end
pipe(cmd::AbstractCmd, dest) = pipe(cmd, stdout=dest)
pipe(src::Union(Redirectable,AbstractString), cmd::AbstractCmd) = pipe(cmd, stdin=src)

pipe(a, b, c, d...) = pipe(pipe(a,b), c, d...)

typealias RawOrBoxedHandle Union(UVHandle,UVStream,Redirectable,IOStream)
typealias StdIOSet NTuple{3,RawOrBoxedHandle}

Expand Down
9 changes: 6 additions & 3 deletions doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -659,11 +659,14 @@ System
The ``dir`` keyword argument can be used to specify a working directory for the
command.

.. function:: pipe(from, to)
.. function:: pipe(from, to, ...)

Create a pipeline from a data source to a destination. The source and destination can
be commands, I/O streams, or strings, but at least one must be a command. Strings refer
to filenames.
be commands, I/O streams, strings, or results of other ``pipe`` calls. At least one
argument must be a command. Strings refer to filenames.
When called with more than two arguments, they are chained together from left to right.
For example ``pipe(a,b,c)`` is equivalent to ``pipe(pipe(a,b),c)``. This provides a more
concise way to specify multi-stage pipelines.

**Examples**:
* ``run(pipe(`ls`, `grep xyz`))``
Expand Down
6 changes: 3 additions & 3 deletions test/spawn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ out = readall(`echo hello` & `echo world`)
@test (run(`printf " \033[34m[stdio passthrough ok]\033[0m\n"`); true)

# Test for SIGPIPE being treated as normal termination (throws an error if broken)
@unix_only @test (run(pipe(pipe(yes,`head`),DevNull)); true)
@unix_only @test (run(pipe(yes,`head`,DevNull)); true)

begin
a = Base.Condition()
Expand All @@ -44,8 +44,8 @@ if false
prefixer(prefix, sleep) = `perl -nle '$|=1; print "'$prefix' ", $_; sleep '$sleep';'`
@test success(pipe(`perl -le '$|=1; for(0..2){ print; sleep 1 }'`,
prefixer("A",2) & prefixer("B",2)))
@test success(pipe(pipe(`perl -le '$|=1; for(0..2){ print; sleep 1 }'`,
prefixer("X",3) & prefixer("Y",3) & prefixer("Z",3)),
@test success(pipe(`perl -le '$|=1; for(0..2){ print; sleep 1 }'`,
prefixer("X",3) & prefixer("Y",3) & prefixer("Z",3),
prefixer("A",2) & prefixer("B",2)))
end

Expand Down

1 comment on commit 7d2d250

@StefanKarpinski
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these are now actually more concise than the original versions.

Please sign in to comment.