effekt
effekt copied to clipboard
Lambda Case
In functional programming languages it is possible to match on arguments directly without naming them. Consider:
data Vec3 = MkVec3 Double Double Double
add : Vec3 -> Vec3 -> Vec3
add (MkVec3 x1 x2 x3) (MkVec3 y1 y2 y3) = MkVec3 (x1 + y1) (x2 + y2) (x3 + y3)
The same code in Effekt is more verbose:
record Vec3(x1: Double, x2: Double, x3: Double)
def add(x: Vec3, y: Vec3): Vec3 = (x, y) match {
case (Vec3(x1, x2, x3), Vec3(y1, y2, y3)) => Vec3(x1 + y1, x2 + y2, x3 + y3)
}
I would like to be able to write the following instead:
def add(Vec3, Vec3): Vec3 = {
case (Vec3(x1, x2, x3), Vec3(y1, y2, y3)) => Vec3(x1 + y1, x2 + y2, x3 + y3)
}
This communicates that add immediately consumes its arguments exactly once. It avoids having to come up with a name for the parameters. This is also a downside, since names are useful for communicating intent.