Repository navigation
Expand file tree
/
Copy pathCFL_system.sc
More file actions
93 lines (85 loc) · 1.93 KB
/
Copy pathCFL_system.sc
File metadata and controls
93 lines (85 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
CFL_system {
var <>rules, <>root, <>current_str, <>map;
/**new {
|rules|
^super.new.rules_(rules = Dictionary.newFrom(List["a","ab","b","a"]))
}*/
// this is a normal constructor method
*new { |rules, map|
^super.new.init(rules, map)
}
init { |rules, map|
//postln("rules", rules);
var result = Dictionary.new();
if (rules == nil, {rules = Dictionary.newFrom(List["a","ab","b","a"])});
//postln("map", map == 7);
if (map == nil, { rules.keys.asSortedList.do{|key, i|
result.add(key.asString -> i);
}; map = result
});
this.rules_(rules);
this.map_(map);
}
gen_simple_map {
var result = Dictionary.new();
this.rules.keys.asSortedList.do{|key, i|
result.add(key.asString -> i);
};
this.map_(result)
}
process_single {
|char|
var result;
result = this.rules[char.asString];
^result;
}
process {
|str|
var result;
str.do {
|char|
result = result ++ this.process_single(char);
}
^result;
}
iterate {
/* returns only final string */
|str, iterations = 1|
var result;
iterations.do {
result = this.process(str);
str = result;
};
//this.current_str_(result);
^result;
}
iterations {
/* returns each iteration as element in list*/
|str, cycles = 1|
var results = Array.new(cycles), cur_result;
postln(cycles);
cycles.do {
|i|
postln(str);
cur_result = this.process(str);
results = results.add(cur_result);
str = cur_result;
};
this.current_str_(results[cycles-1]);
^results;
}
/*map_to_num {
|str = "", mapping_arr = ""|
// mapping_arr maps first rule key to 0
var dict_size = this.rules.size, result = Array.new(str.size), map_dict = Dictionary.new();
if (mapping_arr == "", {mapping_arr = Array.fill(this.rules.size, {|i|i})});
if (str == "", {str = this.current_str});
this.rules.keys.do { |key, i|
map_dict.put(key.asString, mapping_arr[i]);
};
str.do { |char|
result = result.add(map_dict[char.asString]);
}
^result
}*/
}