Skip to content

Commit

Permalink
update LocalDate.Until to return iter.Seq
Browse files Browse the repository at this point in the history
  • Loading branch information
its-felix committed Aug 19, 2024
1 parent ac37b30 commit 77da261
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
18 changes: 10 additions & 8 deletions go/common/localdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package common
import (
"cmp"
"encoding/json"
"iter"
"time"
)

Expand Down Expand Up @@ -47,16 +48,17 @@ func (ld LocalDate) Compare(other LocalDate) int {
return ld.Time(nil).Compare(other.Time(nil))
}

func (ld LocalDate) Until(endInclusive LocalDate) []LocalDate {
r := make([]LocalDate, 0)
func (ld LocalDate) Until(endInclusive LocalDate) iter.Seq[LocalDate] {
return func(yield func(LocalDate) bool) {
curr := ld
for curr.Compare(endInclusive) <= 0 {
if !yield(curr) {
break
}

curr := ld
for curr.Compare(endInclusive) <= 0 {
r = append(r, curr)
curr = curr.Next()
curr = curr.Next()
}
}

return r
}

func (ld *LocalDate) UnmarshalJSON(data []byte) error {
Expand Down
4 changes: 1 addition & 3 deletions go/cron/action/convert_flight_numbers.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,10 @@ func (a *cfnAction) Handle(ctx context.Context, params ConvertFlightNumbersParam
}

func (a *cfnAction) convertRange(ctx context.Context, inputBucket, inputPrefix, outputBucket, outputPrefix string, start, end common.LocalDate) error {
for _, curr := range start.Until(end) {
for curr := range start.Until(end) {
if err := a.convertSingle(ctx, inputBucket, inputPrefix, outputBucket, outputPrefix, curr); err != nil {
return err
}

curr = curr.Next()
}

return nil
Expand Down
4 changes: 1 addition & 3 deletions go/cron/action/convert_flight_schedules.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,10 @@ func (a *cfsAction) Handle(ctx context.Context, params ConvertFlightSchedulesPar
}

func (a *cfsAction) convertRange(ctx context.Context, inputBucket, inputPrefix, outputBucket, outputPrefix string, start, end common.LocalDate) error {
for _, curr := range start.Until(end) {
for curr := range start.Until(end) {
if err := a.convertSingle(ctx, inputBucket, inputPrefix, outputBucket, outputPrefix, curr); err != nil {
return err
}

curr = curr.Next()
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion go/cron/action/load_flight_schedules.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (a *lfsAction) Handle(ctx context.Context, params LoadFlightSchedulesParams
defer cancel()

for _, r := range params.DateRanges {
for _, d := range r[0].Until(r[1]) {
for d := range r[0].Until(r[1]) {
if err := a.loadSingle(ctx, params.OutputBucket, params.OutputPrefix, d); err != nil {
return LoadFlightSchedulesOutput{}, err
}
Expand Down

0 comments on commit 77da261

Please sign in to comment.