logo

How to Use Pipe Operators in Julia 📂Julia

How to Use Pipe Operators in Julia

Overview

Julia supports the pipeline operator, highlighting its strength in handling data.

Code

julia> (1:5) .|> (x -> sqrt(x+2)) .|> sin |> minimum
0.4757718381527513

julia> minimum(sin.((x -> sqrt(x+2)).(1:5)))
0.4757718381527513

The example code above puts the array $[1,2,3,4,5]$ into $\sqrt{x + 2}$, and then puts the result into $\sin$ to obtain the smallest value. The code above and below produces exactly the same results. It goes without saying how useful the pipeline can be when writing complex code. Just remember to always use a dot when inserting arrays to treat each element individually, similar to the use of the pipeline in other languages.

Other Languages

Environment

  • OS: Windows
  • julia: v1.5.0