Skip to content

Instantly share code, notes, and snippets.

View JRHeaton's full-sized avatar

John Heaton JRHeaton

View GitHub Profile
@JRHeaton
JRHeaton / pcomb.swift
Created April 4, 2015 01:07
learning about parser combinators (this is likely wrong code)
struct Parser<Input: CollectionType, Output> {
typealias Function = (Input, Input.Index) -> (Output, Input.Index)?
}
func parseString(string: String) -> Parser<String, String>.Function {
return { str, index in
let end = advance(index, count(string))
if str.substringWithRange(index..<end) == string {
@JRHeaton
JRHeaton / lazyindex.swift
Last active August 29, 2015 14:18
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
@JRHeaton
JRHeaton / midimsg.swift
Last active May 6, 2023 16:41
elegant midi message model w/ swift
import CoreMIDI
enum ChannelMessage {
case NoteOn(key: UInt8, velocity: UInt8)
case NoteOff(key: UInt8)
case ControlChange(controller: UInt8, value: UInt8)
func bytesForChannel(channel: Int) -> [UInt8] {
let status = { $0 | (UInt8(channel) & 0x0F) }
let removeMSB = { $0 & UInt8(0x7F) }
@JRHeaton
JRHeaton / hue.swift
Last active August 29, 2015 14:17
hue in swift
import Foundation
typealias LightState = [String:NSObject]
enum LightCommand {
enum LightEffect: String {
case None = "none"
case ColorLoop = "colorloop"
}
enum LightAlert: String {
@JRHeaton
JRHeaton / zip3.swift
Created February 19, 2015 08:46
Implementing zip3 in Swift
struct Zip3Generator
<
A: GeneratorType,
B: GeneratorType,
C: GeneratorType
>: GeneratorType {
private var first: A
private var second: B
request = require 'request'
_ = require 'underscore'
exec = require('child_process').exec
options =
url: 'https://api.github.com/users/JRHeaton/repos?type=owner'
json: true
headers:
'User-Agent': 'jgithubdownload'
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
import CoreMIDI
extension MIDIPacket {
static func channel(status: UInt8, data1: UInt8, data2: UInt8) -> MIDIPacket {
let newPacket = UnsafeMutablePointer<MIDIPacket>.alloc(1)
newPacket.memory.length = 3
withUnsafePointer(&newPacket.memory.data, { (ptr) -> () in
let dataPtr = UnsafeMutablePointer<UInt8>(ptr)
dataPtr[0] = status
@JRHeaton
JRHeaton / osxtheme.m
Last active August 29, 2015 14:14
set yosemite dark mode programatically
void setOSXThemeDark(BOOL dark) {
CFPreferencesSetAppValue(CFSTR("AppleInterfaceStyle"), !dark ? nil : CFSTR("Dark"), kCFPreferencesAnyApplication);
CFNotificationCenterPostNotification(CFNotificationCenterGetDistributedCenter(), CFSTR("AppleInterfaceThemeChangedNotification"), nil, nil, 1);
}
//
// SignalExtensions.swift
// Fan Club
//
// Created by John Heaton on 12/18/14.
// Copyright (c) 2014 StageBloc. All rights reserved.
//
import Foundation