Advertisement
Guest User

Untitled

a guest
Jun 12th, 2017
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. // Functional Programming exercises.
  2. //
  3. // Rules:
  4. // 1. No loops (for, while, do/while)!
  5. // 2. No if, else, or switch statements.
  6. // 3. No "var" variables - only "let".
  7. //
  8. // Functions to use:
  9. // map, reduce, filter
  10. //
  11. // String functions that might be helpful:
  12. // contains("Some String", "g") --> returns true if "Some String" contains the character "g"
  13. // "Some String".hasSuffix("foo") --> returns true if "Some String" ends with "foo"
  14.  
  15. // Create some test data for our purposes
  16. struct Person {
  17. let name: String
  18. let emailAddresses: [String]
  19. }
  20.  
  21. let john = Person(name: "John Smith", emailAddresses: ["jsmith@gmail.com", "john@smith.org"])
  22. let bob = Person(name: "Bob Jones", emailAddresses: ["bob@jones.org"])
  23. let fred = Person(name: "Fred Barnes", emailAddresses: ["fbarnes@gmail.com", "fred@yahoo.com", "barnes@work.com"])
  24.  
  25. let people = [john, bob, fred]
  26.  
  27. // ---------------------------------------------------------------------------------------------
  28.  
  29. // Exercise 1: Create a list of the peoples' names.
  30. let names: [String] = people.map { return $0.name}
  31.  
  32. // Upon completion of exercise 1, this should evaluate to "true".
  33. names == ["John Smith", "Bob Jones", "Fred Barnes"]
  34.  
  35. // ---------------------------------------------------------------------------------------------
  36.  
  37. // Exercise 2: Create a "flattened" list containing all email addresses of all people.
  38. // NOTE: This should be a [String], not a [ [String] ].
  39.  
  40. let emailAddressArray: [[String]] = people.map { return $0.emailAddresses }
  41. let emails: [String] = emailAddressArray.reduce([]){
  42. return $0 + $1
  43. }// YOU FILL THIS IN
  44.  
  45. // Upon completion of exercise 2, this should evaluate to "true".
  46. let expectEmails = ["jsmith@gmail.com", "john@smith.org",
  47. "bob@jones.org",
  48. "fbarnes@gmail.com", "fred@yahoo.com", "barnes@work.com"]
  49. emails == expectEmails
  50.  
  51. // ---------------------------------------------------------------------------------------------
  52.  
  53. // Exercise 3: Create a list of all people who have the letter "J" somewhere in their name.
  54. let peopleWithJ: [Person] = people.filter {
  55. return contains($0.name, "J")
  56. }// YOU FILL THIS IN
  57.  
  58. // Upon completion of exercise 3, these should evaluate to "true".
  59. peopleWithJ.count == 2
  60. peopleWithJ[0].name == "John Smith"
  61. peopleWithJ[1].name == "Bob Jones"
  62.  
  63. // ---------------------------------------------------------------------------------------------
  64.  
  65. // Silver Challenge: Create a list of all people who have an @gmail.com email address.
  66. let expectGmailPeople = [john, fred]
  67. let gmailPeople: [Person] = expectGmailPeople.filter {
  68. return countElements($0.emailAddresses.filter {
  69. return $0.hasSuffix("@gmail.com")
  70. }) > 0
  71. }// YOU FILL THIS IN
  72. gmailPeople.count == 2
  73. gmailPeople[0].name == "John Smith"
  74. gmailPeople[1].name == "Fred Barnes"
  75.  
  76. // ---------------------------------------------------------------------------------------------
  77.  
  78. // Gold Challenge: Solve Exercise 2 again, this time without using reduce.
  79. // Hint: Look up the "join" function.
  80. let emailStrings: [String] = people.map {
  81. join(",", $0.emailAddresses)
  82. }
  83. let emailStrings2: String = join(",", emailStrings)
  84. let goldChallengeEmails: [String] = split(emailStrings2, {$0 == ","}, maxSplit: Int.max, allowEmptySlices: false)
  85. goldChallengeEmails == expectEmails
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement