http://qs321.pair.com?node_id=1217647


in reply to Hopfield Neural Network in perl6

This looks pretty cool!

If you allow me to make some remarks: it looks like a very C-ish/Perl 5-ish piece of Perl 6 code.

And I think there is one error in it:

method BUILD($inputneuron, $outputneuron, $y1 = 1000000.rand) +{ $.weight = $y1; }

The $.weight is equivalent to self.weight. You can only use the accessor as a mutator if the attribute is marked is rw. So either you can change the assignment to $!weight = $y1 (directly accessing the attribute), or you can add is rw to the attribute declaration: has $.weight is rw. The choice is really whether you want the weight to be assignable from the outside or not and/or you want subclasses to be able to define their own "weight" method or not.

With regards to C-ism / Perl 5-isms: it feels to me that the following loop

loop (my $i = 0; $i < @.inputsynapses.length; $i++) { if (@.inputsynapses[$i].weight * @.inputsynapses[$i].outputneuron. +input >= 0) { @.inputsynapses[$i].outputneuron.input = 1; } else { @.inputsynapses[$i].outputneuron.input = 0; } }

could be simplified to:

.outputneuron.input = +(.weight * .outputneuron.input >= 0) for @.inputsynapses;

This will iterate over all of the inputsynapses without needing to index. The +( ) is what transforms a Boolean True / False to 1 or 0.

Hope this helps