# Function Composition and Piping

**URL:** <https://discourse.julialang.org/t/function-composition-and-piping/46472>\
**Category:** New to Julia\
**Tags:** piping, composition\
**Created:** [September 11, 2020, 5:54pm UTC](https://discourse.julialang.org/t/function-composition-and-piping/46472 "2020-09-11T17:54:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [September 11, 2020, 5:54pm UTC](https://discourse.julialang.org/t/function-composition-and-piping/46472/1 "2020-09-11T17:54:56Z")

</div>

The following are equivalent based on the [docs](https://docs.julialang.org/en/v1/manual/functions/#Function-composition-and-piping). Is there a Julian preference or functional reason to choose one over another, or is it just a matter of personal taste?

Nesting: `g(f(x))`  
Composition: `(g ∘ f)(x)`  
Piping: `x |> f |> g`

---

<div class="post-metadata">

**Author:** ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)\
**Post date:** [September 11, 2020, 6:35pm UTC](https://discourse.julialang.org/t/function-composition-and-piping/46472/2 "2020-09-11T18:35:03Z")

</div>

The style guide doesn’t mention anything about that I think. If there is a preferred way it should. Personally i find the first fine for two functions and the third is clearer for more than two. The second I usually only use when a function is passed to another function eg `map`.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 11, 2020, 6:48pm UTC](https://discourse.julialang.org/t/function-composition-and-piping/46472/3 "2020-09-11T18:48:39Z")

</div>

Nesting is what is almost always used in “normal” code. The other ones have some specific cases where people tend to use them. Piping is with data pipelines (e.g. look at the Query.jl docs). Composition is sometimes used when passing the result as a higher order function instead of writing `x -> g(f(x))`.
