# Chaining of functions

**URL:** https://discourse.julialang.org/t/chaining-of-functions/3276
**Category:** General Usage
**Created:** [April 19, 2017, 7:17am UTC](https://discourse.julialang.org/t/chaining-of-functions/3276 "2017-04-19T07:17:20Z")
**Posts on this page:** 5
**Page:** 1

<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: [April 19, 2017, 7:17am UTC](https://discourse.julialang.org/t/chaining-of-functions/3276/1 "2017-04-19T07:17:20Z")

</div>

It would be great if the chaining operator supported something like:

```julia
my_path |> joinpath(_, "my_file.jld")

```

so I can avoid

```julia
my_path |> x -> joinpath(x, "my_file.jld")

```

i think R supports this in the `%>%` operator using `.`

---

<div class="post-metadata">

### Author: ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)
#### Post date: [April 19, 2017, 8:21am UTC](https://discourse.julialang.org/t/chaining-of-functions/3276/2 "2017-04-19T08:21:57Z")

</div>

I thought about exactly the same thing. The above example is actually pretty simple and may be rewritten in a pretty readable:

```julia
joinpath(my_path, "my_file.jld")

```

But if there are more functions, code becomes pretty messy, e.g.:

```julia
foo(bar(baz(x)), y)

```

Which could have been:

```julia
baz(x) |> bar(_, y) |> foo

```

Which looks more more clear for me. At least, it doesn’t require parenthesis highlighting to parse the code by eye 🙂

To give some more background, many other languages include similar means, e.g. Haskell has a [dot operator](http://stackoverflow.com/questions/631284/dot-operator-in-haskell-need-more-explanation) and Clojure has `->` macro together with short syntax for lambdas (e.g. `#(bar % y)`). So having something like this in Julia might be beneficial for code readability.

However, if we don’t have it in the language itself, we still have _macros_ that can do literally anything with code in question. Some time ago I experimented with a macro (although I don’t remember where the code for it is) that allowed to chain functions like this:

```julia
@c foo bar(_, y) baz

```

The main disadvantage of this macro for me was that users reading my code may be unfamiliar with the syntax and thus may be better with more traditional syntax.

---

<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: [April 19, 2017, 8:27am UTC](https://discourse.julialang.org/t/chaining-of-functions/3276/3 "2017-04-19T08:27:14Z")

</div>

thanks for elaborating.

---

<div class="post-metadata">

### Author: ![phlavenk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phlavenk/32/1873_2.png) [@phlavenk](https://discourse.julialang.org/u/phlavenk)
#### Post date: [April 19, 2017, 1:09pm UTC](https://discourse.julialang.org/t/chaining-of-functions/3276/4 "2017-04-19T13:09:59Z")

</div>

Have a look at package [Lazy.jl](https://github.com/MikeInnes/Lazy.jl). I think i does exactly what you’re looking for.

```
# @as lets you name the threaded argmument
@as _ x f(_, y) g(z, _) == g(z, f(x, y))

```

---

<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: [April 19, 2017, 1:47pm UTC](https://discourse.julialang.org/t/chaining-of-functions/3276/5 "2017-04-19T13:47:35Z")

</div>

thanks, seems to be a really interesting package.

btw: there is a `@_` macro which is equivalent to `@as _`

`@_ x f(_, y) g(z, _) == g(z, f(x, y))`
