Higher-order function for encapsulating method calls