# How to use pipe operator in Mathematica?

t = Table[i1 + i2 i3, {i1, 2}, {i2, 3}, {i3, 2}]
ArrayDepth[t]


How to use "pipe operator" in Mathematica Version 11.0?

In R, with library magrittr I can use a pipeline specification as below:

Table[i1 + i2 i3, {i1, 2}, {i2, 3}, {i3, 2}] %>% ArrayDepth[.]


How to use "pipeline specifications" in Mathematica?

• You could use the postfix // operator, like Table[i1 + i2 i3, {i1, 2}, {i2, 3}, {i3, 2}] // ArrayDepth or Table[i1 + i2 i3, {i1, 2}, {i2, 3}, {i3, 2}] // ArrayDepth[#] & Commented Nov 25, 2020 at 13:20
• "Pipeline operator" discussions at MSE can be found by the search monadic programming. Commented Nov 25, 2020 at 13:23
• Maybe RightComposition ? Which is /* Commented Nov 25, 2020 at 15:49

Perhaps you're looking for

Table[i1 + i2 i3, {i1, 2}, {i2, 3}, {i3, 2}] // ArrayDepth
(*3*)

• If I need more pipe,can I use Table[i1 + i2 i3, {i1, 2}, {i2, 3}, {i3, 2}] // ArrayDepth // function2? Commented Nov 25, 2020 at 13:25
• @kittygirl Yes, see Table[i1 + i2 i3, {i1, 2}, {i2, 3}, {i3, 2}] // ArrayDepth // N // Sqrt. Commented Nov 25, 2020 at 13:27
• Yes! What you call "pipe" is "PostFix" in Mathematica I think. Commented Nov 25, 2020 at 13:27

In order to utilize the post fix operator // in WL in the way the R library magrittr implements/uses %>% several things have to be kept in mind.

• R's magrittr operator %>% :

1. Assumes that the pipeline value is the first and only argument by default

• Many of the tidyverse package functions rely heavily on this
2. If a function needs only the first argument to be specified only the function name can be used

3. Different argument location of the pipeline value can be specified with .

• In WL using the operator // :

1. Often translating R tidyverse pipeline workflows in WL pipelines with // requires the argument location specification of the pipeline object

2. If a function needs only one argument to be specified only the function name can be used

3. The argument slot specification #1 should be used (instead of . of R's magrittr.)

Clarification examples follow.

## Example 1

R-magrittr:

 iris %>% nrow %>% runif


WL:

ExampleData[{"Statistics", "FisherIris"}] // Length // RandomReal[1, #1] &


## Example 2

R-magrittr:

 iris %>% nrow %>% runif( n = 3, min = -10, max = .)


WL:

ExampleData[{"Statistics", "FisherIris"}] // Length //  RandomReal[{-10, #1}, 3] &

• What's the meaning of & of RandomReal[1, #1] &? Commented Nov 27, 2020 at 18:54
• @kittygirl RandomReal[1,12] generates 12 random numbers in the range [0,1]. It is equivalent to R's runif(12). Commented Nov 27, 2020 at 19:05
• What's the usage of &? Commented Nov 27, 2020 at 19:13
• @kittygirl RandomReal[1, #1]& is a pure function specification, similar to function(x) runif( n = x, min = 0, max = 1) in R. Commented Nov 27, 2020 at 19:52
• I wonder if there is a way to do this with Head? like x %>% f(*args) becomes f(x, *args) (in python-like syntax for arguments)
– qwr
Commented Dec 3, 2023 at 21:53

Note that in addition to the postfix notation //, you can also set up a pipeline of operators with RightComposition (/*) that can be re-used later. For example:

pipeline = Map[f] /* Apply[g] /* h
Range[5] // pipeline
Range[10] // pipeline

(* this also works, though it's maybe less intuitive *)
pipeline @ Range[5]


You can also use Composition (@*) if you prefer to read the other way around (i.e., the functions closest to the argument get applied first):

pipeline2 = h @* Apply[g] @* Map[f]
pipeline2 @ Range[5]

• Nice observation/post. I would like point out that WL function composition is not the same as the "pipelining" in R-tidyverse. Commented Nov 27, 2020 at 14:18