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
View cowed.swift
/// Conform references types for use in the COW wrapper to this protocol
protocol Cowable: class {
/// Make a new unique instance of `copied`
static func makeUnique(_ copied: Self) -> Self
}
/// A wrapper that turns a Cowable reference type into a value-semantic
/// type with access to all of its properties
@dynamicMemberLookup
@airspeedswift
airspeedswift / Bounded.swift
Created Oct 26, 2018
Bound a sequence with a start and end marker
View Bounded.swift
struct BoundedSequence<Base: Sequence> {
let _base: Base
}
extension BoundedSequence {
struct Iterator {
enum State { case starting, iterating, ended }
var _state: State
var _iterator: Base.Iterator
}
@airspeedswift
airspeedswift / NonemptyCollection.swift
Created Dec 17, 2016
Non-empty collection in Swift
View NonemptyCollection.swift
protocol NonemptyCollection: Collection {
var first: Iterator.Element { get }
}
enum NonemptyIndex<Base: Collection>: Comparable {
case head
case tail(Base.Index)
static func ==(lhs: NonemptyIndex, rhs: NonemptyIndex) -> Bool {
switch (lhs,rhs) {
@airspeedswift
airspeedswift / protocolextension.swift
Last active May 25, 2019
Protocol vs generic type extensions - overload resolution
View protocolextension.swift
// works for any index type
extension CollectionType {
var mid: Index {
return advance(startIndex,
distance(startIndex,endIndex) / 2
)
}
}
// and specialize for random-access index types
@airspeedswift
airspeedswift / OneSidedRanges.swift
Last active May 25, 2019
One-sided Range operators
View OneSidedRanges.swift
postfix operator ..< { }
prefix operator ..< { }
struct RangeStart<I: ForwardIndexType> { let start: I }
struct RangeEnd<I: ForwardIndexType> { let end: I }
postfix func ..<<I: ForwardIndexType>(lhs: I) -> RangeStart<I>
{ return RangeStart(start: lhs) }
prefix func ..<<I: ForwardIndexType>(rhs: I) -> RangeEnd<I>
View gist:f413beedc42efe76263466263bf80d24
extension Sequence {
func count(where p: (Element)->Bool)->Int {
return reduce(0) { p($1) ? $0 + 1 : $0 }
}
}
let fn: ((Int) -> Bool) -> Int = [1,2,3].count
let n = fn { $0<2 }
print(n) // 1
@airspeedswift
airspeedswift / rbt.swift
Created Jul 22, 2015
Red Black Tree with indirect and pattern matching for Swift 2.0b4
View rbt.swift
enum Color { case R, B }
indirect enum Tree<Element: Comparable> {
case Empty
case Node(Color,Tree<Element>,Element,Tree<Element>)
init() { self = .Empty }
init(_ x: Element, color: Color = .B,
left: Tree<Element> = .Empty, right: Tree<Element> = .Empty)
@airspeedswift
airspeedswift / usort.swift
Created Aug 16, 2018
Unsafe stable mergesort
View usort.swift
extension UnsafeMutableBufferPointer {
public mutating func merge(
using aux: UnsafeMutableBufferPointer<Element>,
_ lo: Int, _ mid: Int, _ hi: Int,
by isOrderedBefore: (Element, Element) -> Bool
) {
assert(hi <= self.endIndex)
assert(self.count == aux.count)
let from = self.baseAddress!, to = aux.baseAddress!
@airspeedswift
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
airspeedswift / list.swift
Last active Jun 18, 2018
indirect enum List as CollectionType
View list.swift
// A simple linked list using enums:
enum List<Element> {
case End
indirect case Node(Element, List<Element>)
func cons(x: Element) -> List<Element> {
return .Node(x, self)
}
}
You can’t perform that action at this time.