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 SignedNumberType { | |
/// The absolute value of `self`. | |
var abs: Self { | |
return self < 0 ? -self : self | |
} | |
} | |
extension AbsoluteValuable { | |
/// The absolute value of `self`. | |
var abs: Self { |
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
let string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi enim lacus, ullamcorper in gravida a, semper id dolor. Mauris quis metus id" | |
extension String { | |
func split(separator: Character, maxSplit: Int = .max, allowEmptySlices: Bool = false) -> [String] { | |
return characters.split(separator, maxSplit: maxSplit, allowEmptySlices: allowEmptySlices).map(String.init) | |
} | |
} | |
let words = try string.split(" ") | |
let counts = words.map { $0.characters.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
/// An infinite sequence of positive integers. | |
struct PostiveIntegers: SequenceType { | |
typealias Generator = AnyGenerator<Int> | |
func generate() -> Generator { | |
var n = 0 | |
return anyGenerator { | |
return ++n | |
} | |
} |
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
import Foundation | |
/* | |
Toying with tools to help read binary formats. | |
I've seen lots of approaches in swift that create | |
an intermediate object per-read (usually another NSData) | |
but even if these are lightweight under the hood, | |
it seems like overkill. Plus this taught me about <()> | |
*/ |
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
for (key, value) in components { | |
if (value === f) { | |
let component = components.removeValueForKey(key) | |
println(component) | |
break | |
} | |
} |
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
// this version of memoize is from the WWDC 2014 Advanced Swift session | |
// https://developer.apple.com/videos/wwdc/2014/?id=404 | |
func memoize<T: Hashable, U>(body: (T -> U, T) -> U ) -> (T) -> U { | |
var memo = [T: U]() | |
var result: (T -> U)! | |
result = { | |
value in | |
if let cached = memo[value] { return cached } | |
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 Character { | |
func toUnicodeScalar() -> UnicodeScalar? { | |
let s = String(self) | |
if distance(s.unicodeScalars.startIndex, s.unicodeScalars.endIndex) > 1 { | |
return nil | |
} | |
return s.unicodeScalars[s.unicodeScalars.startIndex] | |
} | |
} | |
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 arc4random_uniform<T: SignedIntegerType>(max: T) -> T { | |
let max32: Int32 = numericCast(max) | |
return T(Int64(arc4random_uniform(UInt32(max32)))) | |
} | |
func arc4random_uniform<T: UnsignedIntegerType>(max: T) -> T { | |
let max32: UInt32 = numericCast(max) | |
return T(UInt64(arc4random_uniform(max32))) | |
} |
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
// see also https://gist.github.com/griotspeak/8bb4c46611fc90d3043b | |
func findFirst<S: SequenceType>(seq: S, predicate: S.Generator.Element -> Bool) -> S.Generator.Element? { | |
for x in seq { | |
if predicate(x) { return x } | |
} | |
return nil | |
} | |
func not<T>(predicate: T -> Bool) -> (T -> Bool) { |
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
// adadpted from http://atp.fm/t-shirt | |
import UIKit | |
class ATPView : UIView { | |
override func drawRect(rect: CGRect) { | |
let fontName = "MyriadPro-SemiBold" | |
let title = NSLocalizedString("Accidental Tech Podcast", comment: "title") | |
let initials = NSLocalizedString("ATP", comment: "initials") |
OlderNewer