Created
May 10, 2020 16:28
-
-
Save ericwindmill/be19f84e60b97b8dc38633070b7863f1 to your computer and use it in GitHub Desktop.
fbe_dart_iterables_any_and_every
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 `any` on user.roles and `every` on Role.values |
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
/// solution | |
class User { | |
final String name; | |
final List<Role> roles; | |
User(this.name, this.roles); | |
} | |
enum Role { | |
engineer, | |
manager, | |
QA, | |
} | |
bool userCanTest(User u) { | |
// find out if any of the user roles match Role.QA | |
} | |
bool isSuperUser(User u) { | |
// find out if the user has every role | |
} |
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
bool userCanTest(User u) { | |
return u.roles.any((Role r) => r == Role.QA); | |
} | |
bool isSuperUser(User u) { | |
return Role.values.every((Role r) => u.roles.contains(r)); | |
} |
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
void main() { | |
var user1 = User("Sarah", [Role.engineer, Role.QA]); | |
var user2 = User("Ali", [Role.QA, Role.engineer, Role.manager]); | |
final user1CanTest = userCanTest(user1); | |
final user2HasEvery = isSuperUser(user2); | |
if (!user1CanTest) _result(false, ["canTestUser didn't return true"]); | |
if (!user2HasEvery) _result(false, ["isSuperUser didn't return true"]); | |
_result(true, ["Good work, fam"]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment