Skip to content

Instantly share code, notes, and snippets.

/// This is a useful index that can store a comparable element or the end of
/// a collection. Similar to https://github.com/apple/swift/pull/15193
enum IndexWithEnd<T : Comparable> : Comparable {
case element(T)
case end
static func < (lhs: IndexWithEnd, rhs: IndexWithEnd) -> Bool {
switch (lhs, rhs) {
case (.element(let l), .element(let r)):
return l < r
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?
}
@natecook1000
natecook1000 / openRanges.swift
Last active May 27, 2021 19:37
Open-ended range operators for Swift
// Open-ended range operators
//
// 100... is equivalent to 100...Int.max
// ...-100 is equivalent to Int.min...-100
// ..<3 is equivalent to Int.min..<3
import Swift
/// Conforming types provide static `max` and `min` constants.
protocol MinMaxType {
extension Collection {
/// Chooses at random the given number of elements, using reservoir sampling
/// aka Algorithm R.
///
/// - Complexity: O(*n*) where *n* is the collection's count.
func choose<T>(upTo max: Int, using generator: T) -> [Element]
where T: RandomNumberGenerator
{
var i = index(startIndex, offsetBy: max, limitedBy: endIndex) ?? endIndex
var result = self[..<i].shuffled(using: generator)
@natecook1000
natecook1000 / VulgarFraction.swift
Created August 28, 2014 03:08
Vulgar Fractions
// (c) 2014 Nate Cook, licensed under the MIT License
/**
Returns a tuple with the closest compound fraction possible with Unicode's built-in "vulgar fractions".
See here: http://www.unicode.org/charts/PDF/U2150.pdf
:param: number The floating point number to convert.
:returns: A tuple (String, Double): the string representation of the closest possible vulgar fraction and the value of that string
@natecook1000
natecook1000 / Swift-3.0-Hierarchy.png
Last active March 19, 2020 20:21
Swift StdLib Type/Protocol Hierarchies
Swift-3.0-Hierarchy.png
/// 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,
@natecook1000
natecook1000 / Set.swift
Last active March 6, 2020 02:19
Creating a Set Type in Swift
// The MIT License (MIT)
//
// Copyright (c) 2014 Nate Cook
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions: