# Opinions on piping into variable?

**URL:** <https://discourse.julialang.org/t/opinions-on-piping-into-variable/86643>\
**Category:** General Usage\
**Tags:** piping\
**Created:** [September 1, 2022, 2:17pm UTC](https://discourse.julialang.org/t/opinions-on-piping-into-variable/86643 "2022-09-01T14:17:51Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin.auer](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@martin.auer](https://discourse.julialang.org/u/martin.auer)\
**Post date:** [September 1, 2022, 2:17pm UTC](https://discourse.julialang.org/t/opinions-on-piping-into-variable/86643/1 "2022-09-01T14:17:52Z")

</div>

We use the Julia REPL quite a bit with custom data flows. When interactively toying around with them, we find the pipe operator extremely handy; especially combined with expressive Base.show() output, the sense of progression aids the learning, with minimal cursoring-and-bracketing-around.

I was wondering about the possibility of a new operator for assignment at the end of a pipe chain, and wanted to gather some feedback. (I did not find a Julia discussion, only an R one at [r - How do you end a pipe with an assignment operator? - Stack Overflow](https://stackoverflow.com/questions/31506391/how-do-you-end-a-pipe-with-an-assignment-operator))

So instead of:

```julia
result = load("foobar") |> transmogrify

```

one could write:

```julia
load("foobar") |> transmogrify |>= result

```

Leaner/shorter variants than `|>=` might even be better, but `|=` and `|` are already taken. (Maybe this could even be tackled on a higher, not-just-pipe level, via some “rightward” assignment operator that would not have to adhere to some pipe nomenclature.)

Any thaughts appreciated!

---

<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:** [September 1, 2022, 2:22pm UTC](https://discourse.julialang.org/t/opinions-on-piping-into-variable/86643/2 "2022-09-01T14:22:51Z")

</div>

I agree this would be nice (I really with R had this too). fwiw, Chain.jl has something kind of similar in the latest version (which doesn’t wrap in a `let` block)

```julia
julia> using Chain

julia> x = rand(10);

julia> @chain x begin
           first
           y = _
       end
0.5028500583195015

julia> y
0.5028500583195015

```

---

<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:** [September 1, 2022, 6:17pm UTC](https://discourse.julialang.org/t/opinions-on-piping-into-variable/86643/3 "2022-09-01T18:17:38Z")

</div>

I’ve never understood RHS assignment. I use Chain all the time (and simple pipes for simple tasks), but I still always use LHS assignment.

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [September 1, 2022, 6:30pm UTC](https://discourse.julialang.org/t/opinions-on-piping-into-variable/86643/4 "2022-09-01T18:30:34Z")

</div>

You could easily make a macro that takes an expression like `@rhassign x |> f |> g = y` and flips the expression so that it runs like `y = x |> f |> g`.

---

<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:** [September 1, 2022, 6:42pm UTC](https://discourse.julialang.org/t/opinions-on-piping-into-variable/86643/5 "2022-09-01T18:42:04Z")

</div>

This doesn’t solve the problem, though, since if you knew you wanted to assign a variable, you would have written it at the top.

Same response to @tbeason, sometimes you write an expression and then realize only later you want to assign it to a variable. Its a nice feature wither REPL

I also just figured out how to do it in R

```julia
x %>% f() -> y

```

---

<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:** [September 1, 2022, 7:02pm UTC](https://discourse.julialang.org/t/opinions-on-piping-into-variable/86643/6 "2022-09-01T19:02:51Z")

</div>

I don’t think that really makes much of a difference, you can return to the beginning of a long expression with 1 or 2 keystrokes (home key) and add the assignment if you want. It is more about a conceptual difference.

---

<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:** [September 1, 2022, 8:07pm UTC](https://discourse.julialang.org/t/opinions-on-piping-into-variable/86643/7 "2022-09-01T20:07:26Z")

</div>

TIL about the home key. Still, I think it would be useful.

---

<div class="post-metadata">

**Author:** ![martin.auer](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@martin.auer](https://discourse.julialang.org/u/martin.auer)\
**Post date:** [September 2, 2022, 8:08am UTC](https://discourse.julialang.org/t/opinions-on-piping-into-variable/86643/8 "2022-09-02T08:08:44Z")

</div>

Hi,  
first, thanks for the feedback!

I concur with @pdeffebach that macros are not ideal here – when I started piping around, I of course forgot to start the line with the initial macro; I’d need the same procedure (up → home → write something).

(Of course the number of keystrokes is absurdly modest. But maybe it’s also psychological – at least my head does not _want_ to go back to the beginning of the nice pipeline; it is now snugly entrenched at the end of it…)

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [September 2, 2022, 10:35am UTC](https://discourse.julialang.org/t/opinions-on-piping-into-variable/86643/9 "2022-09-02T10:35:48Z")

</div>

I’m not sure about this. I only pipe across many lines, and left handed assignment is a lot clearer in that case. There’s no way I would confuse `=` and `|>`, but I might confuse `|>=` and `|>` at a glance and think `result` is the last called function. I get the use in the REPL, but we already can 1) click the home key to move to the start of the line, or 2) retrieve the value from global `ans` immediately after execution:

```julia
julia> 1:9 |> sum
45
julia> ans
45
julia> result = ans
45
julia> 1+1
2
julia> ans
2
julia> result
45
julia> ans
45

```

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [September 4, 2022, 12:53pm UTC](https://discourse.julialang.org/t/opinions-on-piping-into-variable/86643/10 "2022-09-04T12:53:01Z")

</div>

> [@pdeffebach](#):
>
> I agree this would be nice (I really with R had this too)

Actually it does, the `<-` assignement in `R` is also available as `->`, which is IMHO clever.

---

<div class="post-metadata">

**Author:** ![martin.auer](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@martin.auer](https://discourse.julialang.org/u/martin.auer)\
**Post date:** [April 19, 2023, 2:37pm UTC](https://discourse.julialang.org/t/opinions-on-piping-into-variable/86643/11 "2023-04-19T14:37:15Z")

</div>

Hi,

I wanted to add a possible idea to this thread.

The following, at first sight, seems to achieve the desired “assignment-at-end-of-pipe.” I am very unsure if this does not break something else; any feedback really very welcome!

(The primary goal would be for this to work in the REPL; it would not necessarily need to have to work otherwise…)

The idea is to overwrite the function call operator for `Symbol`:

```julia
julia> x
ERROR: UndefVarError: x not defined

julia> function (newvarname::Symbol)(varvalue) eval(:($newvarname = $varvalue)) end

julia> 1 |> sin |> cos |> sin |> cos |> sin |> cos |> :x
0.7467069000243155

julia> x
0.7467069000243155

```

(I would activate it in my REPL modes for interactive usage only…)

Could there be some dangerous side-effect here in your opinion? Maybe I need to do better escaping/quoting…?

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [April 19, 2023, 7:39pm UTC](https://discourse.julialang.org/t/opinions-on-piping-into-variable/86643/12 "2023-04-19T19:39:52Z")

</div>

Interesting, though the use of `eval` feels like a code smell to me.  
Alternatively, you could have a macro creating a function suitable for piping, i.e.,

```julia
macro rassign(symb)
    :(x -> global $(esc(symb)) = x)
end

```

```julia
julia> 1 |> sin |> cos |> sin |> cos |> sin |> cos |> @rassign x
0.7467069000243155

julia> x
0.7467069000243155

```
