Code reuse with anonymous functions and generics