Skip to content

Instantly share code, notes, and snippets.

View damianesteban's full-sized avatar
🎯
Focusing

Damian Esteban damianesteban

🎯
Focusing
View GitHub Profile
func passAnyObject(param: AnyObject) {
print(param)
}
class MyClass {}
struct MyStruct {}
let a: Int = 1
let b = 2.0
@damianesteban
damianesteban / nthFibonacci.swift
Last active November 26, 2015 16:14
The nth number in the fibonacci sequence
// Find the `nth` number in the Fibonacci sequence recursively.
func nthFibonacci(n: Int) -> (Int) {
if n <= 1 {
return 1
} else {
return nthFibonacci(n - 1) + nthFibonacci(n - 2)
}
}
nthFibonacci(10) // 89
@damianesteban
damianesteban / DefaultKeyBinding.dict
Created November 12, 2015 19:57
xcode keybindings for emacs emulation
{
/* Keybindings for emacs emulation. Compiled by Jacob Rus.
*
* To use: copy this file to ~/Library/KeyBindings/
* after that any Cocoa applications you launch will inherit these bindings
*
* This is a pretty good set, especially considering that many emacs bindings
* such as C-o, C-a, C-e, C-k, C-y, C-v, C-f, C-b, C-p, C-n, C-t, and
* perhaps a few more, are already built into the system.
*
@damianesteban
damianesteban / randomNumberSecRandomCopyBytes.swift
Created November 9, 2015 06:42
Random number in Swift via SecRandomCopyBytes
var randomNumber: Int32 = 1
withUnsafeMutablePointer(&randomNumber) { (pointer) -> Void in
var unsafeBitCastedPointer = unsafeBitCast(pointer, UnsafeMutablePointer<UInt8>.self)
SecRandomCopyBytes(kSecRandomDefault, 4, unsafeBitCastedPointer)
}
print(randomNumber) // 1092372360
F#C#ScalaClojurePythonRubyHaskellSQLOCamlCommon LispErlangSmalltalkSchemeEcmascript 5Perl 5
mapSelectmapmapmapcollectmapSelectmapmapcarmapcollect:mapmapmap
filterWherefilterfilterfilterselectfilterWherefilterremove-if-notfilterselect:filterfiltergrep
foldAggregatefoldLeftreducereduceinjectfoldl
@damianesteban
damianesteban / keyboardWasShown.swift
Created October 17, 2015 18:38
UIScrollView and UITextView
func keyboardWasShown(notification: NSNotification) {
if let userInfo = notification.userInfo {
let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue().size
let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0)
scrollView.contentInset = contentInsets
scrollView.scrollIndicatorInsets = contentInsets
let animationDuration: NSTimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
@damianesteban
damianesteban / SillySet.swift
Created August 31, 2015 13:41
A 'Set' in Swift...even though we already have them now.
struct SillySet<T: Hashable> {
typealias Element = T
private var contents: [Element: Bool]
init() {
self.contents = [Element: Bool]()
}
// number of elemnts in the set.
var count: Int { return contents.count }
@damianesteban
damianesteban / multiples.swift
Created August 28, 2015 17:56
MultiplesOfThreeAndFive
// Ghetto Way
var numbers : [Int] = []
func multiplesOfThreeAndFive(range1: Int, range2: Int) -> Int {
for i in range1..<range2 {
if i % 3 == 0 || i % 5 == 0 {
numbers.append(i)
}
}
let sumOfMultiples = numbers.reduce(0, combine: +)
return sumOfMultiples
@damianesteban
damianesteban / SuperManIdentityTest.swift
Created August 27, 2015 15:29
Test for Superman in Swift 2
enum SuperManIdentityError: ErrorType {
case WrongSecretIdentityError
}
func isSuperManTest(testWithSecretIdentity secretIdentity: String) throws -> String {
if secretIdentity != "Clark Kent" {
throw SuperManIdentityError.WrongSecretIdentityError
}
return secretIdentity
}
@damianesteban
damianesteban / ErrorHandling.swift
Created August 26, 2015 14:32
guard, try, catch and throw - swift 2
enum JSONParsingError: String, ErrorType {
case URLCreationFailed
case SerializationFailed
case DataDownloadingFailed
case DictionaryError
}
func fetchUserRepositories(urlString: String) throws {
guard let reposURL = NSURL(string: urlString) else { throw JSONParsingError.URLCreationFailed }
guard let jsonData = NSData(contentsOfURL: reposURL) else { throw JSONParsingError.DataDownloadingFailed }