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 String { | |
func distanceIn(cView: String.CharacterView, toMatchingCharacter match: Character) -> Int { | |
var count = 0 | |
for c in cView.reverse() { | |
if c == match { | |
return count | |
} else { | |
count++ | |
} |
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 Swift | |
struct BigInt { | |
var value : [UInt64] = [0] | |
} | |
extension BigInt : CustomDebugStringConvertible { | |
var debugDescription : String { | |
return String(value) | |
} |
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
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
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 Collection { | |
/// Splits a collection into its first element and a SubSequence of the rest of its elements. | |
func split() -> (head: Element, tail: SubSequence)? { | |
guard let first = first else { | |
return nil | |
} | |
return (first, dropFirst()) | |
} |
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 | |
} | |
} |