This file contains 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 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 |
This file contains 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
// 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 [] | |
} | |
} | |
} |
This file contains 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 enum Queue<Element> { | |
case Nil | |
indirect case Cons(head: Element, tail: Queue<Element>) | |
} | |
infix operator |> { | |
associativity right | |
precedence 100 | |
} |
This file contains 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 enum List<Element> { | |
case Nil | |
indirect case Cons(head: Element, tail: List<Element>) | |
} | |
infix operator |> { | |
associativity right | |
precedence 100 | |
} |
This file contains 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 ContiguousList<Element> { | |
private var revContents: [Element] | |
} | |
struct ContiguousListIndex { | |
private let revd: Int | |
} | |
extension ContiguousListIndex : Equatable, ForwardIndexType { | |
func successor() -> ContiguousListIndex { |
This file contains 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 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) |
This file contains 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 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> |
This file contains 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 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 { |
This file contains 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 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() { |
This file contains 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 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]) | |
} |