[R] Pipe operator

Richard O'Keefe r@oknz @end|ng |rom gm@||@com
Wed Jan 4 07:55:45 CET 2023


This is both true and misleading.
The shell pipe operation came from functional
programming.  In fact the shell pipe operation
is NOT "flip apply", which is what |> is, but
it is functional composition.  That is
<in command = command(in)
command >out = let out = command
cmd1 | cmd2 = \x.cmd2(cmd1(x)).

Pragmatically, the Unix shell pipe operator does
something very important, which |> (and even
functional composition doesn't in F#):
<in cmd1 | cmd2 > out *interleaves* the computation
of cmd1 and cmd2, streaming the data.  But in R,
x |> f() |> g()
is by definition g(f(x)), and if g needs the value
of its argument, the *whole* of f(x) is evaluated
before g resumes.  This is much closer to what the
pipe syntax in the MS-DOS shell did, if I recall
correctly.



On Wed, 4 Jan 2023 at 17:46, Milan Glacier <news using milanglacier.com> wrote:

> With 50 years of programming experience, just think about how useful
> pipe operator is in shell scripting. The output of previous call becomes
> the input of next call... Genious idea from our beloved unix
> conversion...
>
>
> On 01/03/23 16:48, Sorkin, John wrote:
> >I am trying to understand the reason for existence of the pipe operator,
> %>%, and when one should use it. It is my understanding that the operator
> sends the file to the left of the operator to the function immediately to
> the right of the operator:
> >
> >c(1:10) %>% mean results in a value of 5.5 which is exactly the same as
> the result one obtains using the mean function directly, viz.
> mean(c(1:10)). What is the reason for having two syntactically different
> but semantically identical ways to call a function? Is one more efficient
> than the other? Does one use less memory than the other?
> >
> >P.S. Please forgive what might seem to be a question with an obvious
> answer. I am a programmer dinosaur. I have been programming for more than
> 50 years. When I started programming in the 1960s the only pipe one spoke
> about was a bong.
> >
> >John
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list