Skip to content

Instantly share code, notes, and snippets.

@perlpilot
Created May 30, 2009 01:12
Show Gist options
  • Save perlpilot/120313 to your computer and use it in GitHub Desktop.
Save perlpilot/120313 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl6
class Point {
has ($.name, $.x, $.y);
method to_str { return $.name ~ " (" ~ $.x ~ "," ~ $.y ~ ")"; }
}
class Path {
has Point @.path is rw;
method to_str { (@.path.map: { $_.name }).join(" ") ~ " " ~ self.length.fmt("%8.2f"); }
method mutate {
my $i = @.path.keys.pick;
my $j = @.path.keys.pick;
@.path[$i,$j] = @.path[$j,$i];
}
}
class World {
has Point @.points;
has Path @.paths is rw;
method init (@names,$n) {
@!points = Point.new(:name($_), :x(rand * 100), :y(rand * 100)) for @names;
@!paths.push: Path.new(:path(@.points.pick(*))) for 1..$n;
}
method to_str { return @.paths.map({$_.to_str}).join("\n"); }
}
my $n = 10;
my @names = <alpha beta gamma delta epsilon zeta eta theta iota kappa lambda mu nu xi omicron>;
my $w = World.new;
$w.init(@names, $n);
say $w.to_str;
#vim: ft=perl6 sw=4 ts=4 expandtab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment