Skip to content

Instantly share code, notes, and snippets.

Ben Cohen airspeedswift

Block or report user

Report or block airspeedswift

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
airspeedswift / permute.swift
Last active Jul 26, 2018
Permutation for MutableCollection
View permute.swift
import QuartzCore
/// Reorder elements in-place into the next permutation, returning false when
/// the collection has been permuted into lexicographical order. To cycle
/// through all permutations, start with a lexicographically-sorted collection
/// and permute until it returns false.
/// - Complexity: O(*n*), where *n* is the length of the collection.
public extension MutableCollection where Self: BidirectionalCollection {
airspeedswift / RBTree.swift
Created Oct 4, 2015
red black tree updated for 2.1b2
View RBTree.swift
private enum ListNode<Element> {
case End
indirect case Node(Element, next: ListNode<Element>)
func cons(x: Element) -> ListNode<Element> {
return .Node(x, next: self)
public struct ListIndex<Element> {
airspeedswift / OrderedSet.swift
Created May 11, 2015
Swift Ordered Set based on Red/Black tree
View OrderedSet.swift
enum Color {
case Red, Black
class Tree<T: Comparable> {
let value: T
let left: Tree<T>?
let right: Tree<T>?
let color: Color
airspeedswift / match.swift
Last active Feb 3, 2018
Minimalist Regex Matcher in Swift
View match.swift
/// Brian Kernighan's article in Beautiful Code talks about the minimalist regex matcher written by
/// Rob Pike for their Practice of Programming book as "one of the best examples of recursion that I
/// have ever seen, and it shows the power of C pointers".
/// Swift strings don't use pointers, and the original code relied heavily on the last character of a
/// C string being `\0`, but you can reproduce many of the nice aspects of the original C code using a
/// combination of slicing and `dropFirst`, the `first` function that returns an optional you can then
/// compare to a non-optional character, and the Swift 1.2 `if...let...where`
/// In theory no string copying should be happening since the slices are just a subrange view on the
airspeedswift / shuffle.swift
Last active Feb 2, 2018 — forked from natecook1000/shuffle.swift
Fisher-Yates shuffle as protocol extension on any random-access collection
View shuffle.swift
// adapted from original, which was (c) 2015 Nate Cook, licensed under the MIT license
// Fisher-Yates shuffle as protocol extensions
import Darwin
extension CollectionType where Index: RandomAccessIndexType {
/// Return a copy of `self` with its elements shuffled
func shuffle() -> [Generator.Element] {
var list = Array(self)
View joke.swift
func tellJoke(name: String, character: Character) {
let punchline = name.filter { $0 != character }
let n = name.count - punchline.count
let joke = """
Q: Why does \(name) have \(n) \(character)'s in their name?
A: I don't know, why does \(name) have \(n) \(character)'s in their name?
Q: Because otherwise they'd be called \(punchline).
View MyReverse.swift
struct MyReversed<Base: BidirectionalCollection> {
let base: Base
extension MyReversed: Collection {
public enum Index: Comparable {
case element(Base.Index)
case end
airspeedswift / SubSliceView.swift
Created Jun 27, 2015
Anything can be Sliceable
View SubSliceView.swift
public struct SubSliceView<C: CollectionType>: Sliceable {
let collection: C
let bounds: Range<C.Index>
public var startIndex: C.Index { return bounds.startIndex }
public var endIndex: C.Index { return bounds.endIndex }
public subscript(idx: C.Index) -> C.Generator.Element
airspeedswift / lazysplit.swift
Created Jun 6, 2017
Lazy split: Swift 3 vs Swift 4
View lazysplit.swift
// Swift 4.0 (and 3.2 as well!)
struct LazySplit<Base: Collection>: Collection
where Base.Element: Equatable,
Base.SubSequence: Collection
struct Index: Comparable {
let start, end: Base.Index
static func ==(lhs: Index, rhs: Index) -> Bool { return lhs.start == rhs.start }
static func < (lhs: Index, rhs: Index) -> Bool { return lhs.start < rhs.start }
airspeedswift / COWTree.swift
Last active Mar 5, 2017
Swift copy-on-write behaviour for a struct using HeapBuffer
View COWTree.swift
// ideally we would define this inside the tree struct
private class _Node<T: Comparable> {
var _value: T
var _left: _Node<T>? = nil
var _right: _Node<T>? = nil
init(value: T) { _value = value }
You can’t perform that action at this time.