This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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++]) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| protocol NListType : SequenceType { | |
| typealias Element | |
| init() | |
| var ar: [Element] { get } | |
| } | |
| extension NListType { | |
| func generate() -> IndexingGenerator<[Element]> { | |
| return ar.generate() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) ?? [[]] | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } | |
| } |