Skip to content

Instantly share code, notes, and snippets.

View airspeedswift's full-sized avatar

Ben Cohen airspeedswift

View GitHub Profile
@airspeedswift
airspeedswift / MyReverse.swift
Created October 22, 2017 00:02
De-C++ified Rerverse
struct MyReversed<Base: BidirectionalCollection> {
let base: Base
}
extension MyReversed: Collection {
public enum Index: Comparable {
case element(Base.Index)
case end
@airspeedswift
airspeedswift / joke.swift
Created June 6, 2017 16:54
Multiline Literals
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).
"""
print(joke)
@airspeedswift
airspeedswift / lazysplit.swift
Created June 6, 2017 00:17
Lazy split: Swift 3 vs Swift 4
// 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
airspeedswift / dropPrefix.swift
Created February 17, 2017 19:49
dropPrefix()
// this particular API/implementation for demonstration only,
// not necessarily quite what will be proposed
extension Collection where SubSequence == Self {
/// Drop n elements from the front of `self` in-place,
/// returning the dropped prefix.
mutating func dropPrefix(_ n: IndexDistance) -> SubSequence {
// nature of error handling/swallowing/trapping/optional
// returning here TBD...
let newStart = index(startIndex, offsetBy: n)
defer { self = self[newStart..<endIndex] }
@airspeedswift
airspeedswift / NonemptyCollection.swift
Created December 17, 2016 23:15
Non-empty collection in 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 / mergesort.swift
Last active January 3, 2017 05:04
Swift 3 Merge Sort
extension RangeReplaceableCollection
where
// Index: RandomAccessIndexType → Self: RandomAccessCollection
Self: RandomAccessCollection,
// still need this, until generics allow constraints to guarantee
// this is the case for all collections...
SubSequence.Iterator.Element == Iterator.Element {
// mutating in-place operations should be imperative verb phrases,
// so let's rename this stablySort
@airspeedswift
airspeedswift / RBTree.swift
Created October 4, 2015 00:37
red black tree updated for 2.1b2
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
airspeedswift / list.swift
Last active October 31, 2015 18:46
Swift Singly Linked List
private enum ListNode<Element> {
case End
indirect case Node(Element, tag: Int, next: ListNode<Element>)
/// Computed property to fetch the tag. .End has an
/// implicit tag of zero.
var tag: Int {
switch self {
case .End: return 0
case let .Node(_, tag: n, _):
@airspeedswift
airspeedswift / MyArray.swift
Last active October 8, 2022 19:52
Array using ManagedBuffer
private class MyArrayBuffer<Element>: ManagedBuffer<Int,Element> {
func clone() -> MyArrayBuffer<Element> {
return self.withUnsafeMutablePointerToElements { elements -> MyArrayBuffer<Element> in
return MyArrayBuffer<Element>.create(self.allocatedElementCount) { newBuf in
newBuf.withUnsafeMutablePointerToElements { newElems->Void in
newElems.initializeFrom(elements, count: self.value)
}
return self.value
@airspeedswift
airspeedswift / mergesort.swift
Last active January 20, 2017 12:29
Stable Swift merge sort
extension Array where Element: Comparable
{
mutating func mergesortInPlace() {
var tmp: [Generator.Element] = []
tmp.reserveCapacity(numericCast(self.count))
func merge(lo: Int, _ mi: Int, _ hi: Int) {
tmp.removeAll(keepCapacity: true)
tmp.extend(self[lo..<hi])