Skip to content

Commit

Permalink
add pipe (fix #47)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj committed Mar 11, 2015
1 parent b1d654a commit 671c832
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ Currently, the `@compat` macro supports the following syntaxes:

* `beginswith` is now `startswith` [#9583](https://github.com/JuliaLang/julia/pull/9583)

* `|>`, `>>`, `.>`, and `.>>` are now `pipe` [#10211](https://github.com/JuliaLang/julia/pull/10211)

## New macros

* `@inline` and `@noinline` have been added. On 0.3, these are "no-ops," meaning they don't actually do anything.
Expand Down
23 changes: 23 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,29 @@ if VERSION < v"0.4.0-dev+2014"
export sizehint!
end

if VERSION < v"0.4.0-dev+3413"
# based on pipe in base/process.jl:
function pipe(src::AbstractCmd; stdin=nothing, stdout=nothing, stderr=nothing, append::Bool=false)
if append && stdout === nothing && stderr === nothing
error("append set to true, but no output redirections specified")
end
if stdin !== nothing
cmd = stdin |> cmd
end
if stdout !== nothing
cmd = append ? cmd >> stdout : cmd |> stdout
end
if stderr !== nothing
cmd = append ? cmd .>> stderr : cmd .> stderr
end
return cmd
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...)
export pipe
end

function rewrite_dict(ex)
length(ex.args) == 1 && return ex

Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,6 @@ end
@test CartesianTest.f(1,2,3) == (1,2,3)
@test CartesianTest.f(1,2,3,4) == (1,2,3,4)
@test CartesianTest.f(1,2,3,4,5) == (1,2,3,4,5)

@test readall(pipe(`echo hello`, `sort`)) == "hello\n"
@test success(pipe(`true`, `true`))

0 comments on commit 671c832

Please sign in to comment.