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 |
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 GeneratorType { | |
mutating func any(predicate: Element -> Bool) -> Bool { | |
return next().map { el in predicate(el) || any(predicate) } ?? false | |
} | |
mutating func all(predicate: Element -> Bool) -> Bool { | |
return next().map { el in predicate(el) && all(predicate) } ?? true | |
} | |
} | |
var i = 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
private extension GeneratorType { | |
mutating func any(@noescape pred: Element -> Bool) -> Bool { | |
return next().map { el in pred(el) || any(pred) } ?? false | |
} | |
mutating func all(@noescape pred: Element -> Bool) -> Bool { | |
return next().map { el in pred(el) && all(pred) } ?? true | |
} | |
} | |
public extension 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
private extension GeneratorType { | |
mutating func foldr<T>(initial: T, @noescape combine: (Element, T) -> T) -> T { | |
return next().map { combine($0, foldr(initial, combine: combine) ) } ?? initial | |
} | |
} | |
public extension SequenceType { | |
func foldr<T>(initial: T, @noescape combine: (Generator.Element, T) -> T) -> T { | |
var g = self.generate() | |
return g.foldr(initial, combine: combine) | |
} |
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
import Foundation | |
extension String { | |
func containsUppercase() -> Bool { | |
return unicodeScalars.contains { | |
NSCharacterSet | |
.uppercaseLetterCharacterSet() | |
.longCharacterIsMember(UInt32($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
import Foundation | |
extension String.UnicodeScalarView.Generator { | |
mutating func hasUpper() -> Bool { | |
return next().map { | |
NSCharacterSet | |
.uppercaseLetterCharacterSet() | |
.longCharacterIsMember(UInt32($0)) | |
|| hasUpper() | |
} ?? false |
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 GeneratorType { | |
mutating func find(pred: Element -> Bool) -> Element? { | |
return next().flatMap { el in pred(el) ? el : find(pred) } | |
} | |
} |
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 FlatMapGen<G : GeneratorType, S : SequenceType> : GeneratorType { | |
private let transform: G.Element -> S | |
private var g : G | |
private var innerGen : S.Generator? | |
public mutating func next() -> S.Generator.Element? { | |
for ; innerGen != nil; innerGen = g.next().map(transform)?.generate() { | |
if let next = innerGen?.next() { | |
return 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
func map<T, U, V>(vals: (T?, U?), transform: (T, U) -> V) -> V? { | |
if let x = vals.0, y = vals.1 { | |
return transform(x, y) | |
} else { | |
return nil | |
} | |
} | |
func map<T, U, V>(val0: T?, _ val1: U?, transform: (T, U) -> V) -> V? { | |
if let x = val0, y = val1 { |
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 ==<T : Equatable>(lhs: T?, rhs: T) -> Bool { | |
return lhs.map{$0 == rhs} ?? false | |
} | |
func ==<T : Equatable>(lhs: T, rhs: T?) -> Bool { | |
return rhs == lhs | |
} | |
func ==<T : Equatable>(lhs: T?, rhs: T?) -> Bool { | |
return lhs.map{$0 == rhs} ?? false | |
} |