Guest User

Untitled

a guest
Oct 23rd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. //: Playground - noun: a place where people can play
  2. import UIKit
  3.  
  4. /*:
  5. ## `Date` wrapping and convenience methods
  6. */
  7. let calendar = Calendar.current
  8.  
  9. extension Int {
  10. public var days: DateComponents {
  11. return calendarUnit(unit: .day)
  12. }
  13.  
  14. private func calendarUnit(unit: Calendar.Component) -> DateComponents {
  15. var dateComponents = DateComponents()
  16. dateComponents.setValue(self, for: unit)
  17. return dateComponents
  18. }
  19. }
  20.  
  21. extension Date {
  22. private func valueForUnit(unit: Calendar.Component) -> Int {
  23. return calendar.component(unit, from: self)
  24. }
  25.  
  26. var weekday: Int {
  27. return valueForUnit(unit: .weekday)
  28. }
  29. }
  30.  
  31. func + (date: Date, component: DateComponents) -> Date {
  32. if let date = calendar.date(byAdding: component, to: date) {
  33. return date
  34. }
  35. fatalError()
  36. }
  37.  
  38. let date = Date()
  39. let nextDay = date + 3.days
  40.  
  41. /*:
  42. ## Generators and sequences section
  43. */
  44.  
  45. class DateGenerator: IteratorProtocol {
  46. // We are using naive approach here
  47. // It shouldn't be used in real project
  48. var dayOffWeekday: Int = 1 // Sunday
  49. var N: Int = 3
  50.  
  51. private(set) var lessonsCount: Int
  52. private(set) var startDate: Date
  53.  
  54. private var numIterations = 0
  55.  
  56. init(_ start: Date, _ numLessons: Int) {
  57. lessonsCount = numLessons
  58. startDate = start
  59. }
  60.  
  61. func next() -> Date? {
  62. guard numIterations < lessonsCount else {
  63. return nil
  64. }
  65. numIterations += 1
  66.  
  67. var next = startDate + N.days
  68. if next.weekday == dayOffWeekday {
  69. next = next + 1.days
  70. }
  71. startDate = next
  72. return next
  73. }
  74. }
  75.  
  76. let dg = DateGenerator(Date(), 10)
  77. dg.N = 1
  78. dg.dayOffWeekday = 1
  79. while let date = dg.next() {
  80. print(date)
  81. }
  82.  
  83. while let date = dg.next() { // Exhausted
  84. print(date)
  85. }
  86.  
  87. class DateSequece: Sequence {
  88. typealias Element = Date
  89. typealias Iterator = DateGenerator
  90.  
  91. private var lessonsCount: Int
  92. private var startDate: Date
  93.  
  94. init(_ start: Date, _ numLessons: Int){
  95. lessonsCount = numLessons
  96. startDate = start
  97. }
  98.  
  99. func makeIterator() -> Iterator {
  100. return DateGenerator(startDate, lessonsCount)
  101. }
  102. }
  103.  
  104. let sequence = Array(DateSequece(Date(), 10))
  105.  
  106. /*:
  107. ## `SequenceType` section
  108. */
  109.  
  110. class SimpleDateSequece: Sequence {
  111. typealias Element = Date
  112. typealias Iterator = AnyIterator<Element>
  113.  
  114. var lessonsCount: Int
  115. var startDate: Date
  116. var daysStep: Int = 1
  117. private var numIterations = 0
  118.  
  119. init(_ start: Date, _ numLessons: Int, step: Int){
  120. lessonsCount = numLessons
  121. startDate = start
  122. daysStep = step
  123. }
  124.  
  125. func makeIterator() -> AnyIterator<Date> {
  126. return AnyIterator({ () -> Date? in
  127. guard self.numIterations < self.lessonsCount else {
  128. return nil
  129. }
  130. self.numIterations += 1
  131.  
  132. let next = self.startDate + self.daysStep.days
  133. self.startDate = next
  134. return next
  135. })
  136. }
  137. }
  138.  
  139. let simpleSequence = Array(SimpleDateSequece(Date(), 5, step: 4))
  140. /*:
  141. ## Infinite sequences section
  142. */
  143.  
  144. class InfiniteDateGenerator: IteratorProtocol {
  145. var startDate: Date
  146.  
  147. private var numIterations = 0
  148.  
  149. init(_ start: Date) {
  150. startDate = start
  151. }
  152.  
  153. func next() -> Date? {
  154. let next = startDate + 1.days
  155. startDate = next
  156. return next
  157. }
  158. }
  159.  
  160. class InfiniteDateSequece: Sequence {
  161. typealias Element = Date
  162. typealias Iterator = InfiniteDateGenerator
  163.  
  164. private var startDate: Date
  165.  
  166. init(_ start: Date){
  167. startDate = start
  168. }
  169.  
  170. func makeIterator() -> InfiniteDateGenerator {
  171. return InfiniteDateGenerator(startDate)
  172. }
  173. }
  174.  
  175. //let infiniteSequence = Array(InfiniteDateSequece(NSDate()))
  176. let tenFirsElements = Array(InfiniteDateSequece(Date()).prefix(10))
  177.  
  178. let mondayIndex = 2
  179. //let noMondaysSequence = Array(InfiniteDateSequece(NSDate()).filter({$0.weekday != mondayIndex}).prefix(10) ) // Infinite
  180. let lazyNoMondaysSequence = Array(InfiniteDateSequece(Date()).lazy.filter({$0.weekday != mondayIndex}).prefix(10) ) // Finite
Add Comment
Please, Sign In to add comment