From 74411f32c3dbeefbc6497e94abf8dfa2f0fd61bf Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Sun, 8 May 2022 10:19:09 +0800 Subject: [PATCH] refactor --- src/turtle/_impl/_lsystem2_impl.scad | 4 +--- src/turtle/_impl/_lsystem3_impl.scad | 4 +--- src/turtle/_impl/_lsystem_comm.scad | 14 +++++++++----- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/turtle/_impl/_lsystem2_impl.scad b/src/turtle/_impl/_lsystem2_impl.scad index 7706e435..7c18d1a1 100644 --- a/src/turtle/_impl/_lsystem2_impl.scad +++ b/src/turtle/_impl/_lsystem2_impl.scad @@ -3,9 +3,7 @@ use <../turtle2d.scad>; function _lsystem2_join(str_lt) = _join(str_lt); -function _lsystem2_derive(axiom, rules, n, rule_prs) = - is_undef(rule_prs) ? _derive(axiom, rules, n) : - _derive_p(axiom, rules, rule_prs, n); +function _lsystem2_derive(axiom, rules, n, rule_prs) = _derive(axiom, rules, n, rule_prs); function _next_stack(t, code, stack) = code == "[" ? [t, each stack] : diff --git a/src/turtle/_impl/_lsystem3_impl.scad b/src/turtle/_impl/_lsystem3_impl.scad index 5c021650..ec0496b1 100644 --- a/src/turtle/_impl/_lsystem3_impl.scad +++ b/src/turtle/_impl/_lsystem3_impl.scad @@ -3,9 +3,7 @@ use <../turtle3d.scad>; function _lsystem3_join(str_lt) = _join(str_lt); -function _lsystem3_derive(axiom, rules, n, rule_prs) = - is_undef(rule_prs) ? _derive(axiom, rules, n) : - _derive_p(axiom, rules, rule_prs, n); +function _lsystem3_derive(axiom, rules, n, rule_prs) = _derive(axiom, rules, n, rule_prs); function _next_stack(t, code, stack) = code == "[" ? [t, each stack] : diff --git a/src/turtle/_impl/_lsystem_comm.scad b/src/turtle/_impl/_lsystem_comm.scad index c2a166ca..f605d3da 100644 --- a/src/turtle/_impl/_lsystem_comm.scad +++ b/src/turtle/_impl/_lsystem_comm.scad @@ -21,14 +21,18 @@ function _derive1_p(base, rules, rules_pr) = v == [] ? c : c_or_v(c, v, rules, rules_pr, len(v)) ]); -function _derive_p(base, rules, rules_pr, n, i = 0) = - i == n ? base : _derive_p(_derive1_p(base, rules, rules_pr), rules, rules_pr, n, i + 1); - function _derive1(base, rules) = _join([ for(c = base) let(v = _assoc_lookup(rules, c)) is_undef(v) ? c : v ]); -function _derive(base, rules, n, i = 0) = - i == n ? base : _derive(_derive1(base, rules), rules, n, i + 1); \ No newline at end of file +function _derive(base, rules, n, rule_prs) = + let( + derive = is_undef(rule_prs) ? function(base) _derive1(base, rules) : function(base) _derive1_p(base, rules, rules_pr), + bs = [ + for(i = 0, b = derive(base); i < n; i = i + 1, b = i != n ? derive(b) : undef) + b + ] + ) + bs[len(bs) - 1]; \ No newline at end of file