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
extension Array { | |
func shuffled() -> [T] { | |
var list = self | |
for i in 0..<(list.count - 1) { | |
let j = Int(arc4random_uniform(UInt32(list.count - i))) + i | |
swap(&list[i], &list[j]) | |
} | |
return list | |
} |
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 BulkGenerator<G : GeneratorType> : GeneratorType { | |
var generator: G | |
var bulk: [G.Element] | |
var size: Int | |
init(generator: G, size: Int){ | |
assert(size > 0, "Bulk size must be a positive integer") | |
self.generator = generator | |
self.size = size |
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
infix operator <=> { associativity none precedence 130 } | |
func <=><T: Comparable>(lhs: T, rhs: T) -> NSComparisonResult { | |
if lhs > rhs { | |
return .OrderedDescending | |
} | |
else if lhs < rhs { | |
return .OrderedAscending | |
} | |
else { |
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
// Allows you to use || within switch statement cases :D | |
// Implicitly combines two equatable elements into a set | |
func ||<T: Equatable>(lhs: T, rhs: T) -> Set<T> { | |
return Set([lhs, rhs]) | |
} | |
// Implicitly adds an equatable element into a set | |
func ||<T: Equatable>(var lhs: Set<T>, rhs: T) -> Set<T> { | |
lhs.insert(rhs) |
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
// Making the NSRegularExpression a little bit more pretty | |
typealias RegularExpression = NSRegularExpression | |
extension RegularExpression { | |
convenience init?(pattern: String, options: NSRegularExpressionOptions) { | |
self.init(pattern: pattern, options: options, error: nil) | |
} | |
convenience init?(pattern: String) { | |
self.init(pattern: pattern, options: nil, error: 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
// Example of how call-by-name can be achieved through use of lazy evaluation | |
let a = [1,2,3] | |
var i = 1 | |
func f(@autoclosure j: () -> Int) { | |
println(j()) // -> 2 | |
i = 0 | |
println(j()) // -> 1 | |
} |
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
// Example | |
for x in 1...10 { | |
if 3 < x <= 8 { | |
println(x) // -> 4 -> 5 -> 6 -> 7 -> 8 | |
} | |
} | |
// Implementation |
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
infix operator --> {associativity right precedence 90 assignment } | |
func --><T : IntegerType>(inout lhs: T, rhs: T) -> Bool { | |
if (lhs != rhs) { // Stride | |
lhs = lhs.advancedBy(rhs > lhs ? 1 : -1) | |
return true | |
} | |
else { | |
return false | |
} | |
} |
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
extension Dictionary { | |
init(_ elements: [Element]){ | |
self.init() | |
for (k, v) in elements { | |
self[k] = v | |
} | |
} | |
func map<U>(transform: Value -> U) -> [Key : U] { | |
return Dictionary<Key, U>(Swift.map(self, { (key, value) in (key, transform(value)) })) |
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
// Learn more: http://en.wikipedia.org/wiki/INTERCAL#Operators | |
infix operator ~ { | |
associativity left | |
precedence 140 | |
} | |
// Intercal select | |
func ~(var lhs: Int, var rhs: Int) -> Int { | |
let lsb = 1 |