Skip to content

Instantly share code, notes, and snippets.

View oisdk's full-sized avatar

Donnacha Oisín Kidney oisdk

View GitHub Profile
enum List<Element> {
case Nil
indirect case Cons(head: Element, tail: List<Element>)
}
struct ListGenerator<Element> : GeneratorType {
private var list: List<Element>
mutating func next() -> Element? {
switch list {
case .Nil: return nil
// You could just order the elements into an array, and then return that array's generator
extension Tree {
func inOrder() -> [Element] {
switch self {
case let .Node(_,l,e,r): return l.inOrder() + [e] + r.inOrder()
case .Empty: return []
}
}
}
public enum Queue<Element> {
case Nil
indirect case Cons(head: Element, tail: Queue<Element>)
}
infix operator |> {
associativity right
precedence 100
}
public enum List<Element> {
case Nil
indirect case Cons(head: Element, tail: List<Element>)
}
infix operator |> {
associativity right
precedence 100
}
struct ContiguousList<Element> {
private var revContents: [Element]
}
struct ContiguousListIndex {
private let revd: Int
}
extension ContiguousListIndex : Equatable, ForwardIndexType {
func successor() -> ContiguousListIndex {
struct RecurrenceRelation<Element>: SequenceType, GeneratorType {
private var prevs: [Element]
private let relat: ([Element], Int) -> Element
private var i: Int = 0
mutating func next() -> Element? {
guard i == prevs.endIndex else { return prevs[i++] }
prevs.append(relat(prevs, i))
prevs.removeAtIndex(0)
public final class AnyGen<Element> : GeneratorType {
private let nextFunc: () -> Element?
public final func next() -> Element? { return nextFunc() }
public init<G : GeneratorType where G.Element == Element>(var _ base: G) {
nextFunc = {base.next()}
}
}
public struct AnySeq<Element> : SequenceType {
private let genFunc: () -> AnyGen<Element>
public struct FilterGenerator<G : GeneratorType> : GeneratorType {
private var generator: G
private let predicate: G.Element -> Bool
public mutating func next() -> G.Element? {
while let next = generator.next() { if predicate(next) { return next } }
return nil
}
}
public struct FilterSequence<S : SequenceType> : SequenceType {
extension Tree : SequenceType {
public func generate() -> AnyGenerator<Element> {
var stack: [Tree<Element>] = []
var curr = self
return anyGenerator {
while case let .Node(_, l, _, _) = curr {
stack.append(curr)
curr = l
}
if case let .Node(_, _, x, r)? = stack.popLast() {
protocol OrderedDictType : Indexable, SequenceType, CollectionType, MutableSliceable, RangeReplaceableCollectionType, CustomDebugStringConvertible {
typealias Value
typealias Container : MutableSliceable, RangeReplaceableCollectionType
typealias SubSequence : SequenceType
typealias Key : Hashable
var contents: [Key:Value] { get set }
var keys: Container { get set }
init(keys: Container, contents: [Key:Value])
}