Use pattern matching by extracting a function