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 UIKit | |
struct FibonacciGenerator: GeneratorType, SequenceType { | |
var (f1, f2) = (1, 1) | |
mutating func next() -> Int? { | |
let result = f1 | |
(f1, f2) = (f2, f1 + f2) | |
return result |
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 | |
typealias IndexedSlice = (slice: ArraySlice<Int>, index: Int) | |
let ascending: (Int, Int) -> Bool = { $0 < $1 } | |
func solution(input: [Int]) -> [Int] { | |
assert(input.count > 0, "Cannot calculate for empty array") | |
return largestAscendingSlicesWithIndex(input).map { $0.index } | |
} |
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>(inout variable: T?, expr: @autoclosure () -> T) { | |
if variable == nil { | |
variable = expr() | |
} |
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
public struct MatrixIndex: BidirectionalIndexType { | |
public let x, y : Int | |
private let columns: Int | |
public func successor() -> MatrixIndex { | |
return (x + 1 == columns) ? | |
MatrixIndex(x: 0, y: y + 1, columns: columns) : | |
MatrixIndex(x: x + 1, y: y, columns: columns) |
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 Currency { | |
let code, displayName: String | |
init?(code: String?) { | |
if let code = code, | |
displayName = NSLocale.systemLocale().displayNameForKey(NSLocaleCurrencyCode, value:code) { | |
self.code = code | |
self.displayName = displayName | |
} 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
import UIKit | |
typealias JSON = [String: AnyObject] | |
public typealias ImageRequestCompletion = (UIImage?, NSError?) -> Void | |
public enum UserImageSize: String { | |
case Epic = "epic" | |
case Big = "bigger" | |
case Normal = "normal" | |
case Tiny = "mini" |
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
public extension IteratorProtocol { | |
mutating public func any(_ predicate: (Element) throws -> Bool) rethrows -> Bool { | |
guard let current = next() else { return false } | |
return try predicate(current) || any(predicate) | |
} | |
mutating public func all(_ predicate: (Element) throws -> Bool) rethrows -> Bool { | |
guard let current = next() else { return true } | |
return try predicate(current) && all(predicate) |
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
@implementation NSString (Levenshtein) | |
- (NSUInteger)lev_distanceToString:(NSString *)comparisonString { | |
NSString *referenceString = [self copy]; | |
NSUInteger referenceLength = referenceString.length; | |
NSUInteger comparisonLength = comparisonString.length; | |
unsigned long m, n; | |
unsigned int distanceMatrix[comparisonLength + 1][referenceLength + 1]; | |
distanceMatrix[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
typedef void(^StringEnumerationBlock)(NSString *characterString, NSUInteger index, BOOL *stop); | |
- (void)enumerateCharactersUsingBlock:(StringEnumerationBlock)block { | |
NSParameterAssert(block); | |
BOOL stop = NO; | |
for (NSUInteger i = 0; i < self.length; i++) { | |
const unichar character = [self characterAtIndex:i]; | |
NSString *characterString = [NSString stringWithCharacters:&character length:1]; | |
block(characterString, i, &stop); | |
if (stop) { |
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
NSMutableDictionary *vibrationParameters = [[NSMutableDictionary alloc] init]; | |
vibrationParameters[@"VibePattern"] = @[@YES, @100]; | |
vibrationParameters[@"Intensity"] = @1; | |
#pragma clang diagnostic push | |
#pragma clang diagnostic ignored "-Wimplicit-function-declaration" | |
AudioServicesStopSystemSound(kSystemSoundID_Vibrate); | |
AudioServicesPlaySystemSoundWithVibration(kSystemSoundID_Vibrate, nil, vibrationParameters); | |
#pragma clang diagnostic pop |