Skip to content

Instantly share code, notes, and snippets.

View airspeedswift's full-sized avatar

Ben Cohen airspeedswift

View GitHub Profile
@airspeedswift
airspeedswift / list.swift
Last active November 3, 2019 10:33
indirect enum List as CollectionType
// 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)
}
}
@airspeedswift
airspeedswift / rbt.swift
Created July 22, 2015 12:40
Red Black Tree with indirect and pattern matching for Swift 2.0b4
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)
struct LazilyFlattenedRandomAccessCollection<C: CollectionType where C.Index: RandomAccessIndexType> {
let collections: [C]
var count: Int {
return collections.reduce(0) { (total: Int, next: C) -> Int in
total + numericCast(next.count)
}
}
@airspeedswift
airspeedswift / shuffle.swift
Last active February 2, 2018 20:46 — forked from natecook1000/shuffle.swift
Fisher-Yates shuffle as protocol extension on any random-access collection
// 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)
@airspeedswift
airspeedswift / tostring.swift
Last active August 29, 2015 14:23
toString vs String.init
/*:
You used to be able to do this:
[anything].map(toString)
but then String() replaced toString,
which meant you had to write
[anything].map { String($0) }
@airspeedswift
airspeedswift / homogeneousnessedness.swift
Last active August 29, 2015 14:23
Swift-only homogeneousnessedness
extension SequenceType where Generator.Element: Equatable {
/// Checks every element in a sequence is equal to a given element
func all(element: Generator.Element) -> Bool {
return !contains { $0 != element }
}
/// Checks no element in a sequence is equal to a given element
func none(element: Generator.Element) -> Bool {
return !contains(element)
}
@airspeedswift
airspeedswift / SubSliceView.swift
Created June 27, 2015 21:42
Anything can be Sliceable
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
airspeedswift / protocolextension.swift
Last active May 25, 2019 07:38
Protocol vs generic type extensions - overload resolution
// 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 / mergesort.swift
Created June 25, 2015 20:43
Sliceable mergesort extension
extension Range where T: RandomAccessIndexType {
var mid: T {
return startIndex.advancedBy(
startIndex.distanceTo(endIndex) / 2
)
}
}
extension Sliceable
@airspeedswift
airspeedswift / mid.swift
Created June 25, 2015 20:09
Range midpoint
extension Range where T: RandomAccessIndexType {
var mid: T {
return startIndex.advancedBy(
startIndex.distanceTo(endIndex) / 2
)
}
}