F# | C# | Scala | Clojure | Python | Ruby | Haskell | SQL | OCaml | Common Lisp | Erlang | Smalltalk | Scheme | Ecmascript 5 | Perl 5 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
map | Select | map | map | map | collect | map | Select | map | mapcar | map | collect: | map | map | map |
filter | Where | filter | filter | filter | select | filter | Where | filter | remove-if-not | filter | select: | filter | filter | grep |
fold | Aggregate | foldLeft | reduce | reduce | inject | foldl |
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
func passAnyObject(param: AnyObject) { | |
print(param) | |
} | |
class MyClass {} | |
struct MyStruct {} | |
let a: Int = 1 | |
let b = 2.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
// 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 |
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
{ | |
/* 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. | |
* |
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
var randomNumber: Int32 = 1 | |
withUnsafeMutablePointer(&randomNumber) { (pointer) -> Void in | |
var unsafeBitCastedPointer = unsafeBitCast(pointer, UnsafeMutablePointer<UInt8>.self) | |
SecRandomCopyBytes(kSecRandomDefault, 4, unsafeBitCastedPointer) | |
} | |
print(randomNumber) // 1092372360 |
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
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 | |
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
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 } |
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
// 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 |
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
enum SuperManIdentityError: ErrorType { | |
case WrongSecretIdentityError | |
} | |
func isSuperManTest(testWithSecretIdentity secretIdentity: String) throws -> String { | |
if secretIdentity != "Clark Kent" { | |
throw SuperManIdentityError.WrongSecretIdentityError | |
} | |
return secretIdentity | |
} |
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
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 } |