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 choose<S : SequenceType, T : SequenceType, U : SequenceType where U.Generator.Element == Bool, S.Generator.Element == T.Generator.Element>(onTrue: S, onFalse: T, picker: U) -> [S.Generator.Element]{ | |
return map2(Zip2(onTrue, onFalse), picker, { zipped, choice in choice ? zipped.0 : zipped.1 }) | |
} | |
// Note map2 is defined here: https://gist.github.com/JadenGeller/6f5c5dcb16c39b936d17 |
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
// Note these divide functions both except identical inputs | |
func divide(a: Int, b: Int) -> Int { | |
return a / b | |
} | |
func divide(a: Int, b: Int) -> Float { | |
return Float(a) / Float(b) | |
} |
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 | |
var x = [1, 2, 3, 4, 5] // [1, 2, 3, 4, 5] | |
var y = [1, 1, 2, 3, 5] // [1, 1, 2, 3, 5] | |
var z = x - y // [0, 1, 1, 1, 0] | |
x[ x % 2 != 0 ] = x + 1 // [2, 2, 4, 4, 6] | |
y[ y > 2 * z ] = y * y // [1, 1, 2, 9, 25] | |
z[ x > 2 && x < 6 ] = 0 // [0, 1, 0, 0, 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
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 Random { | |
static func within<B: protocol<Comparable, ForwardIndexType>>(range: ClosedInterval<B>) -> B { | |
let inclusiveDistance = range.start.distanceTo(range.end).successor() | |
let randomAdvance = B.Distance(arc4random_uniform(UInt32(inclusiveDistance.toIntMax())).toIntMax()) | |
return range.start.advancedBy(randomAdvance) | |
} | |
static func within(range: ClosedInterval<Float>) -> Float { | |
return (range.end - range.start) * Float(Float(arc4random()) / Float(UInt32.max)) + range.start | |
} |
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 | |
} |