67 |
71 |
68 Set(1,2,3) == Set(3,1,2) |
72 Set(1,2,3) == Set(3,1,2) |
69 List(1,2,3) == List(3,1,2) |
73 List(1,2,3) == List(3,1,2) |
70 |
74 |
71 |
75 |
72 // this applies to "concrete" values...pretty much everything; |
76 // this applies to "concrete" values...pretty much |
73 // but for example you cannot compare functions (later), |
77 // everything; but for example you cannot compare |
74 // and also not Arrays |
78 // functions (later), and also not arrays |
75 |
79 |
76 Array(1) == Array(1) |
80 Array(1) == Array(1) |
77 |
81 |
78 |
82 |
79 // Printing/Strings |
83 // Printing/Strings |
95 println(lst.mkString(",")) |
99 println(lst.mkString(",")) |
96 |
100 |
97 println(lst.mkString(", ")) |
101 println(lst.mkString(", ")) |
98 |
102 |
99 // some methods take more than one argument |
103 // some methods take more than one argument |
|
104 |
100 println(lst.mkString("{", ",", "}")) |
105 println(lst.mkString("{", ",", "}")) |
101 |
106 |
102 // (in this case .mkString can take no, one, |
107 // (in this case .mkString can take no, one, |
103 // or three arguments...this has to do with |
108 // or three arguments...this has to do with |
104 // default arguments) |
109 // default arguments) |
191 |
196 |
192 |
197 |
193 // Function Definitions |
198 // Function Definitions |
194 //====================== |
199 //====================== |
195 |
200 |
|
201 def foo(s: String) : String = { |
|
202 val tmp = s ++ s ++ s |
|
203 val res = s ++ s |
|
204 res |
|
205 } |
|
206 |
|
207 |
|
208 foo("test") |
|
209 |
196 def incr(x: Int) : Int = x + 1 |
210 def incr(x: Int) : Int = x + 1 |
197 def double(x: Int) : Int = x + x |
211 def double(x: Int) : Int = x + x |
198 def square(x: Int) : Int = x * x |
212 def square(x: Int) : Int = x * x |
199 |
213 |
200 def str(x: Int) : String = x.toString |
214 def str(x: Int) : String = x.toString |
203 double(4) |
217 double(4) |
204 square(6) |
218 square(6) |
205 str(3) |
219 str(3) |
206 |
220 |
207 |
221 |
208 // The general scheme for a function: you have to give a type |
222 // The general scheme for a function: you have to give a |
209 // to each argument and a return type of the function |
223 // type to each argument and a return type of the function |
210 // |
224 // |
211 // def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = { |
225 // def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = { |
212 // body |
226 // |
213 // } |
227 // } |
214 |
228 |
215 |
229 |
216 // |
|
217 // BTW: no returns!! |
230 // BTW: no returns!! |
218 // "last" line (expression) in a function determines the |
231 // "last" line (expression) in a function determines the |
219 // result |
232 // result |
220 |
233 |
221 |
234 |
274 |
286 |
275 |
287 |
276 // For-Comprehensions (not For-Loops) |
288 // For-Comprehensions (not For-Loops) |
277 //==================================== |
289 //==================================== |
278 |
290 |
279 |
291 (1 to 10).toList |
280 for (n <- (1 to 10).toList) yield { |
292 for (n <- (1 to 10).toList) yield { |
281 square(n) + 1 |
293 square(n) + 1 |
282 } |
294 } |
283 |
295 |
284 for (n <- (1 to 10).toList; |
296 for (n <- (1 to 10).toList; |
285 m <- (1 to 10).toList) yield m * n |
297 m <- (1 to 10).toList) yield (m, n) |
286 |
298 |
287 |
299 |
288 // you can assign the result of a for-comprehension |
300 // you can assign the result of a for-comprehension |
289 // to a value |
301 // to a value |
290 val mult_table = |
302 val mult_table = |
294 println(mult_table.mkString) |
306 println(mult_table.mkString) |
295 mult_table.sliding(10,10).mkString("\n") |
307 mult_table.sliding(10,10).mkString("\n") |
296 |
308 |
297 // the list/set/... can also be constructed in any |
309 // the list/set/... can also be constructed in any |
298 // other way |
310 // other way |
|
311 |
299 for (n <- Set(10,12,4,5,7,8,10)) yield n * n |
312 for (n <- Set(10,12,4,5,7,8,10)) yield n * n |
300 |
313 |
|
314 for (n <- (1 to 10)) yield { |
|
315 n * n |
|
316 } |
|
317 |
|
318 if (1 == 2) "a" else "b" |
301 |
319 |
302 // with if-predicates / filters |
320 // with if-predicates / filters |
303 |
321 |
304 for (n <- (1 to 3).toList; |
322 for (n <- (1 to 3).toList; |
305 m <- (1 to 3).toList; |
323 m <- (1 to 3).toList; |
306 if (n + m) % 2 == 0; |
324 if (n + m) % 2 == 0) yield (n, m) |
307 if (n * m) < 2) yield (n, m) |
|
308 |
325 |
309 for (n <- (1 to 3).toList; |
326 for (n <- (1 to 3).toList; |
310 m <- (1 to 3).toList; |
327 m <- (1 to 3).toList; |
311 if ((((n + m) % 2 == 0)))) yield (n, m) |
328 if ((((n + m) % 2 == 0)))) yield (n, m) |
312 |
329 |
328 } |
345 } |
329 |
346 |
330 // Functions producing multiple outputs |
347 // Functions producing multiple outputs |
331 //====================================== |
348 //====================================== |
332 |
349 |
333 def get_ascii(c: Char) : (Char, Int) = (c, c.toInt) |
350 def get_ascii(c: Char) : (Char, Int) = |
|
351 (c, c.toInt) |
334 |
352 |
335 get_ascii('a') |
353 get_ascii('a') |
336 |
354 |
337 |
355 |
338 // .maxBy, sortBy with pairs |
356 // .maxBy, sortBy with pairs |
339 def get_length(s: String) : (String, Int) = (s, s.length) |
357 def get_length(s: String) : (String, Int) = |
|
358 (s, s.length) |
340 |
359 |
341 val lst = List("zero", "one", "two", "three", "four", "ten") |
360 val lst = List("zero", "one", "two", "three", "four", "ten") |
342 val strs = for (s <- lst) yield get_length(s) |
361 val strs = for (s <- lst) yield get_length(s) |
343 |
362 |
344 strs.sortBy(_._2) |
363 strs.sortBy(_._2) |
354 // with only a side-effect (no list is produced), |
373 // with only a side-effect (no list is produced), |
355 // has no "yield" |
374 // has no "yield" |
356 |
375 |
357 for (n <- (1 to 10)) println(n) |
376 for (n <- (1 to 10)) println(n) |
358 |
377 |
359 |
378 (1 to 10).toList |
|
379 (1 until 10).toList |
360 // BTW: a roundabout way of printing out a list, say |
380 // BTW: a roundabout way of printing out a list, say |
361 val lst = ('a' to 'm').toList |
381 val lst = ('a' to 'm').toList |
362 |
382 |
363 for (i <- (0 until lst.length)) println(lst(i)) |
383 for (i <- (0 until lst.length)) println(lst(i)) |
364 |
384 |