memoize written in swift using generics