Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Go code #17

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 24 additions & 42 deletions trogsit/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,38 +44,30 @@ func buildTripResponse(
trips []Trip,
tripsIxByRoute map[string][]int,
) []TripResponse {
tripIxs, ok := tripsIxByRoute[route]

if ok {
resp := make([]TripResponse, 0, len(tripIxs))
for tripIx := range tripIxs {
trip := trips[tripIx]
tripResponse := TripResponse{
TripID: trip.TripID,
ServiceID: trip.ServiceID,
RouteID: trip.RouteID,
}

stopTimeIxs, ok := stopTimesIxByTrip[trip.TripID]
if ok {
tripResponse.Schedules = make([]ScheduleResponse, 0, len(stopTimeIxs))
for stopTimeIx := range stopTimeIxs {
stopTime := stopTimes[stopTimeIx]
tripResponse.Schedules = append(tripResponse.Schedules, ScheduleResponse{
StopID: stopTime.StopID,
Arrival: stopTime.Arrival,
Departure: stopTime.Departure,
})
}
} else {
tripResponse.Schedules = []ScheduleResponse{}
}
resp = append(resp, tripResponse)
tripIxs := tripsIxByRoute[route]
resp := make([]TripResponse, 0, len(tripIxs))
for tripIx := range tripIxs {
trip := trips[tripIx]

stopTimeIxs := stopTimesIxByTrip[trip.TripID]
schedules := make([]ScheduleResponse, 0, len(stopTimeIxs))
for stopTimeIx := range stopTimeIxs {
stopTime := stopTimes[stopTimeIx]
schedules = append(schedules, ScheduleResponse{
StopID: stopTime.StopID,
Arrival: stopTime.Arrival,
Departure: stopTime.Departure,
})
}
return resp
} else {
return []TripResponse{}

resp = append(resp, TripResponse{
TripID: trip.TripID,
ServiceID: trip.ServiceID,
RouteID: trip.RouteID,
Schedules: schedules,
})
}
return resp
}

func main() {
Expand Down Expand Up @@ -124,12 +116,7 @@ func getStopTimes() ([]StopTime, map[string][]int) {
stsByTrip := make(map[string][]int)
for i, rec := range records[1:] {
trip := rec[0]
sts, ok := stsByTrip[trip]
if ok {
stsByTrip[trip] = append(sts, i)
} else {
stsByTrip[trip] = []int{i}
}
stsByTrip[trip] = append(stsByTrip[trip], i)
stopTimes = append(stopTimes, StopTime{TripID: trip, StopID: rec[3], Arrival: rec[1], Departure: rec[2]})
}
end := time.Now()
Expand Down Expand Up @@ -166,12 +153,7 @@ func getTrips() ([]Trip, map[string][]int) {
tripsByRoute := make(map[string][]int)
for i, rec := range records[1:] {
route := rec[0]
ts, ok := tripsByRoute[route]
if ok {
tripsByRoute[route] = append(ts, i)
} else {
tripsByRoute[route] = []int{i}
}
tripsByRoute[route] = append(tripsByRoute[route], i)
trips = append(trips, Trip{TripID: rec[2], RouteID: route, ServiceID: rec[1]})
}
end := time.Now()
Expand Down