2
$\begingroup$
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?

$\endgroup$
3
  • 2
    $\begingroup$ 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[#] & $\endgroup$
    – Carl Lange
    Commented Nov 25, 2020 at 13:20
  • $\begingroup$ "Pipeline operator" discussions at MSE can be found by the search monadic programming. $\endgroup$ Commented Nov 25, 2020 at 13:23
  • 2
    $\begingroup$ Maybe RightComposition ? Which is /* $\endgroup$ Commented Nov 25, 2020 at 15:49

3 Answers 3

8
$\begingroup$

Perhaps you're looking for

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

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] &
$\endgroup$
6
  • $\begingroup$ What's the meaning of & of RandomReal[1, #1] &? $\endgroup$
    – kittygirl
    Commented Nov 27, 2020 at 18:54
  • $\begingroup$ @kittygirl RandomReal[1,12] generates 12 random numbers in the range [0,1]. It is equivalent to R's runif(12). $\endgroup$ Commented Nov 27, 2020 at 19:05
  • $\begingroup$ What's the usage of &? $\endgroup$
    – kittygirl
    Commented Nov 27, 2020 at 19:13
  • 1
    $\begingroup$ @kittygirl RandomReal[1, #1]& is a pure function specification, similar to function(x) runif( n = x, min = 0, max = 1) in R. $\endgroup$ Commented Nov 27, 2020 at 19:52
  • $\begingroup$ 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) $\endgroup$
    – qwr
    Commented Dec 3, 2023 at 21:53
8
$\begingroup$

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]
$\endgroup$
1
  • $\begingroup$ Nice observation/post. I would like point out that WL function composition is not the same as the "pipelining" in R-tidyverse. $\endgroup$ Commented Nov 27, 2020 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.