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
let contactsDurationArr = contacts.map({ return $0.duration })//We use the map function to derive an array that contains only the duration property of all the objects | |
var sum = 0 | |
for duration in contactsDurationArr { | |
sum = sum + duration | |
} | |
print(sum) |
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
//Functional Programming | |
let contactsAbove20 = contacts.filter({ $0.age > 20 }) | |
print(contactsAbove20) |
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
//Imperative Programming | |
func filterContacts(aboveAge age: Int) -> [Contact] { | |
var filteredContacts = [Contact]() | |
for contact in contacts { | |
if contact.age > age { | |
filteredContacts.append(contact) | |
} | |
} | |
return filteredContacts | |
} |
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
//Functional Programming | |
let contactNames = contacts.map({ | |
return $0.firstName + " " + $0.lastName | |
}) | |
print(contactNames) |
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
//Get just the name of the contacts into a separate array | |
//Imperative Programming | |
var contactNamesImp = [String]() | |
for contact in contacts { // for each | |
contactNamesImp.append(contact.firstName) | |
} | |
print(contactNamesImp) |
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
let contacts = [ | |
Contact(firstName: "first", lastName: "1", email: "[email protected]", age: 20, courseDuration: 12), | |
Contact(firstName: "Second", lastName: "2", email: "[email protected]", age: 22, courseDuration: 16), | |
Contact(firstName: "Third", lastName: "3", email: "[email protected]", age: 30, courseDuration: 22) | |
] |
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
struct Contact { | |
var firstName: String | |
var lastName: String | |
var email: String | |
var age: Int | |
var duration: Int | |
} |
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
//Functional Programming | |
func sum(start: Int, end: Int, total: Int) -> Int { | |
if (start > end) { | |
return total; | |
} | |
return sum(start: start + 1, end: end, total: total + start) | |
} | |
print("Functional: total: ", sum(start: 1, end: 10, total: 0)); // prints 55 |
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
//Imperative Programming | |
var total = 0 | |
for i in 1...10 { | |
total += i | |
} | |
print("Imperative: total: ", total); // prints 55 |
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
func add(x, y) { | |
return x + y | |
} |