-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: feedback
Are you sure you want to change the base?
Feedback #1
Conversation
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, | ||
_ => (), | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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
👋! 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:
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”.main
. Click a commit to see specific changes.For more information about this pull request, read “Leaving assignment feedback in GitHub”.
Subscribed: @samanthabeilman