-
Notifications
You must be signed in to change notification settings - Fork 0
/
test2
52 lines (47 loc) · 1017 Bytes
/
test2
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
[[#Object instance-methods] at: 'dump put:
{(self)
[[[self instance-of] instance-variables] foreach:
{(v) [v print]
["=" print]
[[[#Method-Call new: ('self v)] eval] print]
["\n" print]
}
]
#nil
}
]
// Factorial
[[#Integer instance-methods] at: 'fac put:
{(self)
[[self equals: 0] if: 1 else: '[self mult: [[self add: -1] fac]]]
}
]
// GCD
[[#Integer instance-methods] at: "gcd:" put:
{(a b)
[[b gt: a] if: '[b gcd: a] else:
'[[b equals: 0] if: 'a else:
'[b gcd: [a mod: b]]
]
]
}
]
[#Metaclass new: "Point" parent: #Object instance-variables: '(x y)]
[[Point instance-methods] at: 'tostring put:
{(self) [([self x] [self y]) tostring]}
]
[[Point instance-methods] at: "add:" put:
{(self p)
[result := [Point new]]
[result x: [[self x] add: [p x]]]
[result y: [[self y] add: [p y]]]
result
}
]
[#Environment at: 'p1 put: [Point new]]
[#Environment at: 'p2 put: [Point new]]
[p1 x: 1]
[p1 y: 2]
[p2 x: 13]
[p2 y: 42]
[p1 add: p2]