Skip to content

Commit 4a6c776

Browse files
committed
intro verbesserung
1 parent 5c252ea commit 4a6c776

File tree

4 files changed

+129
-21
lines changed

4 files changed

+129
-21
lines changed

src/main/worksheets/a_bridge/c_collections.sc

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,95 @@
33
// | Lists - immutable (normally) |
44
// +----------------------------------------------------+
55

6+
// ====================================
7+
// Array
8+
// ====================================
69
val array = Array(2,3,4,5)
710
array.update(2,400)
811
array
912

1013

11-
// define an empty list
12-
val list1 = List()
14+
// ====================================
15+
// List
16+
// ====================================
17+
18+
// TODO -1- define a list with values 1,2,3,4
19+
val list1: List[Int] = List(1,2,3,4)
20+
21+
assert(list1.sum == 10)
22+
23+
// TODO -2- define a list with value "b", "c"
24+
val list2: List[String] = List("b", "c")
25+
26+
assert(list2(0) == "b")
27+
assert(list2(1) == "c")
28+
1329

1430
// append
1531
// ------
1632
// the operator '::' is defined in List
1733
// - Scala use internal case class '::' which extends List
1834
// - add the left operand as first element to the new list and return it
19-
"a" :: "b" :: list1
35+
// TODO -me- append value "a" to the head of list2
36+
val list3:List[String] = "a" :: list2
37+
38+
assert(list3(0) == "a")
2039

2140
// the same to ::
22-
list1.::("b").::("a")
41+
list2.::("a")
2342
// => http://www.scala-lang.org/api/2.11.5/index.html#scala.collection.immutable.List
2443
// => Scala Basic Part I
2544

2645

27-
// ADD
28-
val list2 = List(1,2)
29-
list2 ++ List(3,4)
30-
list2 :+ 3
31-
list2.+:(3)
46+
// TODO -me- concat list2 and list3
47+
val list4:List[String] = list2 ++ list3
48+
49+
assert(list4(2) == "a")
50+
51+
52+
// TODO -me- addon
53+
// additional
54+
val list5 = List(1,2)
55+
// concat two lists
56+
list5 ++ List(3,4)
57+
// add right
58+
list5 :+ 3
59+
// add left
60+
list5.+:(3)
61+
62+
63+
// ====================================
64+
// Methods
65+
// ====================================
66+
val seq1: Seq[Int] = Seq(1,2,3,4,5,6)
67+
68+
// TODO -1- multiply each value by 2 and
69+
// sum up every value
70+
val seq1Sum = seq1.map(x => x*2).sum
71+
assert(seq1Sum == 42)
72+
73+
// TODO -me- bring the synatic sugar
74+
seq1.map((x: Int) => x*2).sum
75+
seq1 map(_*2) sum
76+
77+
// TODO -me- what is (x: Int) => x*2?
78+
val f = (x: Int) => x*2
79+
seq1.map(f).sum
80+
seq1 map f sum
81+
82+
83+
// +---------------------------------------------
84+
// | TODO -me- addon => only as reference
85+
// | additional - reference
86+
// +---------------------------------------------
87+
// head / tail
88+
seq1.head
89+
seq1.tail
90+
// some methods
91+
seq1.filter(_ % 2 == 0)
92+
seq1.groupBy(x => x%2)
93+
seq1.reduce((x,y) => x+y)
94+
seq1.partition(_ % 2 == 0)
95+
// zip and map
96+
val seq2: Seq[Int] = seq1.reverse
97+
seq1.zip(seq2).map(x => x._1+x._2)
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
// a )
2-
// -1- define an array of Integer with initial values 2,3,4,5
3-
// -2- overwrite the value 4 with 400
4-
1+
// TODO -1- define an array of Integer with
2+
// initial values 2,3,4,5
53
val array:Array[Int] = ???
64
assert(array(3) == 4)
75

6+
// TODO -2- overwrite the value 4 with 400
87

98
assert(array(3) == 400)
9+
Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
1-
// a )
2-
// -1- define a list with values 1,2,3,4
3-
val list1: List[Int] = List()
1+
// TODO -1- define a list with values 1,2,3,4
2+
val list1: List[Int] = ???
43

54
assert(list1.sum == 10)
65

7-
// -2- define a list with value "b", "c"
6+
// TODO -2- define a list with value "b", "c"
87
val list2: List[String] = List()
98

109
assert(list2(0) == "b")
1110
assert(list2(1) == "c")
1211

1312

14-
// -3- append value "a" to the head of list2
15-
val list3 = list2
13+
// TODO -me- append value "a" to the head of list2
14+
val list3:List[String] = ???
1615

1716
assert(list3(0) == "a")
1817

1918

20-
// -4- append list2 and list3
21-
val list4 = list2
19+
// TODO -me- concat list2 and list3
20+
val list4:List[String] = ???
2221

2322
assert(list4(2) == "a")
23+
24+
25+
// TODO -me- addon
26+
// additional
27+
val list5 = List(1,2)
28+
// concat two lists
29+
list5 ++ List(3,4)
30+
// add right
31+
list5 :+ 3
32+
// add left
33+
list5.+:(3)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
val seq1: Seq[Int] = Seq(1,2,3,4,5,6)
2+
3+
// TODO -1- multiply each value by 2 and
4+
// sum up every value
5+
val seq1Sum = seq1.map(x => x*2).sum
6+
assert(seq1Sum == 42)
7+
8+
// TODO -me- bring the synatic sugar
9+
seq1.map((x: Int) => x*2).sum
10+
seq1 map(_*2) sum
11+
12+
// TODO -me- what is (x: Int) => x*2?
13+
val f = (x: Int) => x*2
14+
seq1.map(f).sum
15+
seq1 map f sum
16+
17+
// +---------------------------------------------
18+
// | TODO -me- addon => only as reference
19+
// | additional - reference
20+
// +---------------------------------------------
21+
// head / tail
22+
seq1.head
23+
seq1.tail
24+
// some methods
25+
seq1.filter(_ % 2 == 0)
26+
seq1.groupBy(x => x%2)
27+
seq1.reduce((x,y) => x+y)
28+
seq1.partition(_ % 2 == 0)
29+
// zip and map
30+
val seq2: Seq[Int] = seq1.reverse
31+
seq1.zip(seq2).map(x => x._1+x._2)
32+

0 commit comments

Comments
 (0)