Skip to content

Instantly share code, notes, and snippets.

@natecook1000
natecook1000 / ErrorMachine.h
Last active August 29, 2015 14:23
Crashing error from Objective-C methods
#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;
@natecook1000
natecook1000 / homogeneous.swift
Last active April 15, 2023 07:56
Swift-only homogeneousnessedness
extension String {
var isHomogeneous: Bool {
if let firstChar = characters.first {
for char in dropFirst(characters) where char != firstChar {
return false
}
}
return true
}
}
@natecook1000
natecook1000 / shuffle.swift
Last active April 5, 2017 21:17
Swift 2.0 shuffle / shuffleInPlace
// (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
@natecook1000
natecook1000 / ShuffledCollection.swift
Created June 30, 2015 20:31
Shuffling wrapper for a random access collection
// (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]
@natecook1000
natecook1000 / inits.swift
Created July 3, 2015 22:05
Ambiguous inits
struct Foo {
init(a: String) {
print("init(a: String)")
}
init(b: String) {
print("init(b: String)")
}
init(a: Int) {
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 {
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 }
@natecook1000
natecook1000 / lazyPrimes.swift
Created August 28, 2015 06:34
Lazily-generated infinite sequence of primes
/// An infinite sequence of positive integers.
struct PostiveIntegers: SequenceType {
typealias Generator = AnyGenerator<Int>
func generate() -> Generator {
var n = 0
return anyGenerator {
return ++n
}
}
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++])
}
protocol StringType {
var isEmpty: Bool { get }
}
extension String : StringType { }
extension Optional where Wrapped: StringType {
var isNullOrEmpty: Bool {
return self?.isEmpty ?? true
}