# Is it possible to pipe to a function of several variables?

**URL:** <https://discourse.julialang.org/t/is-it-possible-to-pipe-to-a-function-of-several-variables/76393>\
**Category:** General Usage\
**Tags:** question, piping\
**Created:** [February 14, 2022, 7:32am UTC](https://discourse.julialang.org/t/is-it-possible-to-pipe-to-a-function-of-several-variables/76393 "2022-02-14T07:32:41Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Soldalma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/soldalma/32/29388_2.png) [@Soldalma](https://discourse.julialang.org/u/Soldalma)\
**Post date:** [February 14, 2022, 7:32am UTC](https://discourse.julialang.org/t/is-it-possible-to-pipe-to-a-function-of-several-variables/76393/1 "2022-02-14T07:32:41Z")

</div>

In some other languages, like R, some package allows one to insert a call to a function of several variables into a sequence of pipes. This is done by specifying all arguments but one and inserting a `.` in the place of the unspecified argument. For example:

```julia
foo(x, y) = x + y
bar(x, y) = x * y
 x = 1
y = 2
x |> foo(., y) |> bar(4, .)
# 12

```

Can this be done in Julia or with the help of some Julia package?

---

<div class="post-metadata">

**Author:** ![lucifer1004](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucifer1004/32/22311_2.png) [@lucifer1004](https://discourse.julialang.org/u/lucifer1004)\
**Post date:** [February 14, 2022, 7:38am UTC](https://discourse.julialang.org/t/is-it-possible-to-pipe-to-a-function-of-several-variables/76393/2 "2022-02-14T07:38:34Z")

</div>

You can use [Pipe.jl](https://github.com/oxinabox/Pipe.jl).

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [February 14, 2022, 8:42am UTC](https://discourse.julialang.org/t/is-it-possible-to-pipe-to-a-function-of-several-variables/76393/3 "2022-02-14T08:42:44Z")

</div>

I would like julia to have this feature, officially.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [February 14, 2022, 8:53am UTC](https://discourse.julialang.org/t/is-it-possible-to-pipe-to-a-function-of-several-variables/76393/4 "2022-02-14T08:53:24Z")

</div>

```julia
using Chain

@chain x begin
  foo(y)
  bar(4, _)
end

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [February 14, 2022, 11:16am UTC](https://discourse.julialang.org/t/is-it-possible-to-pipe-to-a-function-of-several-variables/76393/5 "2022-02-14T11:16:22Z")

</div>

> [@photor](#):
>
> I would like julia to have this feature, officially.

Don’t hold your breath but [RFC: curry underscore arguments to create anonymous functions by stevengj · Pull Request #24990 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/24990) is the PR to follow.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 14, 2022, 11:39am UTC](https://discourse.julialang.org/t/is-it-possible-to-pipe-to-a-function-of-several-variables/76393/6 "2022-02-14T11:39:46Z")

</div>

Adding another example to the pipeline:

```julia
using Underscores
@_ x |> foo(__, y) |> bar(4,__)
12

```

---

<div class="post-metadata">

**Author:** ![Soldalma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/soldalma/32/29388_2.png) [@Soldalma](https://discourse.julialang.org/u/Soldalma)\
**Post date:** [February 14, 2022, 12:46pm UTC](https://discourse.julialang.org/t/is-it-possible-to-pipe-to-a-function-of-several-variables/76393/7 "2022-02-14T12:46:26Z")

</div>

> [@rafael.guerra](#):
>
> ```julia
> using Underscores
> @_ x |> foo(__1, y) |> bar(4,__1)
> 12
> 
> ```

Three packages that do what I asked for! As the French say, this is an _embarras de richesse_. Can you guys duke it out on which one is the best? Or, more precisely, the advantages and disadvantages of each package.

Which one is the fastest?  
Which packages can deal with replacing more than one variable simultaneously?  
Anything else?

Thanks a million.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 14, 2022, 12:51pm UTC](https://discourse.julialang.org/t/is-it-possible-to-pipe-to-a-function-of-several-variables/76393/8 "2022-02-14T12:51:43Z")

</div>

> [@Soldalma](#):
>
> Anything else?

You can read more from the plumbing experts [on this thread](https://discourse.julialang.org/t/ann-datapipes-jl/60734).

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [February 14, 2022, 1:07pm UTC](https://discourse.julialang.org/t/is-it-possible-to-pipe-to-a-function-of-several-variables/76393/9 "2022-02-14T13:07:51Z")

</div>

To my understanding, one of the major differences between piping packages lies in where exactly they put the result of the previous pipe step.

`DataPipes.jl` ([[ANN] DataPipes.jl](https://discourse.julialang.org/t/ann-datapipes-jl/60734)): the previous result becomes the _last_ function argument by default. This is ideal for common data processing workflows: think functions like `map` and `filter` - that’s where `Data` in the package name comes from.  
Of course, one can manually use the previous result anywhere with `__`:

```julia
@p [(a=1, b=2), (a=3, b=4)] |>
	map(_.a + _.b + 2) |>
	first(__, 1)

```

_Disclaimer: I’m the author of `DataPipes.jl`_

`Chain.jl`: the previous result becomes the _first_ function argument by default. This is convenient for certain other functions and ecosystems, but requires explicitly writing the placeholder in each step when using common data processing functions.

`Underscores.jl`: always requires specifying where the previous result goes. This adds some boilerplate for common cases, but makes other more general pipelines cleaner. See discussion later in the [[ANN] DataPipes.jl](https://discourse.julialang.org/t/ann-datapipes-jl/60734) thread.

There are other features and differences as well, most notably with anonymous functions, but I think they are less fundamental. See the `DataPipes.jl` usage notebook for more advanced examples: [https://aplavin.github.io/DataPipes.jl/examples/notebook.html](https://aplavin.github.io/DataPipes.jl/examples/notebook.html).

---

<div class="post-metadata">

**Author:** ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)\
**Post date:** [February 14, 2022, 1:50pm UTC](https://discourse.julialang.org/t/is-it-possible-to-pipe-to-a-function-of-several-variables/76393/10 "2022-02-14T13:50:05Z")

</div>

You can also just create an anonymous function.

```julia
x |> x -> foo(x, y) |> x -> bar(4, x)

```

---

<div class="post-metadata">

**Author:** ![Soldalma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/soldalma/32/29388_2.png) [@Soldalma](https://discourse.julialang.org/u/Soldalma)\
**Post date:** [February 14, 2022, 1:52pm UTC](https://discourse.julialang.org/t/is-it-possible-to-pipe-to-a-function-of-several-variables/76393/11 "2022-02-14T13:52:23Z")

</div>

Yes, and one can also use partial functions using package PartialFunctions. But the point of packages like Pipes, DataPipes, Chain and Underscores is eliminating these complications.
