-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoint2.pl
More file actions
executable file
·127 lines (109 loc) · 2.71 KB
/
point2.pl
File metadata and controls
executable file
·127 lines (109 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env perl
use v5.36;
use open ':std', ':encoding(UTF-8)';
use Benchmark qw(cmpthese timethis);
package PointP;
use 5.036;
sub new($class, $x, $y) {
return bless({x => $x, y => $y}, $class);
}
sub x($self, $x=undef) {
if ( defined $x ) {
$self->{x} = $x;
}
return $self->{x};
}
sub y($self, $y=undef) {
if ( defined $y ) {
$self->{y} = $y;
}
return $self->{y};
}
sub add($self, $other) {
return PointP->new(
$self->{x} + $other->{x},
$self->{y} + $other->{y},
);
}
sub to_string($self) {
sprintf "Point(%f,%f)", $self->x, $self->y;
}
package Point3D;
use 5.036;
our @ISA = 'PointP';
sub new($class, $x, $y, $z) {
return bless({ x => $x, y => $y, z => $z }, $class);
}
sub z($self, $z=undef) {
if ( defined $z ) {
$self->{z} = $z;
}
return $self->{z};
}
sub add($self, $other) {
return Point3D->new(
$self->{x} + $other->{x},
$self->{y} + $other->{y},
$self->{z} + $other->{z},
);
}
package main;
my $benchmarks = 0;
# Scheme like
sub make_point($x,$y) { return { x => $x, y => $y } }
sub make_point3($x,$y,$z) { return { x => $x, y => $y, z => $z } }
sub point_x($point) { $point->{x} }
sub point_y($point) { $point->{y} }
sub point_z($point) { $point->{z} }
sub point_str($point) {
return defined $point->{z}
? sprintf "3D: %f,%f,%f", $point->{x}, $point->{y}, $point->{z}
: sprintf "2D: %f,%f", $point->{x}, $point->{y};
}
sub point_add($p1, $p2) {
my $p = {
x => $p1->{x} + $p2->{x},
y => $p1->{y} + $p2->{y},
};
if ( defined $p1->{z} && defined $p1->{z} ) {
$p->{z} = $p1->{z} + $p2->{z};
}
return $p;
}
# Scheme version
my $p = make_point(1,2);
my $p1 = Point3D->new(1,2,3);
my $p2 = Point3D->new(3,2,1);
my $p3 = $p1->add($p2);
my $p4 = point_add(
Point3D->new(1,2,3),
{ x => 3, y => 2, z => 1 },
);
my $p5 = point_add(
{x => 1, y => 2},
{x => 1, y => 2, z => 3},
);
my $p6 = point_add(
make_point(1,2),
{x => 1, y => 2, z => 3},
);
my $p7 = point_add(make_point3(1,2,3), make_point3(3,2,1));
my $p8 = point_add(make_point(1,2), make_point3(3,2,1));
printf "%s\n", point_str($p1);
printf "%s\n", point_str($p2);
printf "%s\n", point_str($p3);
printf "%s\n", point_str($p4);
printf "%s\n", point_str($p5);
printf "%s\n", point_str($p6);
printf "%s\n", point_str($p7);
printf "%s\n", point_str($p8);
{
my $o1 = Point3D->new(1,2,3);
my $o2 = Point3D->new(3,2,1);
my $s1 = make_point3(1,2,3);
my $s2 = make_point3(3,2,1);
cmpthese(-1, {
point3d => sub { $o1->add($o2) for 1 .. 1000 },
scheme => sub { point_add($s1, $s2) for 1 .. 1000 },
});
}