Skip to content

Commit

Permalink
doc: change pipeline operator | to |> [fixes #3523]
Browse files Browse the repository at this point in the history
  • Loading branch information
nolta committed Jun 25, 2013
1 parent fb79518 commit dbde41c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 22 deletions.
4 changes: 2 additions & 2 deletions doc/helpdb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -428,12 +428,12 @@
"),

("Generic Functions","Base","|","|(x, f)
("Generic Functions","Base","|>","|>(x, f)
Applies a function to the preceding argument which allows for easy
function chaining.
**Example**: \"[1:5] | x->x.^2 | sum | inv\"
**Example**: \"[1:5] |> x->x.^2 |> sum |> inv\"
"),

Expand Down
26 changes: 8 additions & 18 deletions doc/manual/running-external-programs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ Here's a simple example of actually running an external program::

julia> run(`echo hello`)
hello
true

The ``hello`` is the output of the ``echo`` command, sent to stdout.
The run method itself returns ``Nothing``, and throws an ``ErrorException``
Expand Down Expand Up @@ -184,7 +183,6 @@ Julia::
1
2
3
true

julia> first = "A"; second = "B";

Expand All @@ -194,7 +192,6 @@ Julia::
julia> run(ans)
1: A
2: B
true

The results are identical, and Julia's interpolation behavior mimics the
shell's with some improvements due to the fact that Julia supports
Expand All @@ -213,30 +210,27 @@ backticks, a pipe is always just a pipe::

julia> run(`echo hello | sort`)
hello | sort
true

This expression invokes the ``echo`` command with three words as
arguments: "hello", "\|", and "sort". The result is that a single line
is printed: "hello \| sort". Inside of backticks, a "\|" is just a
literal pipe character. How, then, does one construct a pipeline?
Instead of using "\|" inside of backticks, one uses Julia's ``|``
Instead of using "\|" inside of backticks, one uses Julia's ``|>``
operator between ``Cmd`` objects::

julia> run(`echo hello` | `sort`)
julia> run(`echo hello` |> `sort`)
hello
true

This pipes the output of the ``echo`` command to the ``sort`` command.
Of course, this isn't terribly interesting since there's only one line
to sort, but we can certainly do much more interesting things::

julia> run(`cut -d: -f3 /etc/passwd` | `sort -n` | `tail -n5`)
julia> run(`cut -d: -f3 /etc/passwd` |> `sort -n` |> `tail -n5`)
210
211
212
213
214
true

This prints the highest five user IDs on a UNIX system. The ``cut``,
``sort`` and ``tail`` commands are all spawned as immediate children of
Expand All @@ -250,18 +244,16 @@ Julia can run multiple commands in parallel::
julia> run(`echo hello` & `echo world`)
world
hello
true

The order of the output here is non-deterministic because the two
``echo`` processes are started nearly simultaneously, and race to make
the first write to the ``stdout`` descriptor they share with each other
and the ``julia`` parent process. Julia lets you pipe the output from
both of these processes to another program::

julia> run(`echo world` & `echo hello` | `sort`)
julia> run(`echo world` & `echo hello` |> `sort`)
hello
world
true

In terms of UNIX plumbing, what's happening here is that a single UNIX
pipe object is created and written to by both ``echo`` processes, and
Expand All @@ -275,7 +267,7 @@ apologies for the excessive use of Perl one-liners::

julia> prefixer(prefix, sleep) = `perl -nle '$|=1; print "'$prefix' ", $_; sleep '$sleep';'`

julia> run(`perl -le '$|=1; for(0..9){ print; sleep 1 }'` | prefixer("A",2) & prefixer("B",2))
julia> run(`perl -le '$|=1; for(0..9){ print; sleep 1 }'` |> prefixer("A",2) & prefixer("B",2))
A 0
B 1
A 2
Expand All @@ -286,7 +278,6 @@ apologies for the excessive use of Perl one-liners::
B 7
A 8
B 9
true

This is a classic example of a single producer feeding two concurrent
consumers: one ``perl`` process generates lines with the numbers 0
Expand All @@ -301,8 +292,8 @@ once, to be read by just one consumer process.)

Here is an even more complex multi-stage producer-consumer example::

julia> run(`perl -le '$|=1; for(0..9){ print; sleep 1 }'` |
prefixer("X",3) & prefixer("Y",3) & prefixer("Z",3) |
julia> run(`perl -le '$|=1; for(0..9){ print; sleep 1 }'` |>
prefixer("X",3) & prefixer("Y",3) & prefixer("Z",3) |>
prefixer("A",2) & prefixer("B",2))
B Y 0
A Z 1
Expand All @@ -314,7 +305,6 @@ Here is an even more complex multi-stage producer-consumer example::
A Z 7
B X 8
A Y 9
true

This example is similar to the previous one, except there are two stages
of consumers, and the stages have different latency so they use a
Expand All @@ -329,7 +319,7 @@ itself::
julia> dup = `perl -ne '$|=1; warn $_; print ".$_"; sleep 1'`
`perl -ne '$|=1; warn $_; print ".$_"; sleep 1'`

julia> run(gen | dup | dup)
julia> run(gen |> dup |> dup)
0
.0
1
Expand Down
4 changes: 2 additions & 2 deletions doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,11 @@ Generic Functions

Invoke a method for the given generic function matching the specified types (as a tuple), on the specified arguments. The arguments must be compatible with the specified types. This allows invoking a method other than the most specific matching method, which is useful when the behavior of a more general definition is explicitly needed (often as part of the implementation of a more specific method of the same function).

.. function:: |(x, f)
.. function:: |>(x, f)

Applies a function to the preceding argument which allows for easy function chaining.

**Example**: ``[1:5] | x->x.^2 | sum | inv``
**Example**: ``[1:5] |> x->x.^2 |> sum |> inv``

Iteration
---------
Expand Down

0 comments on commit dbde41c

Please sign in to comment.