Skip to content

Instantly share code, notes, and snippets.

View oisdk's full-sized avatar

Donnacha Oisín Kidney oisdk

View GitHub Profile
public protocol Occupiable {
var isEmpty: Bool { get }
var isNotEmpty: Bool { get }
}
public extension Occupiable {
public var isNotEmpty: Bool { return !isEmpty }
}
extension CollectionType where Self : Occupiable { }
struct WithIndexGenerator<Base : CollectionType> : GeneratorType {
private let base: Base
private var index: Base.Index
mutating func next() -> (Base.Index, Base.Generator.Element)? {
return index == base.endIndex ? nil : (index, base[index++])
}
}
enum Sharp: Int { case C = 0, G, D, A, E, B, F }
func sKeyForNote(n: Sharp) -> [Sharp] {
return (0..<n.rawValue)
.map{ n in (n + 6) % 7 }
.flatMap(Sharp.init)
}
enum Flat: Int { case F = 1, B, E, A, D, G }
let notes = ["A", "A♯", "B", "C", "C♯", "D", "D♯", "E", "F", "F♯", "G", "G♯"]
extension Strideable {
func strides<S : SequenceType where S.Generator.Element == Stride>(ss: S) -> [Self] {
var i = self
return ss.map { s in
defer { i = i.advancedBy(s) }
return i
}
}
protocol NListType : SequenceType {
typealias Element
init()
var ar: [Element] { get }
}
extension NListType {
func generate() -> IndexingGenerator<[Element]> {
return ar.generate()
}
protocol Nat {}
struct Zero : Nat {}
protocol NonZero: Nat { typealias Pred: Nat }
struct Succ<N : Nat> : NonZero { typealias Pred = N }
protocol _AnyTuple : CustomStringConvertible {
var tDesc: String { get }
var count: Int { get }
typealias Arity : Nat
}
func cons<S : SequenceType>(h: S.Generator.Element)(s: S) -> [S.Generator.Element] {
return [h] + s
}
private extension GeneratorType where Element : CollectionType {
mutating private func product() -> [[Element.Generator.Element]] {
return next()?.lazy.map(cons).flatMap(product().lazy.map) ?? [[]]
}
}
protocol Nat { init() }
struct Zero : Nat {}
protocol NonZero: Nat { typealias Pred: Nat }
struct Succ<N : Nat> : NonZero { typealias Pred = N }
typealias One = Succ<Zero>
typealias Two = Succ<One>
typealias Three = Succ<Two>
typealias Four = Succ<Three>
typealias Five = Succ<Four>
public struct HalfOpenStartInterval<Bound : Comparable> : IntervalType, Equatable, CustomStringConvertible, CustomDebugStringConvertible {
public init(_ start: Bound, _ end: Bound) { (self.start, self.end) = (start, end) }
public let start, end: Bound
public var description: String {
return String(reflecting: start) + ">.." + String(reflecting: end)
}
public var debugDescription: String { return description }
@warn_unused_result
public func contains(x: Bound) -> Bool { return x > start && x <= end }
@warn_unused_result
extension CollectionType where Index : BidirectionalIndexType {
private func lastIndexOfNot(@noescape isNotElement: Generator.Element throws -> Bool) rethrows -> Index? {
for i in indices.reverse()
where (try !isNotElement(self[i])) {
return i
}
return nil
}
}