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 protocol LazySequenceType : SequenceType {} | |
extension LazySequence : LazySequenceType {} | |
extension LazyForwardCollection : LazySequenceType {} | |
extension LazyBidirectionalCollection : LazySequenceType {} | |
extension LazyRandomAccessCollection : LazySequenceType {} | |
extension FilterSequenceView : LazySequenceType {} | |
extension FilterCollectionView : LazySequenceType {} | |
extension MapSequenceView : LazySequenceType {} | |
extension MapCollectionView : LazySequenceType {} |
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 CollectionType { | |
subscript (inds: [Index]) -> [Generator.Element] { return inds.map{self[$0]} } | |
subscript (inds: Index...) -> [Generator.Element] { return inds.map{self[$0]} } | |
} | |
extension MutableCollectionType { | |
subscript (inds: [Index]) -> [Generator.Element] { | |
get { return inds.map{self[$0]} } | |
set(els) { for (ind, val) in zip(inds, els) { self[ind] = val } } | |
} | |
subscript (inds: Index...) -> [Generator.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
func everyOf<T>(var seqs: [[T]]) -> [[T]] { | |
return seqs.isEmpty ? [[]] : | |
{ f in everyOf(seqs).flatMap { s in f.map{ s + [$0] }}}( seqs.removeLast() ) | |
} |
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
func memoize<A : Hashable, T>(var base: [A:T], recFunc: ((A -> T), A) -> T) -> (A -> T) { | |
func f(n: A) -> T { | |
if let r = base[n] { return r } | |
let r = recFunc(f, n) | |
base[n] = r | |
return r | |
} | |
return f | |
} |
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 LayerGen<T> : GeneratorType { | |
typealias Element = T | |
private let proc: T -> T | |
private var prev: T | |
mutating func next() -> Element? { | |
prev = proc(prev) | |
return prev |
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 LayerGen<T> : GeneratorType { | |
typealias Element = T | |
private let proc: T -> T | |
private var prev: T | |
mutating func next() -> Element? { | |
prev = proc(prev) | |
return prev |
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 SeqInterp : SequenceType {} | |
struct ContGen<G : GeneratorType where G.Element : Strideable> : GeneratorType { | |
typealias Element = G.Element | |
private var g: G? | |
private var a: Element? | |
private var b: 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
struct SimpleGen<T> : GeneratorType { | |
typealias Element = T | |
private let inc: T -> T | |
private var cur: T | |
mutating func next() -> Element? { | |
defer { cur = inc(cur) } | |
return cur | |
} | |
} |
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 ProdGen<C : CollectionType> : GeneratorType { | |
typealias Element = [C.Generator.Element] | |
private let seqs: [C] | |
private var gens: [C.Generator] | |
private var cur: Element | |
mutating func next() -> Element? { | |
for i in gens.indices.reverse() { | |
if let n = gens[i].next() { |
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 UniquesGen<G : GeneratorType where G.Element : Hashable> : GeneratorType { | |
typealias Element = G.Element | |
private var prevs: Set<Element> | |
private var g: G | |
public mutating func next() -> Element? { | |
return g.next().flatMap{n in prevs.contains(n) ? next() : {prevs.insert(n); return n}()} | |
} |