Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
JadenGeller / Choose Function.swift
Created March 24, 2015 01:57
Swift Choose Function
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
@JadenGeller
JadenGeller / Return Type Overloading.swift
Last active August 29, 2015 14:17
Swift Overloading on a Function's Return Type
// 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)
}
@JadenGeller
JadenGeller / Array Masking.swift
Created March 24, 2015 05:20
Swift Array Masking Operations
// 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]
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
}
@JadenGeller
JadenGeller / Random.swift
Last active May 25, 2017 20:23
Random Numbers in Swift
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
}
@JadenGeller
JadenGeller / Bulk Generator.swift
Created March 24, 2015 09:14
Swift Bulk Generator
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
@JadenGeller
JadenGeller / Combined Comparison.swift
Last active August 29, 2015 14:17
Swift Combined Comparison Operator
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 {
@JadenGeller
JadenGeller / Within Syntax.swift
Last active August 29, 2015 14:17
Swift Within Syntax
// 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)
@JadenGeller
JadenGeller / Regex.swift
Last active August 29, 2015 14:17
Swift Regex
// 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)
}
@JadenGeller
JadenGeller / Call-By-Name Example.swift
Created March 26, 2015 22:45
Swift "call-by-name"
// 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
}