# How often do you use the |\> operator?

**URL:** <https://discourse.julialang.org/t/how-often-do-you-use-the-operator/56189>\
**Category:** General Usage\
**Created:** [February 28, 2021, 1:18pm UTC](https://discourse.julialang.org/t/how-often-do-you-use-the-operator/56189 "2021-02-28T13:18:10Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon74562486](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@anon74562486](https://discourse.julialang.org/u/anon74562486)\
**Post date:** [February 28, 2021, 1:18pm UTC](https://discourse.julialang.org/t/how-often-do-you-use-the-operator/56189/1 "2021-02-28T13:18:10Z")

</div>

I really like pipe operator (|\>) but if I see the source code of libraries I see that it is often used like this:

```julia
a = function_2(function_1(input_data)) |> gpu

```

rather than this:

```julia
a = input_data |>
        function_1 |>
        function_2 |>
        gpu

```

So I wonder how often julia programmers use the |\> operator?  
And why is it always used the first way instead of the second (which seems more readable)?  
Does the second example constitute a bad practice?  
Thank you, sorry for my english

---

<div class="post-metadata">

**Author:** ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)\
**Post date:** [February 28, 2021, 1:31pm UTC](https://discourse.julialang.org/t/how-often-do-you-use-the-operator/56189/2 "2021-02-28T13:31:21Z")

</div>

In REPL, it’s very handy to interactively use one-argument functions with `|>` operator since it’s easier to type `... |> func` instead of `func(...)`.

In source code, I’m not a big fun of `|>`, but in the case you posted above, I guess the benefit is that one can quickly do some tests on CPU by commenting at `|>` instead of deleting brackets.

---

<div class="post-metadata">

**Author:** ![laborg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laborg/32/5474_2.png) [@laborg](https://discourse.julialang.org/u/laborg)\
**Post date:** [February 28, 2021, 1:39pm UTC](https://discourse.julialang.org/t/how-often-do-you-use-the-operator/56189/3 "2021-02-28T13:39:25Z")

</div>

I used it quite a lot in R for dplyr, but never in Julia.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [February 28, 2021, 1:45pm UTC](https://discourse.julialang.org/t/how-often-do-you-use-the-operator/56189/4 "2021-02-28T13:45:10Z")

</div>

I sometime use it for `do` statements:  
this

```julia
a = fun(x) do i
    ...
end |> wrapper

```

instead of

```julia
a = wrapper(fun(x) do i
    ...
end)

```

where `wrapper` is more often than not `Ref`…

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [February 28, 2021, 2:07pm UTC](https://discourse.julialang.org/t/how-often-do-you-use-the-operator/56189/5 "2021-02-28T14:07:16Z")

</div>

I also use it mostly interactively, almost always specifically as `|> clipboard` or `|> plot`.  
I occasionally use it with broadcasting, `values .|> callables`, since `callables.(values)` doesn’t broadcast over `callables`. But it’s not a very common thing to do.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 28, 2021, 4:05pm UTC](https://discourse.julialang.org/t/how-often-do-you-use-the-operator/56189/6 "2021-02-28T16:05:08Z")

</div>

Never. I am fine with nested functions, or in source code

```julia
result1 = function1(input_data) # of course with functions and ...
result2 = function2(result2) # ... variables given meaningful names
a = gpu(result2)

```

Better locations for stack traces, more understandable code.

---

<div class="post-metadata">

**Author:** ![D\_A](https://avatars.discourse-cdn.com/v4/letter/d/8dc957/32.png) [@D\_A](https://discourse.julialang.org/u/D_A)\
**Post date:** [February 28, 2021, 4:26pm UTC](https://discourse.julialang.org/t/how-often-do-you-use-the-operator/56189/7 "2021-02-28T16:26:38Z")

</div>

I use it almost always (in REPL but mainly in source code), and nested functions are extremely rare. To allow that, I use heavily the Pipe and Chain modules.

I use classical syntax if there are at least 2 arguments with only one function:

```julia
JSON.print(file, data, 4)
result = expression |> JSON.parse

```

and I use nested functions sometimes to have some symmetry:

```julia
for (position, item) ∈ enumerate(items)

```

but I already surprised myself with:

```julia
for (position, item) ∈ items |> enumerate

```

For example, I really prefer this syntax over the classical one:

```julia
@pipe data |> filter(!isnothing, _) |> join(_, " ")

```

So, from the day I discovered this operator, its usage has grown continuously to become a typical trait of my own coding style… I must say that I don’t like parentheses and braces a lot, especially in streaks, even worse if there are trailing arguments!

It is also related to the way I read my code, and usually it comes from the start to the end (it is reversed with nested functions) so having the pipe operator allows me to follow the same way what happens with successive elements…

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [February 28, 2021, 4:39pm UTC](https://discourse.julialang.org/t/how-often-do-you-use-the-operator/56189/8 "2021-02-28T16:39:43Z")

</div>

I use it a lot for chaining data transformations. Outside of that I spontaneously use it at the REPL.

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [February 28, 2021, 4:43pm UTC](https://discourse.julialang.org/t/how-often-do-you-use-the-operator/56189/9 "2021-02-28T16:43:46Z")

</div>

I use it in the REPL as @yha. When writing code, I prefer to be more explicit as mentioned by @Tamas_Papp (easier to debug), or when suitable, use Chain.jl:

> **[GitHub - jkrumbiegel/Chain.jl: A Julia package for piping a value through a...](https://github.com/jkrumbiegel/Chain.jl)**
>
> A Julia package for piping a value through a series of transformation expressions using a more convenient syntax than Julia's native piping functionality. - GitHub - jkrumbiegel/Chain.jl: A Jul...

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [February 28, 2021, 5:28pm UTC](https://discourse.julialang.org/t/how-often-do-you-use-the-operator/56189/10 "2021-02-28T17:28:17Z")

</div>

For chains of function calls, I often write two versions of the code: one with `|>` and one without. Then I keep the one that looks most readable. At a guess I’d say I keep `|>` 30% to 50% of the time.

I’ll probably use it more often if/when Julia finally supports the `_` syntax for partial function application, since that would increase the number of cases where `|>` makes the code more readable. (There are packages like Pipe.jl and Chain.jl to help with that but the readability improvement doesn’t always outweigh the cost of an extra dependency.)

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [February 28, 2021, 5:33pm UTC](https://discourse.julialang.org/t/how-often-do-you-use-the-operator/56189/11 "2021-02-28T17:33:41Z")

</div>

What I found using Chain.jl, was that it’s nice to have an almost normal-looking block of code, just that I’m spared from coming up with intermediate variable names. So I think that communicates quite well to a reader that you’re looking at a sequence of transformations where only the end result matters. I still don’t use it in packages, because the gain seems too low for another dependency, and also you’d have to decide a consistent style. You can’t switch all the time between normal function application and chaining or it will confuse people.

---

<div class="post-metadata">

**Author:** ![Satvik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/satvik/32/20486_2.png) [@Satvik](https://discourse.julialang.org/u/Satvik)\
**Post date:** [February 28, 2021, 6:03pm UTC](https://discourse.julialang.org/t/how-often-do-you-use-the-operator/56189/12 "2021-02-28T18:03:11Z")

</div>

Personally I use the pipe operator almost all the time, especially in conjunction with `Underscores.jl` or `Chain.jl`.

When developing, I’l have a jupyter notebook cell with something like `x |> f`, tweak it until `f` does what I expect, then look at `x |> f |> g`, etc. This workflow also makes it easy to delete or comment out certain lines. I also really like reading left to right or top to bottom, as opposed to right to left with nested functions.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [February 28, 2021, 9:41pm UTC](https://discourse.julialang.org/t/how-often-do-you-use-the-operator/56189/13 "2021-02-28T21:41:00Z")

</div>

I believed, and the answers you received convinced me more, that the reason the `|>` operator is not widely used in Julia is mostly related to the fact that the base operator is pretty limited, and to have convenient implementations one has to use packages as Pipe.jl or Chain.jl, and at that time people don’t bother…

---

<div class="post-metadata">

**Author:** ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)\
**Post date:** [February 28, 2021, 9:51pm UTC](https://discourse.julialang.org/t/how-often-do-you-use-the-operator/56189/14 "2021-02-28T21:51:33Z")

</div>

Do these chaining operations hurt performance?

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [February 28, 2021, 9:52pm UTC](https://discourse.julialang.org/t/how-often-do-you-use-the-operator/56189/15 "2021-02-28T21:52:13Z")

</div>

No. Not unless the compiler **completely** gives up, in which case, you have bigger problems.

---

<div class="post-metadata">

**Author:** ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)\
**Post date:** [February 28, 2021, 9:53pm UTC](https://discourse.julialang.org/t/how-often-do-you-use-the-operator/56189/16 "2021-02-28T21:53:58Z")

</div>

Do you have a reference supporting this claim? I remember chaining operators in R and Python slow down codes.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [February 28, 2021, 9:56pm UTC](https://discourse.julialang.org/t/how-often-do-you-use-the-operator/56189/17 "2021-02-28T21:56:32Z")

</div>

A lot of things hurt performance in R and Python because they don’t have optimizing compilers, or when you use a compiler, the language is too dynamic in the wrong ways to allow much optimization

```julia
julia> f(x) = x |> identity |> identity |> identity |> y -> y + 1
f (generic function with 1 method)

julia> code_llvm(f, (Int,); debuginfo=:none)
define i64 @julia_f_355(i64 signext %0) {
top:
  %1 = add i64 %0, 1
  ret i64 %1
}

```

```julia
julia> let x = Ref(1)
           @btime $x[] + 1
           @btime f($x[])
       end
           
  1.299 ns (0 allocations: 0 bytes)
  1.299 ns (0 allocations: 0 bytes)

```

Things like zero-runtime-cost abstractions are a core feature of julia. I guess I sometimes forget this even needs to be mentioned.

---

<div class="post-metadata">

**Author:** ![Satvik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/satvik/32/20486_2.png) [@Satvik](https://discourse.julialang.org/u/Satvik)\
**Post date:** [February 28, 2021, 11:42pm UTC](https://discourse.julialang.org/t/how-often-do-you-use-the-operator/56189/18 "2021-02-28T23:42:57Z")

</div>

One thing I really like about Scala is that it has underscore syntax for anonymous functions, which makes piping or chaining much easier. Of course, in Julia you can do this with any one of several packages, but you still have to call the macro each time, which adds some friction.

Compare these two out of the box examples:

```julia
val z = (1 to 3)
  .map(_ + 1)
  .map(math.pow(_, 2))

println(z)

```

```julia
z = 1:3 .|> 
  (x -> x + 1) .|>
  (x -> x ^ 2)

```

Julia’s syntax wins on many fronts, but the slightly clunky anonymous functions really hurt function chaining/pipelining, especially when you have functions of multiple arguments.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [March 1, 2021, 4:17am UTC](https://discourse.julialang.org/t/how-often-do-you-use-the-operator/56189/19 "2021-03-01T04:17:54Z")

</div>

It’s coincidental, but I use it quite a bit in Unitful: `(u::Units)(q::Quantity)` is defined such that `25u"kg" |> u"me"` converts 25 kilogram into electron masses. But that’s more Unitful hijacking the notation.

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [March 1, 2021, 11:32am UTC](https://discourse.julialang.org/t/how-often-do-you-use-the-operator/56189/20 "2021-03-01T11:32:14Z")

</div>

I use it very rarely.  
Basically only in the REPL because I have issues with my home/end keys.

I used to use it a fair bit, then I spent time around @ararslan.  
Now I am convicted by their argument that it makes for harder reading in a lot of places as the function that controls the type of the return values is furthers from the assignment.

Though I do appreciate that it does work well for a pipeline like approach common to some workflows.  
The distant function thing + just cognitive overhead of multiple ways to write the same thing means it isn’t worth it for me.  
But it might be for others.

[Next page](https://discourse.julialang.org/t/how-often-do-you-use-the-operator/56189.md?page=2)
