- 
      
 - 
        
Save kwalrath/0be68487b124b4e55927c3e026094547 to your computer and use it in GitHub Desktop.  
  
    
      This file contains hidden or 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
    
  
  
    
  | Use the methods `map()`, `any()`, and `where()` to solve the exercise. | 
  
    
      This file contains hidden or 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
    
  
  
    
  | Iterable<EmailAddress> parseEmailAddresses(Iterable<String> strings) { | |
| // Implement this method | |
| } | |
| bool anyInvalidEmailAddress(Iterable<EmailAddress> emails) { | |
| // Implement this method | |
| } | |
| Iterable<EmailAddress> validEmailAddresses(Iterable<EmailAddress> emails) { | |
| // Implement this method | |
| } | |
| class EmailAddress { | |
| String address; | |
| EmailAddress(this.address); | |
| @override | |
| bool operator ==(Object other) => | |
| identical(this, other) || | |
| other is EmailAddress && | |
| runtimeType == other.runtimeType && | |
| address == other.address; | |
| @override | |
| int get hashCode => address.hashCode; | |
| @override | |
| String toString() { | |
| return 'EmailAddress{address: $address}'; | |
| } | |
| } | 
  
    
      This file contains hidden or 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
    
  
  
    
  | Iterable<EmailAddress> parseEmailAddresses(Iterable<String> strings) { | |
| return strings.map((s) => EmailAddress(s)); | |
| } | |
| bool anyInvalidEmailAddress(Iterable<EmailAddress> emails) { | |
| return emails.any((email) => !isValidEmailAddress(email)); | |
| } | |
| Iterable<EmailAddress> validEmailAddresses(Iterable<EmailAddress> emails) { | |
| return emails.where((email) => isValidEmailAddress(email)); | |
| } | |
| class EmailAddress { | |
| String address; | |
| EmailAddress(this.address); | |
| @override | |
| bool operator ==(Object other) => | |
| identical(this, other) || | |
| other is EmailAddress && | |
| runtimeType == other.runtimeType && | |
| address == other.address; | |
| @override | |
| int get hashCode => address.hashCode; | |
| @override | |
| String toString() { | |
| return 'EmailAddress{address: $address}'; | |
| } | |
| } | 
  
    
      This file contains hidden or 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
    
  
  
    
  | var input = [ | |
| '[email protected]', | |
| 'bobgmail.com', | |
| '[email protected]', | |
| ]; | |
| bool isValidEmailAddress(EmailAddress email) { | |
| return email.address.contains('@'); | |
| } | |
| void main() { | |
| Iterable<EmailAddress> emails; | |
| try { | |
| emails = parseEmailAddresses(input); | |
| if (emails == null) { | |
| _result(false, [ | |
| 'Tried running `parseEmailAddresses`, but received a null value. Did you implement the method?' | |
| ]); | |
| return; | |
| } | |
| if (emails.isEmpty) { | |
| _result(false, [ | |
| 'Tried running `parseEmailAddresses`, but received an empty list.' | |
| ]); | |
| return; | |
| } | |
| if (!_listEquals(emails.toList(), [ | |
| EmailAddress('[email protected]'), | |
| EmailAddress('bobgmail.com'), | |
| EmailAddress('[email protected]'), | |
| ])) { | |
| _result(false, ['Looks like `parseEmailAddresses` is wrong. Keep trying!']); | |
| return; | |
| } | |
| } catch (e) { | |
| _result(false, [ | |
| 'Tried running `parseEmailAddresses`, but received an exception: $e' | |
| ]); | |
| return; | |
| } | |
| try { | |
| final out = anyInvalidEmailAddress(emails); | |
| if (out == null) { | |
| _result(false, [ | |
| 'Tried running `anyInvalidEmailAddress`, but received a null value. Did you implement the method?' | |
| ]); | |
| return; | |
| } | |
| if (!out) { | |
| _result(false, [ | |
| 'Looks like `anyInvalidEmailAddress` is wrong. Keep trying! There is at least one invalid address.' | |
| ]); | |
| return; | |
| } | |
| } catch (e) { | |
| _result(false, [ | |
| 'Tried running `anyInvalidEmailAddress`, but received an exception: $e' | |
| ]); | |
| return; | |
| } | |
| try { | |
| final valid = validEmailAddresses(emails); | |
| if (valid == null) { | |
| _result(false, [ | |
| 'Tried running `validEmailAddresses`, but received a null value. Did you implement the method?' | |
| ]); | |
| return; | |
| } | |
| if (emails.isEmpty) { | |
| _result(false, [ | |
| 'Tried running `validEmailAddresses`, but received an empty list.' | |
| ]); | |
| return; | |
| } | |
| if (!_listEquals(valid.toList(), [ | |
| EmailAddress('[email protected]'), | |
| EmailAddress('[email protected]'), | |
| ])) { | |
| _result(false, ['Looks like `validEmailAddresses` is wrong. Keep trying!']); | |
| return; | |
| } | |
| } catch (e) { | |
| _result(false, [ | |
| 'Tried running the `validEmailAddresses`, but received an exception: $e' | |
| ]); | |
| return; | |
| } | |
| _result(true); | |
| } | |
| bool _listEquals<T>(List<T> a, List<T> b) { | |
| if (a == null) return b == null; | |
| if (b == null || a.length != b.length) return false; | |
| for (int index = 0; index < a.length; index += 1) { | |
| if (a[index] != b[index]) return false; | |
| } | |
| return true; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
Updated in https://gist.github.com/miquelbeltran/bea254b823dd449a0610115405c3c5f7