Skip to content

Instantly share code, notes, and snippets.

View alskipp's full-sized avatar

Al Skipp alskipp

View GitHub Profile
@alskipp
alskipp / uniq.swift
Last active August 29, 2015 14:06
Generic uniq function in Swift
// uniq func for Swift 1.2
func uniq<C: ExtensibleCollectionType where C.Generator.Element: Hashable>(collection: C) -> C {
var seen:Set<C.Generator.Element> = Set()
return reduce(collection, C()) { result, item in
if seen.contains(item) {
return result
}
@alskipp
alskipp / gist:2d81bc316926d779f185
Created September 8, 2014 22:02
Curried, partially applied fizzBuzz Swift
// Declared as a curried function
func fizzBuzzWithOptions(opts:[(divisor: Int, str: String)]) (num: Int) -> String {
let returnString = opts.reduce(String()) { acc, opt in
num % opt.divisor == 0 ? acc + opt.str : acc
}
return returnString.isEmpty ? String(num) : returnString + "!"
}
// Can be called inline with one argument to map
let array1 = [Int](1...100).map(fizzBuzzWithOptions([(3, "Fizz"), (5, "Buzz")]))
@alskipp
alskipp / macruby_fiber.rb
Created January 5, 2012 14:05
Macruby Fibers using GCD (transfer method implemented, 'double resume' error needs implementing)
class FiberError < StandardError; end
class Fiber
attr_accessor :name
def initialize &block
raise ArgumentError, 'new Fiber requires a block' unless block_given?
@block = block
@yield_sem = Dispatch::Semaphore.new(0)
@resume_sem = Dispatch::Semaphore.new(0)
@alskipp
alskipp / video_santafy.rb
Created December 20, 2011 14:38 — forked from pvinis/video_mustachify
macruby video Santa-fy (with support for multiple Santas)
framework 'Cocoa'
framework 'avfoundation'
class Santa
t_url = NSURL.URLWithString("http://dl.dropbox.com/u/349788/mustache.png")
t_source = CGImageSourceCreateWithURL t_url, nil
@@tache = CGImageSourceCreateImageAtIndex t_source, 0, nil
g_url = NSURL.URLWithString("http://dl.dropbox.com/u/349788/glasses.png")
g_source = CGImageSourceCreateWithURL g_url, nil
@alskipp
alskipp / gist:1469659
Created December 12, 2011 23:34
Macruby live(ish) moustachification in Mac OS X Lion.
framework 'Cocoa'
framework 'avfoundation'
class FaceDetectionDelegate
attr_accessor :window
def applicationDidFinishLaunching(aNotification)
width = 640
height = 480
@alskipp
alskipp / face_detector.rb
Created December 9, 2011 23:00 — forked from mattetti/face_detector.rb
Macruby Face Detection in Mac OS X Lion. Usage: $ macruby face_detector.rb or $ macruby face_detector.rb image_url
framework 'Cocoa'
class FaceDetectionDelegate
attr_accessor :window
def initWithURL(url)
case url
when String
@photo_url = NSURL.URLWithString(url)
when NSURL