π°Pipes
Pipe Syntax
expr |> someFunctionCall(<args with at least one underscore>)
The pipe (|>
) takes the expression on the left and "pipes" it into the function right, wherever there are underscores.
For every underscore you use, a new instance of the left-hand expression will be made. For instance, myExpr |> someFunction(_, _)
turns into someFunction(myExpr, myExpr)
. This many result in unwanted side-effects.
Working Example
package main
import "fmt" as fmt
fun add(num: Int, addend: Int): Int {
return num + addend
}
fun subtract(num: Int, subtrahend: Int): Int {
return num - subtrahend
}
fun multiply(num: Int, multiplier: Int): Int {
return num * multiplier
}
fun divide(num: Int, divisor: Int): Int {
return num / divisor
}
fun Unit main() {
10
|> add(_, 10)
|> add(_, 30)
|> divide(_, 2)
|> subtract(_, 5)
|> divide(_, 2)
|> multiply(_, 10)
|> fmt.println(_)
}
Last updated