Skip to content
This repository has been archived by the owner on Aug 5, 2021. It is now read-only.

part1 #179

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

part1 #179

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/i_introduction/_0_Hello_World/n00Start.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ fun todoTask0(): Nothing = TODO(
)

fun task0(): String {
return todoTask0()
return "OK"
}
16 changes: 13 additions & 3 deletions src/i_introduction/_10_Object_Expressions/n10ObjectExpressions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,29 @@ import util.doc10
import java.util.*

fun todoTask10(): Nothing = TODO(
"""
"""
Task 10.
Read about object expressions that play the same role in Kotlin as anonymous classes in Java.

Add an object expression that provides a comparator to sort a list in a descending order using 'java.util.Collections' class.
In Kotlin you use Kotlin library extensions instead of java.util.Collections,
but this example is still a good demonstration of mixing Kotlin and Java code.
""",
documentation = doc10()
documentation = doc10()
)

fun task10(): List<Int> {
val arrayList = arrayListOf(1, 5, 2)
Collections.sort(arrayList, todoTask10())
Collections.sort(arrayList, { x, y -> y-x})
fun xxx(x: Int, y: Int): Int {
return y - x
}
Collections.sort(arrayList, fun(x: Int, y: Int): Int {
return y - x
})
return arrayList
}

fun xxx(x: Int, y: Int): Int {
return y - x
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ fun todoTask11(): Nothing = TODO(

fun task11(): List<Int> {
val arrayList = arrayListOf(1, 5, 2)
Collections.sort(arrayList, { x, y -> todoTask11() })
Collections.sort(arrayList, { x, y -> y - x })
return arrayList
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fun todoTask12(): Nothing = TODO(
)

fun task12(): List<Int> {
todoTask12()
return arrayListOf(1, 5, 2)

return arrayListOf(1, 5, 2).sortedDescending()
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,16 @@ fun todoTask1(collection: Collection<Int>): Nothing = TODO(


fun task1(collection: Collection<Int>): String {
todoTask1(collection)
val sb = StringBuilder()
sb.append("{")
val iterator = collection.iterator()
while (iterator.hasNext()) {
val element = iterator.next()
sb.append(element)
if (iterator.hasNext()) {
sb.append(", ")
}
}
sb.append("}")
return sb.toString()
}
4 changes: 2 additions & 2 deletions src/i_introduction/_2_Named_Arguments/n02NamedArguments.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ fun todoTask2(): Nothing = TODO(
references = { collection: Collection<Int> -> task1(collection); collection.joinToString() })

fun task2(collection: Collection<Int>): String {
todoTask2()
return collection.joinToString()

return collection.joinToString(prefix="{",postfix="}")
}
15 changes: 9 additions & 6 deletions src/i_introduction/_3_Default_Arguments/n03DefaultArguments.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ fun todoTask3(): Nothing = TODO(
documentation = doc2(),
references = { name: String -> JavaCode3().foo(name); foo(name) })

fun foo(name: String): String = todoTask3()
fun foo(name: String, number: Int=42, toUpperCase : Boolean=false): String {
if(toUpperCase == true ) { return name.toUpperCase() + number } else { return name + number}
}


fun task3(): String {
todoTask3()
// return (foo("a") +
// foo("b", number = 1) +
// foo("c", toUpperCase = true) +
// foo(name = "d", number = 2, toUpperCase = true))

return (foo("a") +
foo("b", number = 1) +
foo("c", toUpperCase = true) +
foo(name = "d", number = 2, toUpperCase = true))
}
2 changes: 1 addition & 1 deletion src/i_introduction/_4_Lambdas/n04Lambdas.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ fun todoTask4(collection: Collection<Int>): Nothing = TODO(
documentation = doc4(),
references = { JavaCode4().task4(collection) })

fun task4(collection: Collection<Int>): Boolean = todoTask4(collection)
fun task4(collection: Collection<Int>): Boolean =collection.any{ x -> x%2==0}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ fun todoTask5(): Nothing = TODO(
documentation = doc5(),
references = { getPattern(); month })

fun task5(): String = todoTask5()
fun task5(): String = """\d{2} ${month} \d{4}"""
5 changes: 2 additions & 3 deletions src/i_introduction/_6_Data_Classes/n06DataClasses.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ fun todoTask6(): Nothing = TODO(
references = { JavaCode6.Person("Alice", 29) }
)

class Person
data class Person(var name: String , var age: Int )

fun task6(): List<Person> {
todoTask6()
return listOf(/*Person("Alice", 29), Person("Bob", 31)*/)
return listOf(Person("Alice", 29), Person("Bob", 31))
}

8 changes: 7 additions & 1 deletion src/i_introduction/_7_Nullable_Types/n07NullableTypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ fun todoTask7(client: Client?, message: String?, mailer: Mailer): Nothing = TODO
fun sendMessageToClient(
client: Client?, message: String?, mailer: Mailer
) {
todoTask7(client, message, mailer)
if (client == null || message == null) return

val personalInfo = client.personalInfo ?: return

val email = personalInfo.email ?: return

mailer.sendMessage(email, message)
}

class Client(val personalInfo: PersonalInfo?)
Expand Down
4 changes: 2 additions & 2 deletions src/i_introduction/_8_Smart_Casts/n08SmartCasts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class Sum(val left: Expr, val right: Expr) : Expr()

fun eval(e: Expr): Int =
when (e) {
is Num -> todoTask8(e)
is Sum -> todoTask8(e)
is Num -> (e as Num).value
is Sum -> eval(e.left) + eval(e.right)
}

fun todoTask8(expr: Expr): Nothing = TODO(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fun todoTask9(): Nothing = TODO(

data class RationalNumber(val numerator: Int, val denominator: Int)

fun Int.r(): RationalNumber = todoTask9()
fun Pair<Int, Int>.r(): RationalNumber = todoTask9()
fun Int.r(): RationalNumber = RationalNumber( this,1)
fun Pair<Int, Int>.r(): RationalNumber =RationalNumber( first,second)