import Foundation
func dateRange(from start: Date, to end: Date) -> [Date] {
let calendar = Calendar.current
return sequence(state: start) { current in
guard current <= end else { return nil }
defer { current = calendar.date(byAdding: .day, value: 1, to: current)! }
return current
}.map { $0 }
// .map on a sequence returns an Array, because the return type of the function forces it.
}
let calendar = Calendar.current
let start = calendar.date(from: DateComponents(year: 2026, month: 1, day: 3))!
let end = calendar.date(from: DateComponents(year: 2026, month: 1, day: 12))!
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let dates = dateRange(from: start, to: end)
print("Generated \(dates.count) dates:")
dates.forEach { print(formatter.string(from: $0)) }
/*
run:
Generated 10 dates:
2026-01-03
2026-01-04
2026-01-05
2026-01-06
2026-01-07
2026-01-08
2026-01-09
2026-01-10
2026-01-11
2026-01-12
*/