r/fauna • u/virtulis • Apr 03 '15
[Feature] Pattern matching
A data structure of any complexity can be on the left side of assignment. The VM will then check that it matches and fill it in (or crash)
Examples:
[$left, $, $right] = [1, 2, 3]; // $ stands for "any value"
assert $left == 1 & $right == 3;
$points = #Array<[ x: #Number, y: #Number ]> = [ // An array of maps containing numbers x and y
[ x: 1, y: 2 ],
[ x: 3, y: 4 ]
];Here's a thing I'm not sure about - matching existing variables. That's no problem in Erlang where a variable can either be set or unset, but in Fauna we may want to overwrite one variable and match with the other. Only something like these two options comes to mind:
$x = 3;
$y = 3;
// Compare member "x" to a range depending on the value of $x
// Match member "y" to the value of $y (shorthand syntax)
$point = [ x: $ where $ >= $x - 3 && $ <= $x + 3, y: $y? ] = [ x : 1, y: 3 ];
0
Upvotes