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

Rust: Express the main loop through an iterator #20

Open
wants to merge 2 commits into
base: master
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
26 changes: 12 additions & 14 deletions csa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,22 @@ impl Connection {
}

fn csa_main_loop(timetable: &[Connection], arrival_station: usize, earliest_arrival: &mut [u32], in_connection: &mut [usize]) {
let mut earliest = std::u32::MAX;

for (i, connection) in timetable.iter().enumerate() {
timetable.iter().enumerate().fold(std::u32::MAX, |earliest, (i, connection)| {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using fold in this case is particularly unnatural. The goal of fold is to pass a value throw a stream using a function, not removing a side effect. If you don't use the result of fold, please don't use it.

if connection.departure_timestamp >= earliest_arrival[connection.departure_station] &&
connection.arrival_timestamp < earliest_arrival[connection.arrival_station] {
connection.arrival_timestamp < earliest_arrival[connection.arrival_station] {
earliest_arrival[connection.arrival_station] = connection.arrival_timestamp;
in_connection[connection.arrival_station] = i;
}

if connection.arrival_station == arrival_station && connection.arrival_timestamp < earliest {
earliest = connection.arrival_timestamp;
}
} else if connection.arrival_timestamp > earliest {
break;
if connection.arrival_station == arrival_station && connection.arrival_timestamp < earliest {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is done more than necessary.

connection.arrival_timestamp
} else {
earliest
}
}
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You loose this early break

}

fn csa_print_result(timetable: &Vec<Connection>, in_connection: &[usize], arrival_station: usize) {
fn csa_print_result(timetable: &[Connection], in_connection: &[usize], arrival_station: usize) {
if in_connection[arrival_station] == std::u32::MAX as usize {
println!("NO_SOLUTION");
} else {
Expand All @@ -62,7 +60,7 @@ fn csa_print_result(timetable: &Vec<Connection>, in_connection: &[usize], arriva
println!("");
}

fn csa_compute(timetable: &Vec<Connection>, departure_station: usize, arrival_station: usize, departure_time: u32)
fn csa_compute(timetable: &[Connection], departure_station: usize, arrival_station: usize, departure_time: u32)
{
let mut in_connection = vec!(std::u32::MAX as usize; MAX_STATIONS);
let mut earliest_arrival = vec!(std::u32::MAX; MAX_STATIONS);
Expand All @@ -83,7 +81,7 @@ fn main() {
let timetable = buffered_in.map(|r| { r.ok().expect("failed to read connection line") })
.take_while(|l| { !l.is_empty() })
.map(|l| { Connection::parse(l.trim_right()) })
.collect();
.collect::<Vec<Connection>>();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can .collect::<Vec<_>>();


// Responding to requests from stdin

Expand All @@ -100,6 +98,6 @@ fn main() {
let arrival_station = params[1] as usize;
let departure_time = params[2];

csa_compute(&timetable, departure_station, arrival_station, departure_time);
csa_compute(&timetable[..], departure_station, arrival_station, departure_time);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not needed, no? And using a collect for collecting a Vec<()> is code obfuscation and useless memory allocation. Better to use a for loop, it's not evil.

}).collect::<Vec<_>>();
}