0001    // MARK: - Eager
0002    
0003    // MARK: Cartesian Product
0004    
0005    private extension GeneratorType where Element : CollectionType {
0006      mutating private func product
NestedSequences.swift:8
    let xs = product()
NestedSequences.swift:23
    return g.product()
() -> [[Element.Generator.Element]] { 0007 guard let x = next() else { return [[]] } 0008 let xs = product() 0009 return x.flatMap { h in xs.map { [h] + $0 } } 0010 } 0011 } 0012 0013 public extension SequenceType where Generator.Element : SequenceType, Generator.Element : CollectionType { 0014 0015 /// Returns a cartesian product of self 0016 /// ```swift 0017 /// [[1, 2], [3, 4]].product() 0018 /// [[1, 3], [1, 4], [2, 3], [2, 4]] 0019 /// ``` 0020 @warn_unused_result 0021 public func product
NestedSequences.swift:36
    return map(Array.init).product()
NestedSequences.swift:47
  return cols.product()
() -> [[Generator.Element.Generator.Element]] { 0022 var g = generate() 0023 return g.product() 0024 } 0025 } 0026 0027 public extension SequenceType where Generator.Element : SequenceType { 0028 0029 /// Returns a cartesian product of self 0030 /// ```swift 0031 /// [[1, 2], [3, 4]].product() 0032 /// [[1, 3], [1, 4], [2, 3], [2, 4]] 0033 /// ``` 0034 @warn_unused_result 0035 public func product
NestedSequences.swift:57
  return cols.product()
() -> [[Generator.Element.Generator.Element]] { 0036 return map(Array.init).product() 0037 } 0038 } 0039 0040 /// Cartesian product 0041 /// ```swift 0042 /// product([1, 2], [3, 4]) 0043 /// [[1, 3], [1, 4], [2, 3], [2, 4]] 0044 /// ``` 0045 @warn_unused_result 0046 public func product<C : CollectionType>(cols: C...) -> [[C.Generator.Element]] { 0047 return cols.product() 0048 } 0049 0050 /// Cartesian product 0051 /// ```swift 0052 /// product([1, 2], [3, 4]) 0053 /// [[1, 3], [1, 4], [2, 3], [2, 4]] 0054 /// ``` 0055 0056 public func product<S : SequenceType>(cols: S...) -> [[S.Generator.Element]] { 0057 return cols.product() 0058 } 0059 0060 // MARK: Transposition 0061 0062 private extension SequenceType { 0063 private func mapM
NestedSequences.swift:194
    return gens.indices.mapM { i in gens[i].next() }
<U>(@noescape transform: Generator.Element -> U?) -> [U]? { 0064 var result: [U] = [] 0065 result.reserveCapacity(underestimateCount()) 0066 for e in self { 0067 if let r = transform(e) { result.append(r) } else { return nil } 0068 } 0069 return result 0070 } 0071 } 0072 0073 public extension SequenceType where Generator.Element : SequenceType { 0074 0075 /// Returns a transposed self 0076 /// ```swift 0077 /// [[1, 2, 3], [1, 2, 3], [1, 2, 3]].transpose() 0078 /// 0079 /// [[1, 1, 1], [2, 2, 2], [3, 3, 3]] 0080 /// ``` 0081 @warn_unused_result 0082 func transpose() -> [[Generator.Element.Generator.Element]] { 0083 return Array(TransposeSeq(seq: self)) 0084 } 0085 } 0086 0087 // MARK: - Lazy 0088 0089 // MARK: Cartesian Product 0090 /// :nodoc: 0091 public struct ProdGen
NestedSequences.swift:127
  public func generate() -> ProdGen<C> {
NestedSequences.swift:128
    return ProdGen( cols: cols )
<C
NestedSequences.swift:93
  private let cols: [C] // Must be collections, not sequences, in order to be multi-pass
NestedSequences.swift:95
  private var gens: [C.Generator]
NestedSequences.swift:96
  private var curr: [C.Generator.Element]
NestedSequences.swift:98
  public mutating func next() -> [C.Generator.Element]? {
NestedSequences.swift:112
  private init(cols: [C]) {
: CollectionType> : GeneratorType { 0092 0093 private let cols
NestedSequences.swift:105
        gens[i] = cols[i].generate()  // reset the generator
NestedSequences.swift:114
    self.cols = cols
NestedSequences.swift:115
    curr = gens.dropLast().indices.map{gens[$0].next()!} + [self.cols.last!.first!]
: [C] // Must be collections, not sequences, in order to be multi-pass 0094 0095 private var gens
NestedSequences.swift:100
    for i in gens.indices.reverse() { // Loop through generators in reverse order, rolling over
NestedSequences.swift:101
      if let n = gens[i].next() {     // if generator isn't finished, just increment that column, return
NestedSequences.swift:105
        gens[i] = cols[i].generate()  // reset the generator
NestedSequences.swift:106
        curr[i] = gens[i].next()!     // set the current column to the first element of the generator
NestedSequences.swift:120
    self.gens = gens
: [C.Generator] 0096 private var curr
NestedSequences.swift:102
        curr[i] = n
NestedSequences.swift:103
        return curr
NestedSequences.swift:106
        curr[i] = gens[i].next()!     // set the current column to the first element of the generator
NestedSequences.swift:115
    curr = gens.dropLast().indices.map{gens[$0].next()!} + [self.cols.last!.first!]
: [C.Generator.Element] 0097 /// :nodoc: 0098 public mutating func next() -> [C.Generator.Element]? { 0099 0100 for i in gens.indices.reverse() { // Loop through generators in reverse order, rolling over 0101 if let n = gens[i].next() { // if generator isn't finished, just increment that column, return 0102 curr[i] = n 0103 return curr 0104 } else { // generator is finished 0105 gens[i] = cols[i].generate() // reset the generator 0106 curr[i] = gens[i].next()! // set the current column to the first element of the generator 0107 } 0108 } 0109 return nil 0110 } 0111 0112 private init
NestedSequences.swift:128
    return ProdGen( cols: cols )
(cols: [C]) { 0113 var gens = cols.map{$0.generate()} 0114 self.cols = cols 0115 curr = gens.dropLast().indices.map{gens[$0].next()!} + [self.cols.last!.first!] 0116 /** 0117 set curr to the first value of each of the generators, except the last: don't 0118 increment this generator, so that the first value returned contains it. 0119 */ 0120 self.gens = gens 0121 } 0122 } 0123 /// :nodoc: 0124 public struct ProdSeq
NestedSequences.swift:144
  (cols: C...) -> ProdSeq<C> {
NestedSequences.swift:145
    return ProdSeq(cols: cols)
NestedSequences.swift:156
  (cols: S...) -> ProdSeq<[S.Generator.Element]> {
NestedSequences.swift:157
    return ProdSeq(cols: cols.map(Array.init))
NestedSequences.swift:169
  func lazyProduct() -> ProdSeq<[Generator.Element.Generator.Element]> {
NestedSequences.swift:170
    return ProdSeq(cols: map(Array.init))
NestedSequences.swift:183
  func lazyProduct() -> ProdSeq<Generator.Element> {
NestedSequences.swift:184
    return ProdSeq(cols: Array(self))
<C
NestedSequences.swift:125
  private let cols: [C]
NestedSequences.swift:127
  public func generate() -> ProdGen<C> {
: CollectionType> : LazySequenceType { 0125 private let cols
NestedSequences.swift:128
    return ProdGen( cols: cols )
NestedSequences.swift:132
    return cols.reduce(1) { (n,c) in n * c.underestimateCount() }
: [C] 0126 /// :nodoc: 0127 public func generate() -> ProdGen<C> { 0128 return ProdGen( cols: cols ) 0129 } 0130 /// :nodoc: 0131 public func underestimateCount() -> Int { 0132 return cols.reduce(1) { (n,c) in n * c.underestimateCount() } 0133 } 0134 } 0135 0136 /// Lazy Cartesian Product 0137 /// ```swift 0138 /// lazyProduct([1, 2], [3, 4]) 0139 /// 0140 /// [1, 3], [1, 4], [2, 3], [2, 4] 0141 /// ``` 0142 @warn_unused_result 0143 public func lazyProduct<C : CollectionType> 0144 (cols: C...) -> ProdSeq<C> { 0145 return ProdSeq(cols: cols) 0146 } 0147 0148 /// Lazy Cartesian Product 0149 /// ```swift 0150 /// lazyProduct([1, 2], [3, 4]) 0151 /// 0152 /// [1, 3], [1, 4], [2, 3], [2, 4] 0153 /// ``` 0154 @warn_unused_result 0155 public func lazyProduct<S : SequenceType> 0156 (cols: S...) -> ProdSeq<[S.Generator.Element]> { 0157 return ProdSeq(cols: cols.map(Array.init)) 0158 } 0159 0160 public extension SequenceType where Generator.Element : SequenceType { 0161 0162 /// Lazy Cartesian Product 0163 /// ```swift 0164 /// [[1, 2], [3, 4]].lazyProduct() 0165 /// 0166 /// [1, 3], [1, 4], [2, 3], [2, 4] 0167 /// ``` 0168 @warn_unused_result 0169 func lazyProduct() -> ProdSeq<[Generator.Element.Generator.Element]> { 0170 return ProdSeq(cols: map(Array.init)) 0171 } 0172 } 0173 0174 public extension SequenceType where Generator.Element : CollectionType { 0175 0176 /// Lazy Cartesian Product 0177 /// ```swift 0178 /// [[1, 2], [3, 4]].lazyProduct() 0179 /// 0180 /// [1, 3], [1, 4], [2, 3], [2, 4] 0181 /// ``` 0182 @warn_unused_result 0183 func lazyProduct() -> ProdSeq<Generator.Element> { 0184 return ProdSeq(cols: Array(self)) 0185 } 0186 } 0187 0188 // MARK: Transposition 0189 /// :nodoc: 0190 public struct TransposeGen
NestedSequences.swift:205
    -> TransposeGen<S.Generator.Element.Generator.Element, S.Generator.Element.Generator>{
NestedSequences.swift:206
    return TransposeGen(gens: seq.map { g in g.generate() })
<T
NestedSequences.swift:193
  mutating public func next() -> [T]? {
, G
NestedSequences.swift:191
  private var gens: [G]
: GeneratorType where G.Element == T> : GeneratorType { 0191 private var gens
NestedSequences.swift:194
    return gens.indices.mapM { i in gens[i].next() }
NestedSequences.swift:194
    return gens.indices.mapM { i in gens[i].next() }
: [G] 0192 /// :nodoc: 0193 mutating public func next() -> [T]? { 0194 return gens.indices.mapM { i in gens[i].next() } 0195 } 0196 } 0197 /// :nodoc: 0198 public struct TransposeSeq
NestedSequences.swift:83
    return Array(TransposeSeq(seq: self))
NestedSequences.swift:210
public extension TransposeSeq where S: CollectionType {
NestedSequences.swift:226
  func transpose() -> TransposeSeq<Self> {
NestedSequences.swift:227
    return TransposeSeq(seq: self)
< 0199 S
NestedSequences.swift:202
  private let seq: S
NestedSequences.swift:205
    -> TransposeGen<S.Generator.Element.Generator.Element, S.Generator.Element.Generator>{
NestedSequences.swift:205
    -> TransposeGen<S.Generator.Element.Generator.Element, S.Generator.Element.Generator>{
: SequenceType where 0200 S.Generator.Element : SequenceType 0201 > : LazySequenceType { 0202 private let seq
NestedSequences.swift:206
    return TransposeGen(gens: seq.map { g in g.generate() })
NestedSequences.swift:213
    return seq.first?.underestimateCount() ?? 0
: S 0203 /// :nodoc: 0204 public func generate() 0205 -> TransposeGen<S.Generator.Element.Generator.Element, S.Generator.Element.Generator>{ 0206 return TransposeGen(gens: seq.map { g in g.generate() }) 0207 } 0208 } 0209 0210 public extension TransposeSeq where S: CollectionType { 0211 @warn_unused_result 0212 public func underestimateCount() -> Int { 0213 return seq.first?.underestimateCount() ?? 0 0214 } 0215 } 0216 0217 public extension LazySequenceType where Generator.Element: SequenceType { 0218 0219 /// Returns a lazily transposed self 0220 /// ```swift 0221 /// lazy([[1, 2, 3], [1, 2, 3], [1, 2, 3]]).transpose() 0222 /// 0223 /// [1, 1, 1], [2, 2, 2], [3, 3, 3] 0224 /// ``` 0225 @warn_unused_result 0226 func transpose() -> TransposeSeq<Self> { 0227 return TransposeSeq(seq: self) 0228 } 0229 } 0230