
'use strict'
/*

productions are in the form:
productions:
[
	['A', 'B++'],
	['B', 'BBA-B']
]

which then get transformed to a map via
this.productions = new Map(production)

or you can use a map directly



var kochkurve = new lsys({
	word:'A',
	productions: [['A', 'A+BB'], ['B', 'BA--']]
})


*/


class LSystem {
	constructor({word, productions}) {
			this.word = word
			this.productions = new Map(productions)
			this.iterations = 0
			this.generations = this.generations()
	}

	*generations(){
		while(++this.iterations) {
			let newWord = ''
			for (let literal of this.word) {
				if(this.productions.has(literal)){
					newWord += this.productions.get(literal)
				} else {
					newWord += literal
				}
			}

			this.word = newWord
			yield this.word
		}
	}

	// just a shortcut to be able to use an instance of LSystem like a generator
	next(argument){
		return this.generations.next(argument)
	}

	// iterate n times - executes this.generations.next() n-1 times
	iterate(n) {
		if (typeof n === 'number') {
			for (var i = 0; i < n - 1; i++) {
				this.generations.next()
			}
		}
		return this.generations.next()
	}

}






var bla = new LSystem({
	word:'A---',
	productions: [['A', 'AARA-BB-B'], ['B', 'ABBA-+--B+-'], ['R', 'RA-']]
})

let foo = bla.iterate(20)
console.log(foo)
// let test = bla.iterate(3)

@@ -342,16 +342,22 @@
 ductions
+, save
 %7D) %7B%0A%09%09%09
@@ -454,20 +454,17 @@
 .generat
-ions
+e
  = this.
@@ -470,20 +470,17 @@
 .generat
-ions
+e
 ()%0A%09%7D%0A%0A%09
@@ -487,20 +487,17 @@
 *generat
-ions
+e
 ()%7B%0A%09%09wh
@@ -873,28 +873,25 @@
 this.generat
-ions
+e
 .next(argume
@@ -937,28 +937,25 @@
 this.generat
-ions
+e
 .next() n-1 
@@ -1050,36 +1050,33 @@
 %09%09%09%09this.generat
-ions
+e
 .next()%0A%09%09%09%7D%0A%09%09%7D
@@ -1097,20 +1097,17 @@
 .generat
-ions
+e
 .next()%0A
@@ -1115,19 +1115,16 @@
 %7D%0A%0A%7D%0A%0A%0A%0A
-%0A%0A%0A
 var bla 
@@ -1254,9 +1254,9 @@
 ate(
-2
+1
 0)%0Ac

rename *generations() to *generate(). might want to use this.generations for saving previous generations