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...
Garageband has some interesting features, made more interesting by the fact that some are quietly hidden away in the program. Here are some hidden and/or notable Garageband features: AUSampler & Effects Yep, Garageband 10 has a sampler that can play an entire keyboard range given a single audio file, but it's not nearly as easy to find as the iOS version's less powerful sampler. Here's how to get to it: 1. Create a new instrument and double click its icon to go to Smart Controls. 2. Hit the "(i)" button in the top right hand corner of the Smart Controls window. 3. Click "plug-ins" to expand it 4. Click instruments → AU Instruments → AUSampler → Stereo 5. Click the keyboard icon in the bottom right of the popup window. 6. Click "Sine 440 built in" 7. Click "choose file" (it's hard to see, but it's right under "Key mappings") 8. Choose file 9. Set the pitch of the file (called "root" in...
There's a talk (click here to go to it) from WWDC 2015 which discusses uses of Swift enumerations in its second half. When I first watched it, I was somewhat suspicious that it was overhyping enums as "magical unicorn solutions" to all problems. However, as I've worked on my game, I've found a great deal of use cases for enums. Here are some examples of how I've used enums and what I've learned about them: Raw values limited, computed vars unlimited I made function that checks if two rectangles intersect in any of the four cardinal directions by using subrectangles (see the diagram below). I use the function to allow the player to jump only when standing on top of the ground. (I actually use a spiffy PhysicsSensor class more often than this function, but more on that later). ... and therefore it is legal for the player to jump. Note: I actually run this check by enumerating over all of the obstacles in the scene, as the player could be standing...
Comments
Post a Comment