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
airspeedswift / homogeneousnessedness.swift
Last active Aug 29, 2015
Swift-only homogeneousnessedness
View homogeneousnessedness.swift
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 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
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 / mergesort.swift
Created Jun 25, 2015
Sliceable mergesort extension
View mergesort.swift
extension Range where T: RandomAccessIndexType {
var mid: T {
return startIndex.advancedBy(
startIndex.distanceTo(endIndex) / 2
)
}
}
extension Sliceable
View mid.swift
extension Range where T: RandomAccessIndexType {
var mid: T {
return startIndex.advancedBy(
startIndex.distanceTo(endIndex) / 2
)
}
}
@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>
@airspeedswift
airspeedswift / rbt.swift
Last active Aug 29, 2015
Persistent Red/Black Tree in Swift using 2.0 Pattern Matching
View rbt.swift
final class Box<T> {
let unbox: T
init(_ x: T) { self.unbox = x }
}
enum Color { case R, B }
enum Tree { //<T: Comparable> { // something not working when I make this generic
case Empty
@airspeedswift
airspeedswift / subscript.swift
Created Jun 3, 2015
subscript mutation example
View subscript.swift
extension Int {
mutating func increment() {
self = self.successor()
}
}
struct S {
var x: Int = 0
subscript()->Int {
View mergesort.swift
import Darwin
func mid<T: RandomAccessIndexType>(r: Range<T>)->T {
return r.startIndex.advancedBy(
r.startIndex.distanceTo(r.endIndex) / 2
)
}
func merge<T: Comparable>(inout source: [T], lhs: Range<Int>, rhs: Range<Int>) {
var tmp: [T] = []
@airspeedswift
airspeedswift / CSV.swift
Created May 11, 2015
CSV-parsing code in Swift
View CSV.swift
// after http://www.cocoawithlove.com/2009/11/writing-parser-using-nsscanner-csv.html
import Foundation
extension NSScanner {
func scanUpToCharactersFromSet(set: NSCharacterSet) -> String? {
var str: NSString?
return self.scanUpToCharactersFromSet(set, intoString: &str)
? str as? String
: nil
You can’t perform that action at this time.