Skip to content

Instantly share code, notes, and snippets.

extension String.StringInterpolation {
mutating func appendInterpolation<T>(_ value: T?, default: @autoclosure () -> String) {
self.appendLiteral(value.map(String.init(describing:)) ?? `default`())
}
}
let num = Int("21")
print("The number is: \(num, default: "<invalid>")")
// The number is: 21
struct Transposition<Base: Collection>: RandomAccessCollection
where Base.Element: RandomAccessCollection
{
typealias Index = Int
typealias Indices = Range<Int>
var base: Base
struct Transposed: Collection {
typealias Index = Base.Index

The following table lists the Unicode properties as described here, along with the corresponding Unicode.Scalar.Properties property and type, if implemented.

Unicode Property Unicode.Scalar.Properties Type
General
Name name String?
Name_Alias nameAlias String?
Block
Age age `Unicode.Versi
// Based on https://gist.github.com/dabrahams/852dfdb0b628e68567b4d97499f196f9
struct Dispatch<Model> {
func apply<A, R>(_ a: A, _ f: (Model) -> R) -> R {
f(a as! Model)
}
}
protocol RandomAccessCollectionDispatch {
func fastCount<C: Collection>(_ x: C) -> Int?
}
/// Calls the given closure with a buffer over the given tuple's elements.
///
/// - Parameters:
/// - tuple: The tuple.
/// - elementType: The type of all of the elements in the tuple.
/// - body: The closure to execute.
///
/// - Returns: The result of `body`, if any.
func withUnsafeElementBufferPointer<Tuple, Element, Result>(
of tuple: Tuple,
public struct DefaultedDictionary<Key: Hashable, Value> {
var _data: [Key: Value]
var _default: Value
public init(default defaultValue: Value) {
_data = [:]
_default = defaultValue
}
public init(_ data: [Key: Value], default defaultValue: Value) {
struct EachPairCollection<Base: Collection>: Collection {
var base: Base
struct Index: Comparable {
var first: Base.Index
var second: Base.Index
// This is a little weird. Equality and comparisons
// are only based on `second`, to allow an endIndex
// to be created where both `first` and `second`
extension Collection where Element: Hashable {
/// Returns the first matching element in `needles`, along with its index.
func firstLiteralMatch<C: Collection>(from needles: C) -> (C.Element, Index)?
where C.Element: Collection, C.Element.Element == Element
{
let prefixes = Dictionary(grouping: needles, by: { $0.first! })
for i in indices {
if let possibleMatches = prefixes[self[i]] {
if let match = possibleMatches.first(where: { self[i...].starts(with: $0) }) {
return (match, i)
// These functions turn nested tuples into flat ones
func munge<T, U, V>(_ x: ((T, U), V)) -> (T, U, V) {
return (x.0.0, x.0.1, x.1)
}
func munge<T, U, V>(_ x: (T, (U, V))) -> (T, U, V) {
return (x.0, x.1.0, x.1.1)
}
/// A dictionary wrapper that uses a `CaseIterable` type as its key.
/// This differs from a dictionary in two ways:
///
/// - Key-value pairs are accessed in a fixed order, which is the same
/// as the `Key`'s `allCases` property.
/// - Every possible key must have a value given, so using the key-based
/// subscript returns a non-optional value.
struct CaseMap<Key: CaseIterable & Hashable, Value> : Collection {
typealias Index = Key.AllCases.Index