Skip to content

Instantly share code, notes, and snippets.

@JRHeaton
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save JRHeaton/23529db396bf2b0f949d to your computer and use it in GitHub Desktop.

Select an option

Save JRHeaton/23529db396bf2b0f949d to your computer and use it in GitHub Desktop.
cool way to create a lazy collection that fetches from an indexed function
func lazyCollection<T>(#count: () -> Int, #get: Int -> T) -> LazyRandomAccessCollection<MapCollectionView<Range<Int>, T>> {
return lazy(0..<count()).map(get)
}
// Example
import CoreMIDI
let devices = lazyCollection(count: MIDIGetNumberOfDevices, get: MIDIGetDevice)
devices.array // compute all
devices[0] // MIDIDeviceRef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment