Skip to content

Instantly share code, notes, and snippets.

@eito
Created October 7, 2014 06:28
Show Gist options
  • Save eito/34e5a24e5251d83f5907 to your computer and use it in GitHub Desktop.
Save eito/34e5a24e5251d83f5907 to your computer and use it in GitHub Desktop.
functional programming
// Functional Programming exercises.
//
// Rules:
// 1. No loops (for, while, do/while)!
// 2. No if, else, or switch statements.
// 3. No "var" variables - only "let".
//
// Functions to use:
// map, reduce, filter
//
// String functions that might be helpful:
// contains("Some String", "g") --> returns true if "Some String" contains the character "g"
// "Some String".hasSuffix("foo") --> returns true if "Some String" ends with "foo"
// Create some test data for our purposes
struct Person {
let name: String
let emailAddresses: [String]
}
let john = Person(name: "John Smith", emailAddresses: ["jsmith@gmail.com", "john@smith.org"])
let bob = Person(name: "Bob Jones", emailAddresses: ["bob@jones.org"])
let fred = Person(name: "Fred Barnes", emailAddresses: ["fbarnes@gmail.com", "fred@yahoo.com", "barnes@work.com"])
let people = [john, bob, fred]
// ---------------------------------------------------------------------------------------------
// Exercise 1: Create a list of the peoples' names.
let names: [String] = people.map { return $0.name}
// Upon completion of exercise 1, this should evaluate to "true".
names == ["John Smith", "Bob Jones", "Fred Barnes"]
// ---------------------------------------------------------------------------------------------
// Exercise 2: Create a "flattened" list containing all email addresses of all people.
// NOTE: This should be a [String], not a [ [String] ].
let emailAddressArray: [[String]] = people.map { return $0.emailAddresses }
let emails: [String] = emailAddressArray.reduce([]){
return $0 + $1
}// YOU FILL THIS IN
// Upon completion of exercise 2, this should evaluate to "true".
let expectEmails = ["jsmith@gmail.com", "john@smith.org",
"bob@jones.org",
"fbarnes@gmail.com", "fred@yahoo.com", "barnes@work.com"]
emails == expectEmails
// ---------------------------------------------------------------------------------------------
// Exercise 3: Create a list of all people who have the letter "J" somewhere in their name.
let peopleWithJ: [Person] = people.filter {
return contains($0.name, "J")
}// YOU FILL THIS IN
// Upon completion of exercise 3, these should evaluate to "true".
peopleWithJ.count == 2
peopleWithJ[0].name == "John Smith"
peopleWithJ[1].name == "Bob Jones"
// ---------------------------------------------------------------------------------------------
// Silver Challenge: Create a list of all people who have an @gmail.com email address.
let expectGmailPeople = [john, fred]
let gmailPeople: [Person] = expectGmailPeople.filter {
return countElements($0.emailAddresses.filter {
return $0.hasSuffix("@gmail.com")
}) > 0
}// YOU FILL THIS IN
gmailPeople.count == 2
gmailPeople[0].name == "John Smith"
gmailPeople[1].name == "Fred Barnes"
// ---------------------------------------------------------------------------------------------
// Gold Challenge: Solve Exercise 2 again, this time without using reduce.
// Hint: Look up the "join" function.
let emailStrings: [String] = people.map {
join(",", $0.emailAddresses)
}
let emailStrings2: String = join(",", emailStrings)
let goldChallengeEmails: [String] = split(emailStrings2, {$0 == ","}, maxSplit: Int.max, allowEmptySlices: false)
goldChallengeEmails == expectEmails
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment