Function Composition and Piping

The following are equivalent based on the docs. 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

5 Likes

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.

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)).

7 Likes