Skip to content

Instantly share code, notes, and snippets.

@ericwindmill
Created May 10, 2020 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericwindmill/1bfc7ca4f97d2bce611f1bd612b8a456 to your computer and use it in GitHub Desktop.
Save ericwindmill/1bfc7ca4f97d2bce611f1bd612b8a456 to your computer and use it in GitHub Desktop.
Flutter By Example exercises, Dart, Iterables, Fold
Try using 'fold' to derive a value of a different type.
// Do not edit this class
class User {
final String name;
final int money;
User(this.name, this.money);
}
int combineFriendsMoney(List<User> friends) {
// try to find the amount of money several friends have combined
// don't use a loop, use a List method!
}
int combineFriendsMoney(List<User> friends) {
return friends.fold(0, (int newTotal, User friend) {
return newTotal + friend.money;
});
}
final scott = User('Scott', 10);
final ellen = User('Ellen', 12);
final morgen = User('Morgen', 15);
final phoebe = User('Phoebe', 9);
final friends = [scott, ellen, morgen, phoebe];
void main() {
final friendsMoneyCombined = combineFriendsMoney(friends);
if (friendsMoneyCombined != 46) {
_result(false, ["amount expected from combineFriendsMoney was wrong"]);
}
_result(true, ['All Tests Passed woo woo']);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment