Created
September 10, 2021 16:24
-
-
Save jangjunha/5a05332d169234da1c1bccd2a14b7869 to your computer and use it in GitHub Desktop.
Date+Strideable and laziness of stride test
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 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) | |
} | |
} |
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
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