In functional programming it is frequently suggested to use pattern matching as it gives a more intuitive description of what the operation is doing.
For example, the addPoints
function that adds three dimension points is traditional written to pattern match the individual dimensions of the point like this:
addPoints : (Decimal, Decimal, Decimal) -> (Decimal, Decimal, Decimal) -> (Decimal, Decimal, Decimal)
addPoints (a,b,c) (x,y,z) = (a+x, b+y, c+z)
as opposed to something like
addPoints : (Decimal, Decimal, Decimal) -> (Decimal, Decimal, Decimal) -> (Decimal, Decimal, Decimal)
addPoints p1 p2 = (p1._1 + p2._1, p1._2 + p2._2, p1._3 + p2._3)
or even
addPoints : (Decimal, Decimal, Decimal) -> (Decimal, Decimal, Decimal) -> (Decimal, Decimal, Decimal)
addPoints p1 p2 = (fst3 p1 + fst3 p2, snd3 p1 + snd3 p2, thd3 p1 + thd3 p2)
- Do we still make that recommendation in Daml?
- For those on the fence about the clarity of pattern matching. Are there performance implications for one style over the other? Both
fst3
and the “_1” syntax are function calls so do they incur an extra penalty?