# Reverse Chaining macro: @macro f x to be equivalent to f(x)

**URL:** <https://discourse.julialang.org/t/reverse-chaining-macro-macro-f-x-to-be-equivalent-to-f-x/11115>\
**Category:** New to Julia\
**Tags:** macros\
**Created:** [May 24, 2018, 12:29am UTC](https://discourse.julialang.org/t/reverse-chaining-macro-macro-f-x-to-be-equivalent-to-f-x/11115 "2018-05-24T00:29:44Z")\
**Posts on this page:** 7\
**Page:** 1

<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:** [May 24, 2018, 12:29am UTC](https://discourse.julialang.org/t/reverse-chaining-macro-macro-f-x-to-be-equivalent-to-f-x/11115/1 "2018-05-24T00:29:44Z")

</div>

Is there a way that is the backwards version of Lazy’s `@>`?

Is there a macro called, say, `@x` such that `@x f x` evaluates to `f(x)`.

My use case is that I have a long plot code and I want to display it when it is done. I could do

```julia
display (
plot(...
lots
of
code)
)

```

But I would rather avoid parentheses. So I defined a macro

```julia
macro display(x)
esc(:display($x))
end

@display plot(...)

```

But I feel like there should be a more general way of doing this.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [May 24, 2018, 2:30am UTC](https://discourse.julialang.org/t/reverse-chaining-macro-macro-f-x-to-be-equivalent-to-f-x/11115/2 "2018-05-24T02:30:58Z")

</div>

Something like this?

```julia
macro run(f, x)
         esc(:($f($x)))
end

```

Or for any number of args:

```julia
macro run(f, x...)
         esc(:($f($x...)))
end

```

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [May 24, 2018, 2:36am UTC](https://discourse.julialang.org/t/reverse-chaining-macro-macro-f-x-to-be-equivalent-to-f-x/11115/3 "2018-05-24T02:36:51Z")

</div>

You could also use a pipe `x |> f` but its depreciated.

---

<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:** [May 24, 2018, 2:55am UTC](https://discourse.julialang.org/t/reverse-chaining-macro-macro-f-x-to-be-equivalent-to-f-x/11115/4 "2018-05-24T02:55:32Z")

</div>

A macro with one set of arguments is pretty easy to make.

An extensible function that allows you to do, say,

```julia
f(x) = x + 1
g(x) = x + 1

@run f g 1

```

Seems harder, since I think you have to know that `g` is callable and construct a new syntax tree for those functions.

---

<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:** [May 24, 2018, 4:45am UTC](https://discourse.julialang.org/t/reverse-chaining-macro-macro-f-x-to-be-equivalent-to-f-x/11115/5 "2018-05-24T04:45:27Z")

</div>

Here’s a solution, assuming everything is a single-variable function until the last term

```julia
macro run(x...)
    ex = Expr(:call, x[1])
    push!(ex.args, Expr(:call, x[2],1))
    t = ex
    for i in 3:(length(x)-1)
        t = t.args[2]
        t.args[2] = Expr(:call, x[i],x[end])
        println(ex)
    end
    ex
end

function xx(x) x + 1 end
function yy(x) x + 1 end
function zz(x) x + 1 end
function gg(x) x + 1 end

print(@run xx yy zz gg 1)
> 5

```

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [May 24, 2018, 7:37am UTC](https://discourse.julialang.org/t/reverse-chaining-macro-macro-f-x-to-be-equivalent-to-f-x/11115/6 "2018-05-24T07:37:32Z")

</div>

is `|>` deprecated? I hope not because I use it quite a lot.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [May 24, 2018, 8:43am UTC](https://discourse.julialang.org/t/reverse-chaining-macro-macro-f-x-to-be-equivalent-to-f-x/11115/7 "2018-05-24T08:43:18Z")

</div>

Yeah I’m wrong. Turns out I only read half the depreciation thread.

So just use a pipe! Macros kill revise, and make everything weird.

```julia
plot(
  blah,
  blah
) |> display

```

and youre done
