Evolutionary algorithm: Difference between revisions

→‎{{header|Raku}}: more FP version and use \r not to scroll
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
(→‎{{header|Raku}}: more FP version and use \r not to scroll)
Line 6,846:
=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo|2015-1107-1109}}
<syntaxhighlight lang="raku" line>constant target = "METHINKS IT IS LIKE A WEASEL";
constant mutate_chance = .08001;
constant @alphabet = flat 'A'..'Z',' ';
constant C = 10010;
 
sub mutate { [~] (rand < mutate_chance ?? @alphabet.pick !! $_ for $^string.comb) }
sub fitness { [+] $^string.comb Zeq target.comb }
 
) { printf "\r%6d: '%s'\n", $++, $parent_ }</syntaxhighlight>for
loop (
my $parent = @alphabet.roll(target.chars).join;,
{ max :by(&fitness), mutate($_) xx C } ... * eq target;</syntaxhighlight>
$parent ne target;
$parent = max :by(&fitness), mutate($parent) xx C
) { printf "%6d: '%s'\n", $++, $parent }</syntaxhighlight>
 
=={{header|Red}}==
1,934

edits