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
// | |
// JBOptimizedRange.m | |
// Enumeration Performance Test | |
// | |
// Created by jakebromberg on 6/20/16. | |
// Copyright © 2016 jakebromberg. All rights reserved. | |
// | |
#import "JBOptimizedRange.h" |
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 | |
protocol Identifiable { | |
associatedtype IdentifierType : Equatable | |
var id : IdentifierType { get } | |
} | |
struct Activity : Identifiable { | |
let id : Int | |
} |
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 CircularIterator<S: Sequence> : IteratorProtocol { | |
let elements : S | |
var iterator : S.Iterator | |
init(elements: S) { | |
self.elements = elements | |
self.iterator = elements.makeIterator() | |
} | |
mutating func next() -> S.Iterator.Element? { |
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 is what I wish I could write, but it doesn't compile | |
typealias EmptyClosure<T> = (T) -> () | |
func MakeEmptyClosure<T>() -> EmptyClosure<T> { | |
return { _ in } | |
} | |
let emptyClosure = EmptyClosure<Int>() // Error: Cannot explicitly specialize a generic function |
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
typealias Predicate<T> = (T) -> Bool | |
func ||<T>(lhs: @escaping Predicate<T>, rhs: @escaping Predicate<T>) -> Predicate<T> { | |
return { element in | |
return lhs(element) || rhs(element) | |
} | |
} | |
func &&<T>(lhs: @escaping Predicate<T>, rhs: @escaping Predicate<T>) -> Predicate<T> { | |
return { element in |
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 OptionSet where RawValue: BinaryInteger, RawValue.Stride: SignedInteger { | |
func splitBitwise() -> [Self] { | |
let radix: [RawValue] = (0..<RawValue(0.bitWidth)).map { 1 << $0 } | |
let bitwiseRadices: [RawValue] = radix.map { self.rawValue & $0 } | |
let nonzeroBitwiseRadices: [RawValue] = bitwiseRadices.filter { $0 != 0 } | |
let result: [Self] = nonzeroBitwiseRadices.map { Self(rawValue: $0) } | |
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
extension Sequence { | |
typealias Element = Iterator.Element | |
func groupBy<T: Hashable>(grouper: (Element) -> (T)) -> Dictionary<T, [Element]> { | |
var groups = [T : [Element]]() | |
for element in self { | |
let groupKey = grouper(element) | |
var group = groups[groupKey] ?? [Element]() | |
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 Sequence where Element: Hashable { | |
func frequencies() -> [Element : Int] { | |
return Dictionary(map { ($0, 1) }, uniquingKeysWith: { $0 + $1 }) | |
} | |
} | |
assert("hello".frequencies() == ["e": 1, "o": 1, "l": 2, "h": 1]) |
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
final class ReadOnly<T> { | |
private let value: T | |
init(_ value: T) { | |
self.value = value | |
} | |
subscript<U>(keyPath: KeyPath<T, U>) -> U { | |
return self.value[keyPath: keyPath] | |
} |
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 CoreLocation | |
extension CLLocation { | |
public static func +(lhs: CLLocation, rhs: CLLocation) -> CLLocation { | |
let summedLat = lhs.coordinate.latitude + rhs.coordinate.latitude | |
let summedLong = lhs.coordinate.longitude + rhs.coordinate.longitude | |
return CLLocation(latitude: summedLat, longitude: summedLong) | |
} | |
public static func /(lhs: CLLocation, rhs: Double) -> CLLocation { |