Skip to content

Instantly share code, notes, and snippets.

View alskipp's full-sized avatar

Al Skipp alskipp

View GitHub Profile
@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
@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 / 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 / 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 / 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 / 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 / encrypt_xor1.swift
Last active November 1, 2018 11:10
Swift encrypt/decrypt string using XOR
import Foundation
extension Character {
func utf8() -> UInt8 {
let utf8 = String(self).utf8
return utf8[utf8.startIndex]
}
}
func encrypt(c:Character, key:Character) -> String {
@alskipp
alskipp / creditCardValidationA.swift
Last active April 30, 2016 14:12
Credit card validation in Swift for Swift London Meetup
// Swift 2.0
/* Function composition operator */
infix operator •> { associativity left precedence 150 }
func •> <A,B,C> (f:A -> B, g:B -> C ) -> A -> C {
return { g(f($0)) }
}
extension Array where T: IntegerType {
func sum() -> T {
@alskipp
alskipp / haskell_towers.hs
Created January 14, 2015 15:01
The Towers of Hanoi – Haskell/Swift comparison
type Peg = String
type Move = (Peg, Peg)
hanoi :: Integer -> Peg -> Peg -> Peg -> [Move]
hanoi 0 _ _ _ = []
hanoi n a b c = hanoi (n-1) a c b ++ [(a,b)] ++ hanoi (n-1) c b a
{-
Usage:
hanoi 3 "a" "b" "c"
@alskipp
alskipp / beasts_to_feed.hs
Last active August 29, 2015 14:14
Swift cat care with enums and protocols (+ Haskell for a varied diet)
type MilliLitresWaterPerGram = Double
type CaloriesPerGram = Double
type Grams = Double
data Food = Dry CaloriesPerGram
| Wet CaloriesPerGram MilliLitresWaterPerGram
deriving (Show, Eq)
data Animal = Cat { clawLength :: Double, calorieCount :: Double, hydrationLevel :: Double }
| Dog { barkVolume :: Double, calorieCount :: Double, hydrationLevel :: Double }