Pipe operator (|
in shell) makes codes more concise and easy to read.
R Ecosystem
Pipe operator for R is %>%
defined in package
magrittr:
library(dplyr)
probes <- 'probes.csv' %>%
read.table(header = FALSE, sep = ',',
col.names = c('poinitID', 'fullDesc', 'remark'),
colClasses = c('integer', 'character', 'integer')) %>%
filter(remark==0)
See magrittr: Simplifying R code with pipes for detailed explanations and examples.
The rules are simple: the object on the left hand side is passed as the first argument to the function on the right hand side. So:
my.data %>% my.function
is the same asmy.function(my.data)
my.data %>% my.function(arg=value)
is the same asmy.function(my.data, arg=value)
For functions with only 1 parameter, there's no need to write parenthesis.
Python Family
Pipe operator for Coconut is |>
:
"hello, world!" |> print
BTW: Elixir use the same operator for pipeline.
With Toolz package:
from toolz import pipe
pipe(12, sqrt, str)
Pandas also has a pipe()
method for dataframe object.
See Functional pipes in python like %>% from R's dplyr
for more details.
Lisp Family
Pipe operator for Hy and Clojure is ->
:
=> (defn output [a b] (print a b))
=> (-> (+ 4 6) (output 5))
10 5
Haskell
In Haskell the dot operator .
plays the similar role through
Function composition:
reverse . sort
is equivalent to\x -> reverse (sort x)
.