Skip to content

Instantly share code, notes, and snippets.

Avatar

Donnacha Oisín Kidney oisdk

View GitHub Profile
View HashableSequence.swift
extension SequenceType where Generator.Element: Hashable {
var uniques: [Generator.Element] {
var past = Set<Generator.Element>()
return self.filter {
element in
if past.contains(element) {
return false
View multiMaps.Swift
extension SequenceType where Generator.Element: SequenceType {
var transposeAndPad: [[Generator.Element.Generator.Element]]? {
var genGen = self.generate()
let first = genGen.next()
var gens = AnySequence{genGen}.map{$0.generate()}
return first.map { $0.map { leftEl in [leftEl] + gens.indices.map{gens[$0].next() ?? leftEl} }
}
}
var transpose: [[Generator.Element.Generator.Element]] {
View digits.Swift
extension IntegerType {
func digits() -> [Self] {
var runner = self
return anyGenerator {
defer { runner /= 10 }
return runner == 0 ? nil : runner % 10
}.reverse()
}
func digits(base: Self) -> [Self] {
var runner = self
View everyOf.swift
extension SequenceType where Generator.Element: SequenceType {
var everyOf: [[Generator.Element.Generator.Element]] {
var seqGen = self.generate()
return seqGen.next().map {
let fSeq = Array($0)
return AnySequence{seqGen}.everyOf.flatMap {
lSeq in fSeq.map{ [$0] + lSeq }
}
} ?? [[]]
}
View levDis.swift
func levDis(w1: String, w2: String) -> Int {
let (t, s) = (w1.characters, w2.characters)
let empty = Repeat(count: s.count, repeatedValue: 0)
var mat = [[Int](0...s.count)] + (1...t.count).map{[$0.0] + empty}
for (i, tLett) in t.enumerate() {
for (j, sLett) in s.enumerate() {
mat[i + 1][j + 1] = tLett == sLett ?
View searchWhenSorted.swift
extension CollectionType where Generator.Element: Comparable {
func searchWhenSorted(toFind: Generator.Element) -> Self.Index? {
return self.order.flatMap {
(order: ((Generator.Element, Generator.Element) -> Bool)) -> Index? in
for (index, element) in zip(self.indices, self) {
if element == toFind {
return index
} else if order(toFind, element) {
return nil
}
View transpose.Swift
extension SequenceType where Generator.Element: SequenceType {
func transposeAndPad() -> [[Generator.Element.Generator.Element]]? {
var g = self.generate()
return g.next().map {
var gens = GeneratorSequence(g).map{$0.generate()}
return $0.map { fEl in [fEl] + gens.indices.map{gens[$0].next() ?? fEl}}
}
}
}
View subStrings.Swift
extension Sliceable where Generator.Element : Equatable, Index.Distance == Int {
func lComSubStr<
S: Sliceable where
S.Generator.Element == Generator.Element,
S.Index.Distance == Int
>(with: S) -> SubSlice {
var (len, end) = (0, 0)
let empty = Array(Repeat(count: with.count + 1, repeatedValue: 0))
View Lazy.Swift
protocol Lazy : SequenceType {}
extension LazySequence : Lazy {}
extension LazyBidirectionalCollection : Lazy {}
extension LazyForwardCollection : Lazy {}
extension LazyRandomAccessCollection : Lazy {}
extension Lazy {
var array: [Generator.Element] {
return Array(self)
View PaddedZip.Swift
struct PaddedZipGenerator<G0: GeneratorType, G1: GeneratorType> : GeneratorType {
typealias E0 = G0.Element
typealias E1 = G1.Element
typealias Element = (E0?, E1?)
private var (g0, g1): (G0?, G1?)
mutating func next() -> PaddedZipGenerator.Element? {
You can’t perform that action at this time.