Skip to content

Instantly share code, notes, and snippets.

@speaktoalvin
Created November 28, 2016 21:50
Show Gist options
  • Save speaktoalvin/f4a6488cd54741117a20b10fa7241d5a to your computer and use it in GitHub Desktop.
Save speaktoalvin/f4a6488cd54741117a20b10fa7241d5a to your computer and use it in GitHub Desktop.
ConvertingAFunction
FROM ------>>>
func createTodayContacts() -> [Contact] {
var allContacts = contactsManager.contacts
allContacts = contactsManager.sortContactsByOlderDateFirst(contacts: allContacts)
var dayContacts = [Contact]()
var neededContacts = settings.numberOfContactsPerDay
var requiredHighPriority = neededContacts * settings.priorityHighPercent / 100
var requiredMediumPriority = neededContacts * settings.priorityMediumPercent / 100
var requiredLowPriority = neededContacts * settings.priorityLowPercent / 100
while (dayContacts.count < settings.numberOfContactsPerDay && allContacts.count > 0)
{
//add high priority contacts
let highPriorityContacts : [Contact] = getPriorityContacts(withPriority: .high, requiredNumber: requiredHighPriority, fromContacts: allContacts)
for contact in highPriorityContacts {
dayContacts.append(contact)
allContacts.remove(at: allContacts.index(where: {$0.id == contact.id})!)
}
//add medium priority contacts
let mediumPriorityContacts : [Contact] = getPriorityContacts(withPriority: .medium, requiredNumber: requiredMediumPriority, fromContacts: allContacts)
for contact in mediumPriorityContacts {
dayContacts.append(contact)
allContacts.remove(at: allContacts.index(where: {$0.id == contact.id})!)
}
//add low priority contacts
let lowPriorityContacts : [Contact] = getPriorityContacts(withPriority: .low, requiredNumber: requiredLowPriority, fromContacts: allContacts)
for contact in lowPriorityContacts {
dayContacts.append(contact)
allContacts.remove(at: allContacts.index(where: {$0.id == contact.id})!)
}
//recalculate how many contacts do we need now
neededContacts = neededContacts - dayContacts.count
//decrease by half needed by category
requiredHighPriority = (Int) (requiredHighPriority / 2) + 1
requiredMediumPriority = (Int) (requiredHighPriority / 2) + 1
requiredLowPriority = (Int) (requiredHighPriority / 2) + 1
}
return dayContacts
}
To ----->>>>>>>>>>
func createTodayContacts() -> [Contact] {
// Day contacts
var dayContacts = [Contact]()
// All Contacts
var allContacts = contactsManager.contacts
allContacts = contactsManager.sortContactsByOlderDateFirst(contacts: allContacts)
var neededContacts = settings.numberOfContactsPerDay
// Priority
var priority = (high : neededContacts * settings.priorityHighPercent / 100, medium : neededContacts * settings.priorityMediumPercent / 100, low : neededContacts * settings.priorityLowPercent / 100)
// Inline Function
func addContacts(priority : Priority, requiredPriority : Int){
let priorityContacts = getPriorityContacts(withPriority: priority, requiredNumber: requiredPriority, fromContacts: allContacts)
for contact in priorityContacts {
dayContacts.append(contact)
allContacts.remove(at: allContacts.index(where: {$0.id == contact.id})!)
}
}
while (dayContacts.count < settings.numberOfContactsPerDay && allContacts.count > 0)
{
// Add priority contacts
addContacts(priority: .high, requiredPriority: priority.high)
addContacts(priority: .medium, requiredPriority: priority.medium)
addContacts(priority: .low, requiredPriority: priority.low)
// Recalculate how many contacts do we need now
neededContacts = neededContacts - dayContacts.count
// Decrease by half needed by category
priority.high = (Int) (priority.high / 2) + 1
priority.medium = (Int) (priority.medium / 2) + 1
priority.low = (Int) (priority.low / 2) + 1
}
return dayContacts
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment