This file contains hidden or 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 animals = ["cow", "dog", "pig"] | |
var emoji = ["🐮", "🐶", "🐷"] | |
for (animal, pic) in Zip2(animals, emoji) { | |
println("\(animal) \(pic)") | |
} |
This file contains hidden or 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
extension Array { | |
func each (block : (T) -> ()) { | |
for item in self { | |
block(item) | |
} | |
} | |
} | |
animals.each(println) |
This file contains hidden or 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
extension NSURL : StringLiteralConvertible { | |
public class func convertFromStringLiteral(value: String) -> Self { | |
return self(string: value) | |
} | |
public class func convertFromExtendedGraphemeClusterLiteral(value: String) -> Self { | |
return self(string: value) | |
} | |
} |
This file contains hidden or 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
// TinyWeb OOP(ish) Style | |
import Foundation | |
class HTTPResponse { | |
let body: String | |
let responseCode: Int | |
init(body: String, responseCode: Int) { | |
self.body = body |
This file contains hidden or 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
// also see http://oleb.net/blog/2014/07/swift-strings/ | |
// Mark: Strings | |
// if you want to stringByReplacingOccurrencesOfString without using a nil range, you need to convert to NSString first | |
var str = "some junk" | |
let tmpString: NSString = str | |
str = tmpString.stringByReplacingOccurrencesOfString(" ", withString: "+", options: nil, range: NSMakeRange(1, 6)) | |
// find the length of a swift string |
This file contains hidden or 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 Name : StringLiteralConvertible, Equatable { | |
var firstName: String | |
var lastName: String | |
required init(firstName: String, lastName: String) { | |
self.firstName = firstName | |
self.lastName = lastName | |
} | |
class func convertFromStringLiteral(value: StringLiteralType) -> Self { |
This file contains hidden or 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 Fallible<T> { | |
case Error(NSError) | |
case Some(T) | |
init(value: T) { | |
self = .Some(value) | |
} | |
init(error: NSError) { | |
self = .Error(error) |
This file contains hidden or 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 EvenNaturalNumbers: SequenceType { | |
typealias GeneratorType = EvenNaturalNumbersGenerator | |
func generate() -> EvenNaturalNumbersGenerator { | |
return EvenNaturalNumbersGenerator() | |
} | |
} | |
class EvenNaturalNumbersGenerator : GeneratorType { | |
var current = 2 |
This file contains hidden or 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
// Playground - noun: a place where people can play | |
import UIKit | |
typealias Vector2D = (x: Float, y: Float) | |
typealias Point2D = Vector2D | |
func -(lhs: Vector2D, rhs: Vector2D) -> Vector2D { | |
return (lhs.x - rhs.x, lhs.y - rhs.y) | |
} |
This file contains hidden or 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 meanSquaredError(predictions: [Double], values: [Double]) -> Double { | |
let sum = reduce(Zip2(predictions, values), 0.0) { (acc, y) in | |
let diff = y.0 - y.1 | |
return acc + (diff * diff) | |
} | |
return sum / Double(predictions.count) | |
} | |
// what I want is to bind the arguments in the closure to (acc, (yHat, y)) |