Memoization in Swift 2
The WWDC 2014 talk "Advanced Swift" (https://developer.apple.com/videos/wwdc/2014/#404 ) included a sample 'memoization function' which could speed up performance of certain functions by storing function results in a dictionary. Here's my implementation of it: func memoize<Input: Hashable, Output>(function: Input -> Output ) -> ( Input -> Output ) { var lookupTable = [ Input : Output ]() return {input in if lookupTable[input] == nil {lookupTable[input] = function(input)} return lookupTable[input]! } } This implementation is very similar to the first implementation of memoization presented in the Advanced Swift talk. However, that first implementation had an issue with creating recursive functions: let factorialMemoized = memoize {n in return n < 2 ? 1 : factorialMemoized(n- 1 )*n} //Error: Variable used within its own initia...