Created
May 10, 2020 17:24
-
-
Save ericwindmill/1bfc7ca4f97d2bce611f1bd612b8a456 to your computer and use it in GitHub Desktop.
Flutter By Example exercises, Dart, Iterables, Fold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Try using 'fold' to derive a value of a different type. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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! | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int combineFriendsMoney(List<User> friends) { | |
return friends.fold(0, (int newTotal, User friend) { | |
return newTotal + friend.money; | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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