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

Feedback #1

Open
wants to merge 7 commits into
base: feedback
Choose a base branch
from
Open

Feedback #1

wants to merge 7 commits into from

Conversation

github-classroom[bot]
Copy link
Contributor

@github-classroom github-classroom bot commented Nov 18, 2022

👋! GitHub Classroom created this pull request as a place for your teacher to leave feedback on your work. It will update automatically. Don’t close or merge this pull request, unless you’re instructed to do so by your teacher.
In this pull request, your teacher can leave comments and feedback on your code. Click the Subscribe button to be notified if that happens.
Click the Files changed or Commits tab to see all of the changes pushed to main since the assignment started. Your teacher can see this too.

Notes for teachers

Use this PR to leave feedback. Here are some tips:

  • Click the Files changed tab to see all of the changes pushed to main since the assignment started. To leave comments on specific lines of code, put your cursor over a line of code and click the blue + (plus sign). To learn more about comments, read “Commenting on a pull request”.
  • Click the Commits tab to see the commits pushed to main. Click a commit to see specific changes.
  • If you turned on autograding, then click the Checks tab to see the results.
  • This page is an overview. It shows commits, line comments, and general comments. You can leave a general comment below.
    For more information about this pull request, read “Leaving assignment feedback in GitHub”.

Subscribed: @samanthabeilman

Comment on lines +29 to +39
let mut tally = 0;
for card in self.cards.iter() {
for other_card in other.cards.iter() {
let result = card.print_fight(other_card);
match result {
FightResult::Win => tally += 1,
FightResult::Loss => tally -= 1,
_ => (),
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

It's a bit funky, but here's another way to solve this that just uses iterators:

let tally = self
    .cards
    .iter()
    .flat_map(|card| {
        other.cards.iter().map(move |other_card| {
            let result = card.print_fight(other_card);
            match result {
                FightResult::Win => 1,
                FightResult::Loss => -1,
                _ => 0,
            }
        })
    })
    .sum();

Where we iterate over the first store's cards, then over the second, and collect +1 or -1 for each battle, then add them up at the end. We use flat_map so that even though we get something that might look like [1,-1,-1], [0, 1, -1]... for each of the cards in the first store, it "flattens" it to just [1, -1, -1, 0, 1, -1], then we can just sum() that together :)

Though I don't think I got far enough in the fall to talk about iterator methods, so this is mostly just extra info /shrug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants