syntax: 'meth' -> 'fun', 'attr' -> 'var'
[nit.git] / tests / test_refinement.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17
18 redef class Int
19 fun fact0: Int
20 do
21 return fact(1)
22 end
23 fun fact(r: Int): Int
24 do
25 if self <= 1 then
26 return r
27 else
28 return (self-1).fact(r*self)
29 end
30 return 1
31 end
32
33 fun fact2: Int
34 do
35 var r = 1
36 var i = self
37 while i > 0 do
38 r = r * i
39 i = i - 1
40 end
41 return r
42 end
43 end
44
45 redef class Array[F]
46 redef fun add(item: F)
47 do
48 self[length] = item
49 self[length] = item
50 end
51 end
52
53 redef class Object
54 redef fun printn(a: Object...)
55 do
56 stdout.write("print:")
57 stdout.write(a.to_s)
58 end
59 end
60
61 printn("4! = ")
62 print(4.fact2)
63 printn("4! = ")
64 print(4.fact0)
65
66 var a = [1,2]
67 do
68 print(a)
69 a.add(3)
70 print(a)
71 end
72
73 var b = new Buffer.from("ab")
74 do
75 print(b)
76 b.add('c')
77 print(b)
78 end