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 BuildGen<T> : GeneratorType { | |
typealias Element = T | |
private var cur: T | |
private let inc: T -> T | |
public mutating func next() -> Element? { | |
defer { cur = inc(cur) } | |
return cur | |
} | |
public init(start: T, inc: T -> T) { | |
self.cur = start |
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 CycleGen<C: CollectionType> : GeneratorType, LazySequenceType { | |
typealias Element = C.Generator.Element | |
typealias Generator = CycleGen<C> | |
private let inner: C | |
private var innerGen: C.Generator | |
private init(col: C) { | |
self.inner = col |
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 extension SequenceType { | |
func reduce(@noescape combine: (Generator.Element, Generator.Element) -> Generator.Element) -> Generator.Element? { | |
var g = self.generate() | |
return g.next().map { | |
(var accu) in | |
while let next = g.next() { accu = combine(accu, next) } | |
return accu | |
} | |
} | |
} |
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 Token { | |
public let token, content: String | |
public let column, line : Int | |
public init(token: String, content: String, column: Int, line: Int) { | |
(self.token, self.content, self.column, self.line) = (token, content, column, line) | |
} | |
} |
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 OrderedDict<K : Hashable, T> : | |
CollectionType, | |
DictionaryLiteralConvertible, | |
CustomStringConvertible { | |
private var keys: [K] | |
private var vals: [K : T] | |
public var endIndex: Int { return keys.endIndex } | |
public var startIndex: Int { return 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
class SomeClass { | |
var a, b: Int | |
init(a: Int, b: Int) { | |
(self.a, self.b) = (a, b) | |
} | |
} | |
struct SomeStruct { | |
var a, b: Int | |
} |
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 SelectGenerator<T> : GeneratorType { | |
private let vals: [T] | |
private var i: Int | |
mutating public func next() -> (T, [T])? { | |
guard ++i < vals.endIndex else { return nil } | |
var ar = vals | |
return (ar.removeAtIndex(i), ar) | |
} |
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 class SelectGenerator<T> : GeneratorType { | |
private let (head, tail): (T, [T]) | |
private var first: Bool = true | |
private var gen: SelectGenerator<T>? | |
public func next() -> (T, [T])? { | |
return first ? { | |
first = false | |
if !tail.isEmpty { gen = SelectGenerator(vals: tail) } |
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 Array { | |
func splitEvery(nInEach: Int) | |
-> LazySequence<MapSequenceView<StrideTo<Int>, ArraySlice<T>>> { | |
return lazy(stride(from: 0, to: self.count, by: nInEach)).map { | |
from in self[from ..< advance(from, nInEach, self.count)] | |
} | |
} | |
} |
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 class SelectGenerator<T> : GeneratorType { | |
private var head: T? | |
private var tail: [T] = [] | |
private var first: Bool = true | |
private var gen: SelectGenerator<T>? | |
public func next() -> (T, [T])? { | |
return first ? { | |
first = false |