Skip to content

Instantly share code, notes, and snippets.

//
// JBOptimizedRange.m
// Enumeration Performance Test
//
// Created by jakebromberg on 6/20/16.
// Copyright © 2016 jakebromberg. All rights reserved.
//
#import "JBOptimizedRange.h"
import Foundation
protocol Identifiable {
associatedtype IdentifierType : Equatable
var id : IdentifierType { get }
}
struct Activity : Identifiable {
let id : Int
}
@jakebromberg
jakebromberg / CircularIterator.swift
Last active June 2, 2017 00:31
The CircularIterator iterates over the sequence it ingests. When it exhausts the elements of the sequence, it begins afresh.
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? {
@jakebromberg
jakebromberg / EmptyClosure1.swift
Last active August 6, 2017 20:51
Empty closures and syntactic frustrations
// 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
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
@jakebromberg
jakebromberg / EnumeratingOptionSets.swift
Created August 29, 2017 00:02
Lets you represent OptionSets in an enumerable form since OptionSets are not Sequences. Don't try this at home.
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
}
}
@jakebromberg
jakebromberg / GroupBy.swift
Last active August 29, 2017 16:25
Group elements in a sequence. Note that this implementation predates Swift 4's `grouping` operation
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]()
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])
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]
}
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 {