Destructuring

We can destructure literals when pattern matching. This allows us to extract values from a list or tuple and bind them to variables. We can use the _ wildcard pattern to ignore values we don't care about and when destructuring a list, we can use the ... pattern to mark a variable as an accumulator that collects the remaining values.

l = [1, 2, 3] [a, ...b] = l [...c, d] = l [e, _, f] = l ((a, b), (c, d), (e, f))

We can also deconstruct tuples by pattern matching. In this example, we extract the age field from the xenia tuple.

xenia = ("Xenia", 42) (_, age) = xenia age

© 2024 ouzu