ios - Swift : How to refactor my code using more elegant approach? -


i wrote piece of code creates array of objects based on array of dictionaries. problem solution, it's written in noob way using multiple loops imperatively.

let's consider following case. there class called "drug"

class drug {  var name:string var dosage:[(dose:string,time:string)]  init(name:string){      self.name = name     self.dosage = []     } } 

i obtain data array of dictionaries, here example:

var array = [["drugname":"amotaks", "time":"17.00", "dose":"5"], ["drugname":"amotaks", "time":"18.00", "dose":"5"], ["drugname":"amotaks", "time":"18.00", "dose":"5"], ["drugname":"amotaks", "time":"17.00", "dose":"5"], ["drugname":"claritin", "time":"17.00", "dose":"5"], ["drugname":"claritin", "time":"18.00", "dose":"5"]] 

now tricky part, best way turn data array of drug objects? not glad of solution because uses nested loops, additional helper arrays it's inefficient. purpose create objects without repeating names , provide each object dosage array consist of (dose, time) tuples)

var drugarray:[drug] = []     var drugs:[string] = []      in array          {         if contains(drugs, i["drugname"]! string){             continue             }         else{              drugs.append(i["drugname"]!)             drugarray.append(drug(name: i["drugname"]!))              }          }      in drugarray{          in array {             if i["drugname"]! == a.name             {                 a.dosage.append(dose: i["dose"]!, time: i["time"]!)              }          }} 

i believe there better way that, in few lines of code. tried figure out basic fp functions such map or filter end nothing.

my question is: how perform operation coded above in elegant way(probably using more advanced fuctional programming)?

so @ first, should restructure objects little better, easier , more readable, possibly this:

struct dosage {      // declare variables     let dose : string     let time : string      // dosage drug data     init(drugdata : drugdata) {         self.dose = drugdata["dose"]!         self.time = drugdata["time"]!     } }   class drug {      let name : string     var dosage = [dosage]()      init(drugdata : drugdata) {          // assign name         self.name = drugdata["dose"]!          // append dosage         self.appenddosage(drugdata)     }       func appenddosage(drugdata : drugdata) {          self.dosage.append(dosage(drugdata: drugdata))     } } 

notice using typealias masks definition of dictionary, this:

typealias drugdata = [string : string] 

define storage drugs:

var drugs = [string : drug]() 

now when have definitions sorted out, can improve algorithm:

func createdrugs() {      // drug definitions     var array = [["drugname":"amotaks", "time":"17.00", "dose":"5"]]      // iterate through them     drugdefinition in array {          // check if there name, if yes,         if let name = drugdefinition["drugname"] {              // if drug exists, update it, otherwise create             if let drug = drugs[name] {                 drug.appenddosage(drugdefinition)             } else {                 drugs[name] = drug(drugdata: drugdefinition)             }         }     } } 

as can see, used dictionary uses drug name key. searching using key extremely fast, faster iterating through entire array.i believe there many more possibilities improvement, think :) hope helps!


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -