Hello roboticus,
I know next-to-nothing about electronics, so I can’t comment on the substance of your code. But...
Comments about my coding style are always welcome
...well, since you ask ;-), I think your OO design could be improved.
(1) Statements like delete $g->{nodes}{out_neg}; violate encapsulation. So does using a return bless ..., "Gr"; statement in a non-Gr function such as build_impedence.
(2) The 4 bless ... "Gr" statements effectively create 4 separate constructors, with different names, which is at best confusing. But the real problem here is the superfluous creation of new objects. There is usually no need to return an object from a method call. Instead of:
$atten = build_pad(10); $atten->dump("PAD 10dB"); my $atten2 = build_pad(20); $atten2->dump("PAD 20dB"); $term = build_impedance(50); $term->dump("TERM 50ohm"); $g = $atten2->attach($term, [qw( out_pos in_pos )], [qw( out_neg in_ne +g )]); delete $g->{nodes}{out_neg}; delete $g->{nodes}{out_pos}; $g->dump("PAD 20dB + TERM 50ohm"); $h = $atten->attach($atten2, [qw( out_pos in_pos )], [qw( out_neg in_n +eg )]); delete $h->{nodes}{out_neg}; delete $h->{nodes}{out_pos}; $h->dump("PAD 10dB + PAD 20dB + TERM 50ohm"); my $i = $h->simplify(); $i->dump("RESULT!");
a cleaner interface would allow:
$atten = Gr->new(pad => 10); $atten->dump("PAD 10dB"); my $atten2 = Gr->new(pad => 20); $atten2->dump("PAD 20dB"); $term = Gr->new(impedance => 50); $term->dump("TERM 50ohm"); $atten2->attach($term, [qw( out_pos in_pos )], [qw( out_neg in_neg )]) +; $atten2->delete('nodes', 'out_neg'); $atten2->delete('nodes', 'out_pos'); $atten2->dump("PAD 20dB + TERM 50ohm"); $atten->attach($atten2, [qw( out_pos in_pos )], [qw( out_neg in_neg )] +); $atten->delete('nodes', 'out_neg'); $atten->delete('nodes', 'out_pos'); $atten->dump("PAD 10dB + PAD 20dB + TERM 50ohm"); $atten->simplify(); $atten->dump("RESULT!");
with all the gory details of object manipulation — including cloning, where required — confined to the Gr class.
BTW, this line of code:
my ($in_neg, $in_pos, $out_neg, $out_pos) = (node(), node(), node(), n +ode());
struck me as a challenge: is it possible to remove the duplicate calls? The obvious ... = (node()) x 4 won’t work, because the node() function has to be called four times, not just once. With a little trial-and-error, I found (to my surprise) that this does work (tested on Strawberry Perls 5.20.0 and 5.28.0):
$$_ = node() for \my ($in_neg, $in_pos, $out_neg, $out_pos);
Hope that’s of interest,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
In reply to Re: Resistor network simplifier
by Athanasius
in thread Resistor network simplifier
by roboticus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |