# |\>=, piping update operator

**URL:** <https://discourse.julialang.org/t/piping-update-operator/64511>\
**Category:** General Usage\
**Created:** [July 12, 2021, 2:26pm UTC](https://discourse.julialang.org/t/piping-update-operator/64511 "2021-07-12T14:26:00Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [July 12, 2021, 2:26pm UTC](https://discourse.julialang.org/t/piping-update-operator/64511/1 "2021-07-12T14:26:00Z")

</div>

Using SymPy.jl for a little bit I ended up wanting to do something like this

```julia
using SymPy
@syms x y
x = cos(y)^2 + sin(y)^2
x |>= simplify

```

i.e. using a piped [updating operator](https://docs.julialang.org/en/v1/manual/mathematical-operations/#Updating-operators). Could this be possible, and if not, why?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 12, 2021, 2:28pm UTC](https://discourse.julialang.org/t/piping-update-operator/64511/2 "2021-07-12T14:28:43Z")

</div>

Chain.jl is close

```julia
x = @chain begin 
    cos(y)^2 + sin(y)^2
    simplify 
end

```

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [July 12, 2021, 2:31pm UTC](https://discourse.julialang.org/t/piping-update-operator/64511/3 "2021-07-12T14:31:41Z")

</div>

Thanks, though I’m not really trying to find a workaround, writing `x=simplify(x)` is not so much effort after all. I was wondering what keeps this syntactic sugar from being there.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 12, 2021, 2:34pm UTC](https://discourse.julialang.org/t/piping-update-operator/64511/4 "2021-07-12T14:34:19Z")

</div>

> [@fgerick](#):
>
> Could this be possible, and if not, why?

It seems like it would be possible — it is currently a syntax error, so adding it would be non-breaking.

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [July 12, 2021, 2:39pm UTC](https://discourse.julialang.org/t/piping-update-operator/64511/5 "2021-07-12T14:39:27Z")

</div>

Visually, I am a bit concerned. The `|>` operator suggests that information is travelling toward the r.h.s. of the operator—bit in this constellation `|>=` would actually update the l.h.s.

It might by more readable if instead of the pipe operator it was using an “apply to” / “mutate” operator

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 12, 2021, 2:46pm UTC](https://discourse.julialang.org/t/piping-update-operator/64511/6 "2021-07-12T14:46:58Z")

</div>

True, it’s easy to write that. I understand you speculating about a syntax change, but it’s still worth emphasizing how useful a macro might be here.

```julia
julia> macro update(x,y)
           esc(update_helper(x, y))
       end;

julia> function update_helper(x, y)
           quote
           $x = $y($x)
           end
       end;

julia> foo(x) = x + 100;

julia> t = 100;

julia> @update t foo
200

julia> t
200

```

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [July 12, 2021, 2:59pm UTC](https://discourse.julialang.org/t/piping-update-operator/64511/7 "2021-07-12T14:59:31Z")

</div>

> [@josuagrw](#):
>
> Visually, I am a bit concerned. The `|>` operator suggests that information is travelling toward the r.h.s. of the operator—bit in this constellation `|>=` would actually update the l.h.s.

I agree that does seem unintuitive, more so than the other updating operators. It seemed like a natural thing to try. I don’t think another unicode symbol would fix this though.

> [@pdeffebach](#):
>
> it’s still worth emphasizing how useful a macro might be here.

Yes, I thought about that also, although I don’t think I would use that very often.

I mostly use `|>` only in Jupyter notebooks or the REPL during development. To be able to just add an equal sign and move one would be great for this. A macro, although nicer, is as cumbersome as writing `x=foo(x)`.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [July 12, 2021, 3:25pm UTC](https://discourse.julialang.org/t/piping-update-operator/64511/8 "2021-07-12T15:25:48Z")

</div>

Would a `simplify!` be a more conventional alternative here?

```julia
using SymPy
@syms x y
x = cos(y)^2 + sin(y)^2
x |> simplify!

```

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [July 12, 2021, 3:42pm UTC](https://discourse.julialang.org/t/piping-update-operator/64511/9 "2021-07-12T15:42:36Z")

</div>

How about something like this?

```julia
using SymPy
@syms x y
x = cos(y)^2 + sin(y)^2
x <!|> simplify

```

Just to illustrate my point about the arrow direction being a useful visual cue

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [July 12, 2021, 4:02pm UTC](https://discourse.julialang.org/t/piping-update-operator/64511/10 "2021-07-12T16:02:06Z")

</div>

Yes, that would indeed be nice for my example. But more generally this is not practical, as not many scalar functions are mutating.

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [July 12, 2021, 4:04pm UTC](https://discourse.julialang.org/t/piping-update-operator/64511/11 "2021-07-12T16:04:08Z")

</div>

This somehow reminds me too much of an inner product notation. And the `!` is also used to negate boolean operations, so it’s a bit distracting there.

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [July 12, 2021, 4:19pm UTC](https://discourse.julialang.org/t/piping-update-operator/64511/12 "2021-07-12T16:19:27Z")

</div>

I dont think the notation is confusing — the _operation_ is quite confusing here. x is both the argument and the target of the assignment. It’s not trivial to express that concisely because it combines two distinct operations

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [July 12, 2021, 4:24pm UTC](https://discourse.julialang.org/t/piping-update-operator/64511/13 "2021-07-12T16:24:32Z")

</div>

From a mathematical point these updating operators don’t really make sense, I agree. What you’re saying is that any updating operator is kind of weird?

If I accept the concept of updating operators and compare again

```julia
x = x * y
x *= y

x = x |> sin
x |>= sin

```

I don’t find it so illogical.

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [July 12, 2021, 4:48pm UTC](https://discourse.julialang.org/t/piping-update-operator/64511/14 "2021-07-12T16:48:48Z")

</div>

I think what makes it odd is that `|>(x, y)` is different from `+(x, y)` and `*(x, y)` because its second argument is a function. (Plus the implied directionality from the arrow which `+` and `*` dont have.)

The operator you are proposing `x |>= sin`, effectively applies `sin` to `x` in-place — but its pointing from x to sin.

Here is another idea that doesn’t have any arrow:

```julia
x °= sin

```

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [July 12, 2021, 5:14pm UTC](https://discourse.julialang.org/t/piping-update-operator/64511/15 "2021-07-12T17:14:40Z")

</div>

In `R` `magrittr` they have [assignment](https://magrittr.tidyverse.org/reference/compound.html) operator `%<>%`, so something like `<|>` is not completely unheard of. But generally people do not like to use it, since it can be rather confusing.

---

<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:** [July 12, 2021, 7:07pm UTC](https://discourse.julialang.org/t/piping-update-operator/64511/16 "2021-07-12T19:07:02Z")

</div>

What prevents you from just tacking `|> simplify` onto the first expression?

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [July 12, 2021, 7:23pm UTC](https://discourse.julialang.org/t/piping-update-operator/64511/17 "2021-07-12T19:23:35Z")

</div>

It’s not necessarily restricted to this simple example, where of course nothing would keep me from just reevaluating `x = cos(y)^2 + sin(y)^2 |> simplify`. I’m more interested in the case where you wouldn’t want to redo a calculation and overwrite `x` by the function call. Maybe I should’ve not make the example so explicit. It’s really meant more general.

Maybe the application of this is niche enough so as not be relevant to many people. It didn’t seem to have caught on in other languages…
