File tree 15 files changed +36
-33
lines changed
Function literals with receiver
All Any and other predicates
Getting used to new style
Generics/Generic functions
15 files changed +36
-33
lines changed Original file line number Diff line number Diff line change 15
15
}
16
16
```
17
17
18
- ** 'td' is:**
18
+ ** ` td ` is:**
19
19
20
20
a. a special built-in syntactic construct
21
21
@@ -38,7 +38,7 @@ tr (color = "yellow") {
38
38
}
39
39
```
40
40
41
- ** ' color' is:**
41
+ ** ` color ` is:**
42
42
43
43
a. a new variable declaration
44
44
Original file line number Diff line number Diff line change 2
2
3
3
Learn about [ function literals with receiver] ( https://kotlinlang.org/docs/lambdas.html#function-literals-with-receiver ) .
4
4
5
- You can declare ` isEven ` and ` isOdd ` as values, that can be called as extension functions.
5
+ You can declare ` isEven ` and ` isOdd ` as values that can be called as extension functions.
6
6
Complete the declarations in the code.
Original file line number Diff line number Diff line change 1
1
## HTML builder
2
2
3
- _ 1. _ Fill the table with proper values from the product list.
3
+ 1 . Fill the table with proper values from the product list.
4
4
The products are declared in ` data.kt ` .
5
5
6
- _ 2. _ Color the table like a chess board .
6
+ 2 . Color the table like a chessboard .
7
7
Use the ` getTitleColor() ` and ` getCellColor() ` functions.
8
8
Pass a color as an argument to the functions ` tr ` , ` td ` .
9
9
Original file line number Diff line number Diff line change @@ -20,6 +20,6 @@ val s = buildString {
20
20
s == " Numbers: 123"
21
21
```
22
22
23
- Implement the function ` buildMutableMap ` that takes a parameter (of extension function type) creates a new ` HashMap ` ,
23
+ Implement the function ` buildMutableMap ` that takes a parameter (of extension function type), creates a new ` HashMap ` ,
24
24
builds it, and returns it as a result. Note that starting from 1.3.70, the standard library has a similiar ` buildMap `
25
25
function.
Original file line number Diff line number Diff line change 2
2
3
3
The previous examples can be rewritten using the library function
4
4
[ ` apply ` ] ( https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/apply.html ) .
5
- Write your own implementation of this function named ` myApply ` .
5
+ Write your implementation of this function named ` myApply ` .
6
6
7
7
Learn about the other [ scope functions] ( https://kotlinlang.org/docs/scope-functions.html )
8
8
and how to use them.
Original file line number Diff line number Diff line change @@ -8,10 +8,11 @@ Implement the following functions using
8
8
[ ` any ` ] ( https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/any.html ) ,
9
9
[ ` count ` ] ( https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/count.html ) ,
10
10
[ ` find ` ] ( https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/find.html ) :
11
- - ` checkAllCustomersAreFrom ` should return true if all customers are from a given city
12
- - ` hasCustomerFrom ` should check if there is at least one customer from a given city
13
- - ` countCustomersFrom ` should return the number of customers from a given city
14
- - ` findCustomerFrom ` should return a customer who lives in a given city, or ` null ` if there is none
11
+
12
+ * ` checkAllCustomersAreFrom ` should return true if all customers are from a given city
13
+ * ` hasCustomerFrom ` should check if there is at least one customer from a given city
14
+ * ` countCustomersFrom ` should return the number of customers from a given city
15
+ * ` findCustomerFrom ` should return a customer who lives in a given city, or ` null ` if there is none
15
16
16
17
``` kotlin
17
18
val numbers = listOf (- 1 , 0 , 2 )
Original file line number Diff line number Diff line change @@ -6,9 +6,9 @@ Implement the following functions using
6
6
[ ` associateWith ` ] ( https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/associate-with.html ) ,
7
7
and [ ` associate ` ] ( https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/associate.html ) :
8
8
9
- - Build a map from the customer name to the customer
10
- - Build a map from the customer to their city
11
- - Build a map from the customer name to their city
9
+ * Build a map from the customer name to the customer
10
+ * Build a map from the customer to their city
11
+ * Build a map from the customer name to their city
12
12
13
13
``` kotlin
14
14
val list = listOf (" abc" , " cdef" )
Original file line number Diff line number Diff line change 1
1
## Compound tasks
2
2
3
3
Implement two functions:
4
- - The first one should find the most expensive product among all the * delivered* products
5
- ordered by the customer. Use ` Order.isDelivered ` flag.
6
4
7
- - The second one should count the amount of times a product was ordered.
8
- Note that a customer may order the same product several times.
5
+ * The first one should find the most expensive product among all the * delivered* products
6
+ ordered by the customer. Use ` Order.isDelivered ` flag
7
+ * The second one should count the number of times a product was ordered. Note that a customer may order the same product
8
+ several times
9
9
10
10
Use the functions from the Kotlin standard library that were previously discussed.
11
11
Original file line number Diff line number Diff line change 1
1
## Filter; map
2
2
3
- Learn about [ mapping] ( https://kotlinlang.org/docs/collection-transformations.html#mapping ) and
4
- [ filtering] ( https://kotlinlang.org/docs/collection-filtering.html#filtering -by-predicate ) a collection.
3
+ Learn about [ mapping] ( https://kotlinlang.org/docs/collection-transformations.html#map ) and
4
+ [ filtering] ( https://kotlinlang.org/docs/collection-filtering.html#filter -by-predicate ) a collection.
5
5
6
6
Implement the following extension functions
7
7
using the
8
8
[ ` map ` ] ( https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/map.html ) and
9
9
[ ` filter ` ] ( https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/filter.html )
10
10
functions:
11
- - Find all the different cities the customers are from.
12
- - Find the customers living in a given city.
11
+
12
+ * Find all the different cities the customers are from
13
+ * Find the customers living in a given city
13
14
14
15
``` kotlin
15
16
val numbers = listOf (1 , - 1 , 2 )
Original file line number Diff line number Diff line change 3
3
Learn about [ flattening] ( https://kotlinlang.org/docs/collection-transformations.html#flatten )
4
4
and implement two functions using
5
5
[ ` flatMap ` ] ( https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/flat-map.html ) :
6
- - The first should return all products the given customer has ordered
7
- - The second should return all products that were ordered by at least one customer
6
+
7
+ * The first should return all products the given customer has ordered
8
+ * The second should return all products that at least one customer ordered
8
9
9
10
``` kotlin
10
11
val result = listOf (" abc" , " 12" )
Original file line number Diff line number Diff line change 1
1
## Fold
2
2
3
3
Learn about [ fold and reduce] ( https://kotlinlang.org/docs/collection-aggregate.html#fold-and-reduce )
4
- and implement a function that returns the set of products that were ordered by all the customers using
5
- [ ` fold ` ] ( https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/fold.html ) .
4
+ and implement a function that returns the set of products that all the customers ordered using [ ` fold ` ] ( https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/fold.html ) .
6
5
7
- You can use the ` Customer.getOrderedProducts() ` which was defined in the previous task
8
- (copy its implementation).
6
+ You can use the ` Customer.getOrderedProducts() ` defined in the previous task (copy its implementation).
9
7
10
8
``` kotlin
11
9
listOf (1 , 2 , 3 , 4 )
Original file line number Diff line number Diff line change 1
- ## Getting used to new style
1
+ ## Getting used to the new style
2
2
3
3
We can rewrite and simplify the following code using lambdas and operations on collections.
4
4
Fill in the gaps in ` doSomethingWithCollection ` ,
Original file line number Diff line number Diff line change 3
3
Learn about [ collection aggregate operations] ( https://kotlinlang.org/docs/collection-aggregate.html ) .
4
4
5
5
Implement two functions:
6
- - the first should return the customer who has placed the most amount of orders in this shop
7
- - the second should return the most expensive product that has been ordered by the given customer
6
+
7
+ * The first should return the customer who has placed the most amount of orders in this shop
8
+ * The second should return the most expensive product that the given customer has ordered
8
9
9
10
The functions
10
11
[ ` maxOrNull ` ] ( https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/max-or-null.html ) ,
@@ -19,6 +20,6 @@ listOf("a", "ab").minByOrNull(String::length) == "a"
19
20
```
20
21
21
22
You can use [ callable references] ( https://kotlinlang.org/docs/lambdas.html#instantiating-a-function-type )
22
- instead of lambdas. This can be especially helpful in call chains, where
23
+ instead of lambdas. It can be especially helpful in call chains, where
23
24
` it ` occurs in different lambdas and has different types.
24
25
Implement the ` getMostExpensiveProductBy ` function using callable references.
Original file line number Diff line number Diff line change 2
2
3
3
Learn about [ sequences] ( https://kotlinlang.org/docs/sequences.html ) ,
4
4
they allow you to perform operations lazily rather than eagerly.
5
+
5
6
Copy the implementation from the previous task and modify it in a way
6
7
that the operations on sequences are used.
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ a collection into two collections according to the predicate.
6
6
7
7
There is a [ ` partition() ` ] ( https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/partition.html )
8
8
function in the standard library that always returns two newly created lists.
9
- You should write a function that splits the collection into two collections given as arguments.
9
+ Write a function that splits the collection into two collections given as arguments.
10
10
The signature of the
11
11
[ ` toCollection() ` ] ( https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/to-collection.html )
12
12
function from the standard library might help you.
You can’t perform that action at this time.
0 commit comments