Merge: doc: fixed some typos and other misc. corrections
[nit.git] / examples / int_stack.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # An example that defines and uses stacks of integers.
16 # The implementation is done with a simple linked list.
17 # It features: free constructors, nullable types and some adaptive typing.
18 module int_stack is example
19
20 # A stack of integer implemented by a simple linked list.
21 # Note that this is only a toy class since a real linked list will gain to use
22 # generics and extends interfaces, like `Collection`, from the `core` library.
23 class IntStack
24 # The head node of the list.
25 # Null means that the stack is empty.
26 private var head: nullable ISNode = null
27
28 # Add a new integer in the stack.
29 fun push(val: Int)
30 do
31 self.head = new ISNode(val, self.head)
32 end
33
34 # Remove and return the last pushed integer.
35 # Return null if the stack is empty.
36 fun pop: nullable Int
37 do
38 var head = self.head
39 if head == null then return null
40 # Note: the followings are statically safe because of the
41 # previous 'if'.
42 var val = head.val
43 self.head = head.next
44 return val
45 end
46
47 # Return the sum of all integers of the stack.
48 # Return 0 if the stack is empty.
49 fun sumall: Int
50 do
51 var sum = 0
52 var cur = self.head
53 while cur != null do
54 # Note: the followings are statically safe because of
55 # the condition of the 'while'.
56 sum += cur.val
57 cur = cur.next
58 end
59 return sum
60 end
61
62 # Note: Because all attributes have a default value, a free constructor
63 # "init()" is implicitly defined.
64 end
65
66 # A node of a IntStack
67 private class ISNode
68 # The integer value stored in the node.
69 var val: Int
70
71 # The next node, if any.
72 var next: nullable ISNode
73
74 # Note: A free constructor "init(val: Int, next: nullable ISNode)" is
75 # implicitly defined.
76 end
77
78 var l = new IntStack
79 l.push(1)
80 l.push(2)
81 l.push(3)
82
83 print l.sumall
84
85 # Note: the 'for' control structure cannot be used on IntStack in its current state.
86 # It requires a more advanced topic.
87 # However, why not using the 'loop' control structure?
88 loop
89 var i = l.pop
90 if i == null then break
91 # The following is statically safe because of the previous 'if'.
92 print i * 10
93 end
94
95 # Note: 'or else' is used to give an alternative of a null expression.
96 l.push(5)
97 print l.pop or else 0 # l.pop gives 5, so print 5
98 print l.pop or else 0 # l.pop gives null, so print the alternative: 0
99
100