Optimised pattern matching in functions