forked from IoLanguage/iolanguage.org
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtutorial.html
281 lines (233 loc) · 6.68 KB
/
tutorial.html
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
<title>Core Programming Language – Tutorial</title>
<link rel="stylesheet" href="css/base.css">
<script src="js/toc.js" type="text/javascript"></script>
</head>
<body>
<div class="container leftSpace">
<header>
<div class="pageHeader">
<div class="topSpace"></div>
<div class="pageTitle"><a href=".">Core</a></div>
<div class="pageSubtitle"> / tutorial </div>
</div>
</header>
<main>
<nav class="toc">
<div id="toc-lang">
<p>Language</p>
</div>
<div id="toc-std">
<p>Library</p>
</div>
</nav>
<div id="doc-lang">
<h3>Literals</h3>
<h4>Numbers</h4>
<div class="code">
1 -23 123'456
<div class="explanation">some integer values of type Int64</div>
1i32 -23i32 123'456i32
<div class="explanation">some integer values of type Int32</div>
1.0 -23.0 123'456.0
<div class="explanation">some floating-point values of type Float64</div>
1.0f32 -23.0f32 123'456.0f32
<div class="explanation">some floating-point values of type Float32</div>
</div>
<h4>Booleans</h4>
<div class="code">
true false
<div class="explanation">the two values of type Bool</div>
</div>
<h4>Strings</h4>
<div class="code">
"abc" ""
<div class="explanation">some values of type String</div>
</div>
<h3>Definitions</h3>
<h4>Constants</h4>
<div class="code">
let a = 1
<div class="explanation">the Int64 value 1 assigned to the constant a</div>
a
<div class="result">1</div>
a = 2
<div class="explanation">compilation error – a constant cannot be reassigned</div>
</div>
<h4>Variables</h4>
<div class="code">
var a = 1
<div class="explanation">the Int64 value 1 assigned to the variable a</div>
a
<div class="result">1</div>
a = 2 * 3
<div class="result">6</div>
</div>
<h4>Functions</h4>
<div class="code">
fun sum(a: Int64, b: Int64): Int64 = a + b
<div class="explanation">a function accepting two arguments of type Int64</div>
<div class="explanation">and returning a value of type Int64</div>
sum(12, 23)
<div class="result">35</div>
</div>
<h4>Classes</h4>
<div class=code>
class Person(let firstName: String, let lastName: String, var age: Int64) {
fun fullName(): String = "${self.firstName} ${self.lastName}"
}
let alice = Person("Alice", "Smith", 23)
alice.lastName
<div class="result">"Smith"</div>
alice.fullName()
<div class="result">"Alice Smith"</div>
alice.age = 24
alice.age
<div class="result">24</div>
</div>
<h4>Modules (not implemented)</h4>
<div class=code>
module Utilities {
fun appendTest(value: String): String = "${value} test"
}
Utilities.appendTest("string")
<div class="result">"string test"</div>
</div>
<h4>Namespaces (not implemented)</h4>
<h3>Control Flow</h3>
<h4>Conditions</h4>
<div class=code>
let a = 2
if a == 1 { println("a is one") } else { println("a is not one") }
<div class="result">a is not one</div>
fun describeFloat(float: Float64): String =
if float
... === Float64.NaN { "not a number" }
... .isFinite.not { "infinity" }
... >= 0.0 { "positive" }
else { "negative" }
describeFloat(123.4)
<div class="result">"positive"</div>
describeFloat(1.0/0.0)
<div class="result">"infinity"</div>
</div>
<h4>Loops</h4>
<div class=code>
for i in List[Int64](1, 2, 3) {
print(i.toString())
}
<div class="result">"123"</div>
</div>
<h3>Operators</h3>
From strongest precedence to weakest:
<table class="operators">
<thead>
<tr>
<th>Precedence</th>
<th>Operator</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td><code><span class="operator">*</span><span class="operator">/</span><span class="operator">&</span></code></td>
</tr>
<tr>
<td>2</td>
<td><code><span class="operator">+</span><span class="operator">-</span><span class="operator">|</span><span class="operator">^</span></code></td>
</tr>
<tr>
<td>3</td>
<td><code><span class="operator">==</span><span class="operator">!=</span><span class="operator"><</span><span class="operator"><=</span><span class="operator">></span><span class="operator">>=</span><span class="operator">===</span><span class="operator">!==</span></code></td>
</tr>
<tr>
<td>4</td>
<td><code><span class="operator">&&</span></code></td>
</tr>
<tr>
<td>5</td>
<td><code><span class="operator">||</span></code></td>
</tr>
</tbody>
</table>
<div class=code>
1 + 1
<div class="result">2</div>
23.0 + 42.0 / 2.0
<div class="result">44.0</div>
</div>
</div>
<div id="doc-std">
<h3>Basic Types</h3>
<h4>Strings</h4>
<div class=code>
let a = "foo"
<div class="result">"foo"</div>
let c = a + "bar"
<div class="result">"foobar"</div>
c.size()
<div class="result">6</div>
c.isEmpty()
<div class="result">false</div>
</div>
<h4>Options</h4>
<div class=code>
let some = Some[String]("value")
<div class="result">Some("value")</div>
let none = None[String]()
<div class="result">None</div>
some.isSome()
<div class="result">true</div>
none.isSome()
<div class="result">false</div>
some.contains("value")
<div class="result">true</div>
some.contains("other")
<div class="result">false</div>
none.contains("value")
<div class="result">false</div>
</div>
<h4>Lists</h4>
<div class=code>
let list = List[Int64](30, 10, 20, 40)
<div class="result">List(30, 10, 20, 40)</div>
list.size()
<div class="result">4</div>
list(0) // same as list.get(0)
<div class="result">30</div>
list(0) = 5 // same as list.set(0, 5)
<div class="result">List(5, 10, 20, 40)</div>
list.first()
<div class="result">Some(5)</div>
list.last()
<div class="result">Some(40)</div>
d.removeAt(3)
<div class="result">List(5, 10, 20)</div>
d.push(40)
<div class="result">List(5, 10, 20, 40)</div>
</div>
<h4>Set</h4>
<div class=code>...</div>
<h4>Maps</h4>
<div class=code>
let map = HashMap[String, String](("hello", "a greeting"), ("goodbye", "a parting"))
<div class="result">HashMap(("hello", "a greeting"), ("goodbye", "a parting"))</div>
map.contains("hello")
<div class="result">true</div>
map.get("hello")
<div class="result">Some("a greeting")</div>
map.insert("ciao", "not sure ...")
<div class="result">HashMap(("hello", "a greeting"), ("goodbye", "a parting"), ("ciao", "not sure ..."))</div>
map.remove("ciao")
<div class="result">"not sure ..."</div>
</div>
<h4>Array</h4>
<div class=code>...</div>
</div>
</main>
</div>
</body>
</html>