Skip to content

Instantly share code, notes, and snippets.

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

  • Save JRHeaton/364461a563fd56f37c1b to your computer and use it in GitHub Desktop.

Select an option

Save JRHeaton/364461a563fd56f37c1b to your computer and use it in GitHub Desktop.
infix operator >>= { }
func >>= <A, B> (x: A?, f: A -> B?) -> B? {
if let val = x {
return f(val)
}
return nil
}
protocol MIDIEnumerable {
class func count() -> Int
class func atIndex(Int) -> Self?
}
struct MIDI {
static func enumerate<A: MIDIEnumerable>() -> GeneratorOf<A> {
var index = 0
return GeneratorOf<A> {
return A.atIndex(++index)
}
}
static func all<A: MIDIEnumerable>() -> [A] {
var ret: [A] = []
for val: A in enumerate() {
ret.append(val)
}
return ret
}
class Object {
let ref: MIDIObjectRef
var properties: NSDictionary? {
var cfProps: Unmanaged<CFPropertyList>?
MIDIObjectGetProperties(ref, &cfProps, 1)
return cfProps?.takeUnretainedValue() as? NSDictionary
}
init(ref: MIDIObjectRef) {
self.ref = ref
}
}
class Device: Object, MIDIEnumerable {
class func count() -> Int {
return Int(MIDIGetNumberOfDevices())
}
class func atIndex(index: Int) -> Self? {
if index < count() {
return self.init(ref: MIDIGetDevice(ItemCount(index)))
}
return nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment