Skip to content

Instantly share code, notes, and snippets.

Avatar

Donnacha Oisín Kidney oisdk

View GitHub Profile
View MegaSequences.Swift
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 {}
View multSubscript.Swift
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] {
View everyOf.Swift
func everyOf<T>(var seqs: [[T]]) -> [[T]] {
return seqs.isEmpty ? [[]] :
{ f in everyOf(seqs).flatMap { s in f.map{ s + [$0] }}}( seqs.removeLast() )
}
View RecSeq.Swift
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
View memoize.Swift
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
}
View SeqMakers.Swift
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?
View cst.Swift
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
View PySw.Swift
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
}
}
View Product.Swift
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() {
View uniques.swift
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}()}
}
You can’t perform that action at this time.