This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
framework 'Cocoa' | |
class FaceDetectionDelegate | |
attr_accessor :window | |
def initWithURL(url) | |
case url | |
when String | |
@photo_url = NSURL.URLWithString(url) | |
when NSURL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
framework 'Cocoa' | |
framework 'avfoundation' | |
class FaceDetectionDelegate | |
attr_accessor :window | |
def applicationDidFinishLaunching(aNotification) | |
width = 640 | |
height = 480 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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")])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension Character { | |
func utf8() -> UInt8 { | |
let utf8 = String(self).utf8 | |
return utf8[utf8.startIndex] | |
} | |
} | |
func encrypt(c:Character, key:Character) -> String { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
OlderNewer