This file contains hidden or 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/Foundation.h> | |
| NS_ASSUME_NONNULL_BEGIN | |
| @interface ErrorMachine : NSObject | |
| +(nullable NSString*)foo:(nonnull NSNumber*)num error:(NSError **)error; | |
| +(nullable NSString*)bar:(nonnull NSNumber*)num error:(NSError **)error; | |
| +(nullable NSString*)baz:(nonnull NSNumber*)num error:(NSError **)error; |
This file contains hidden or 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 String { | |
| var isHomogeneous: Bool { | |
| if let firstChar = characters.first { | |
| for char in dropFirst(characters) where char != firstChar { | |
| return false | |
| } | |
| } | |
| return true | |
| } | |
| } |
This file contains hidden or 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
| // (c) 2015 Nate Cook, licensed under the MIT license | |
| // | |
| // Fisher-Yates shuffle as protocol extensions | |
| extension CollectionType { | |
| /// Return a copy of `self` with its elements shuffled | |
| func shuffle() -> [Generator.Element] { | |
| var list = Array(self) | |
| list.shuffleInPlace() | |
| return list |
This file contains hidden or 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
| // (c) 2015 Nate Cook, licensed under the MIT license | |
| // | |
| // ShuffledCollection.swift | |
| // | |
| // Requires shuffle methods from https://gist.github.com/natecook1000/ef096622dab1981823c5 | |
| /// Type-erasing shuffling wrapper for any random access collection | |
| struct ShuffledCollection<T> : CollectionType, _CollectionDefaultsType, _CollectionGeneratorDefaultsType { | |
| private let collection: AnyRandomAccessCollection<T> | |
| private var shuffledIndices: [AnyRandomAccessIndex] |
This file contains hidden or 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 Foo { | |
| init(a: String) { | |
| print("init(a: String)") | |
| } | |
| init(b: String) { | |
| print("init(b: String)") | |
| } | |
| init(a: Int) { |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 CollectionType { | |
| /// Return a lazy SequenceType containing pairs *(i, x)*, | |
| /// where *i*s are the sequential indices and *x*s are the elements of `base`. | |
| func enumerateWithIndex() -> AnySequence<(Index, Generator.Element)> { | |
| var index = startIndex | |
| return AnySequence { | |
| return anyGenerator { | |
| guard index != self.endIndex else { return nil } | |
| return (index, self[index++]) | |
| } |
This file contains hidden or 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
| protocol StringType { | |
| var isEmpty: Bool { get } | |
| } | |
| extension String : StringType { } | |
| extension Optional where Wrapped: StringType { | |
| var isNullOrEmpty: Bool { | |
| return self?.isEmpty ?? true | |
| } |