Swift L-System Algae -


so i'm doing fun in ibm swift sandbox. did l-system algae in scala , though cool in swift see how language compares.

https://github.com/i2obin/l-system-algorithms-and-fractals/blob/master/algae.scala

that's scala 1 show i'm aiming for, , have in swift;

/**   * created t.hood on 26/01/16   * l-system algae   *   */   import foundation   // mapping function string   func stringmap(x: string) -> string {  var str = x;       // replace characters in string      str = x.stringbyreplacingoccurrencesofstring("a", withstring: "ab") +      str.stringbyreplacingoccurrencesofstring("b", withstring: "a");    // return mapped string;   return str;  }   func lsys() {   // declarations   var iteration : int = 2;  var x = 0;  var lsystem: string = "a";  let chara: character = "a";  let charb: character = "b";   while(x != iteration) {   print(lsystem)    // iterate through characters in string   chars in lsystem.characters {     lsystem = stringmap(lsystem);   }    // inc count ..   x+=1   } }   // run ..  lsys(); 

the problem i'm having in mapping function. need map x, print result, next map on str. problem have operator + between both maps , can't swift print str after first operation.

anyone ideas on how around this? if print str after first replacements think work.

i created project in xcode use debugger see what's going on. here's found.

i moved print statement loop show value of lsystem every time loop executed.

the while loop executes twice, when x == 0 , 1 first time through while loop lsystem == 'a', loop executes once. in stringmap(), 'a' becomes 'aba'. 'aba' printed. second time through while loop, loop gets executed 3 times. loop 1: 'aba' sent stringmap() , 'abbabaaa' returned. loop 2: 'abbabaaa' sent stringmap() , 'abbbabbabababaaaaaaaa' returned. loop 3: 'abbbabbabababaaaaaaaa' sent stringmap() , 'abbbbabbbabbabbabbababababababababaaaaaaaaaaaaaaaaaaaaa' returned.

i modified stringmap() function iterate through string character character , apply grammar each character, appending each change new string returned lsys().

seven iterations of while loop returned this, agrees see in wikipedia article l-system. iteration 0: iteration 1: ab iteration 2: aba iteration 3: abaab iteration 4: abaababa iteration 5: abaababaabaab iteration 6: abaababaabaababaababa iteration 7: abaababaabaababaababaabaababaabaab

here's code. worked in playground on imac, i'd expect work in ibm swift sandbox too.

func stringmap(x: string) -> string {     var returnstring = ""      char in x.characters     {         switch (char)         {         case "a" :             returnstring += "ab"             break         case "b":             returnstring += "a"             break         default:             break         }     }     return returnstring }   func lsys() {      // declarations      let iteration : int = 7;     var x = 0;     var lsystem: string = "a";      print("iteration \(x): \(lsystem)")      while(x != iteration)     {         lsystem = stringmap(lsystem)         x++         print("iteration \(x): \(lsystem)")      } }  lsys() 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -