Skip to content

Instantly share code, notes, and snippets.

@jangjunha
Created September 10, 2021 16:24
Show Gist options
  • Save jangjunha/5a05332d169234da1c1bccd2a14b7869 to your computer and use it in GitHub Desktop.
Save jangjunha/5a05332d169234da1c1bccd2a14b7869 to your computer and use it in GitHub Desktop.
Date+Strideable and laziness of stride test
import Foundation
extension Date: Strideable {
public typealias Stride = TimeInterval
public func distance(to other: Date) -> Stride {
return other.timeIntervalSince(self)
}
public func advanced(by n: Stride) -> Date {
return self.addingTimeInterval(n)
}
}
let d0 = Date(timeIntervalSince1970: 0)
let d10 = Date(timeIntervalSince1970: 10)
print(d0.timeIntervalSince(d10))
print(0.distance(to: 10))
let t1 = stride(from: d0, through: d10, by: 2)
let t2 = t1.lazy
let t3: LazyMapSequence<StrideThrough<Date>, Date> = t2.map { elem -> Date in
print("mapTo: \(elem)")
return elem.addingTimeInterval(1)
}
for i in t3.prefix(2) {
print("access: \(i)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment