This repository has been archived by the owner on Sep 17, 2019. It is now read-only.
forked from Kotlin/kotlin-koans
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Synchronized the first part with workshop @web-demo
- Loading branch information
Showing
41 changed files
with
228 additions
and
212 deletions.
There are no files selected for viewing
49 changes: 13 additions & 36 deletions
49
src/i_introduction/_10_Object_Expressions/ObjectExpressions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,22 @@ | ||
package i_introduction._10_Object_Expressions | ||
|
||
import java.util.Comparator | ||
import util.TODO | ||
import java.io.File | ||
import java.awt.event.MouseListener | ||
import java.awt.event.MouseAdapter | ||
import java.awt.event.MouseEvent | ||
|
||
fun objectLiteral() { | ||
|
||
abstract class Foo { | ||
abstract fun foo() | ||
} | ||
|
||
// Anonymous object extending a class | ||
val foo: Foo = object : Foo() { | ||
override fun foo() { | ||
// ... | ||
} | ||
} | ||
|
||
// Anonymous object extending an interface | ||
runInANewThread(object : Runnable { | ||
override fun run() { | ||
// ... | ||
} | ||
}) | ||
} | ||
|
||
fun runInANewThread(runnable: Runnable) = Thread(runnable).start() | ||
import java.util.* | ||
import util.* | ||
|
||
fun todoTask10(): Nothing = TODO( | ||
""" | ||
Task 10. | ||
Add an object expression that extends MouseAdapter and counts the number of mouse clicks | ||
as an argument to the function 'handleMouse()'. | ||
""" | ||
Read about object expressions that play the same role in Kotlin as anonymous classes do 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() | ||
) | ||
|
||
fun task10(handleMouse: (MouseListener) -> Unit): Int { | ||
var mouseClicks = 0 | ||
handleMouse(todoTask10()) | ||
return mouseClicks | ||
fun task10(): List<Int> { | ||
val arrayList = arrayListOf(1, 5, 2) | ||
Collections.sort(arrayList, todoTask10()) | ||
return arrayList | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package i_introduction._11_SAM_Conversions | ||
|
||
import util.TODO | ||
import util.doc11_1 | ||
import java.util.* | ||
|
||
fun todoTask11() = TODO( | ||
""" | ||
Task 11. | ||
When an object implements a SAM interface (one with a Single Abstract Method), you can pass a lambda instead. | ||
Read more about SAM conversions in the blog posts about Kotlin. | ||
Rewrite the previous example changing an object expression to a lambda. | ||
""", | ||
documentation = doc11_1() | ||
) | ||
|
||
fun task11(): List<Int> { | ||
val arrayList = arrayListOf(1, 5, 2) | ||
Collections.sort(arrayList, { x, y -> todoTask11() }) | ||
return arrayList | ||
} |
22 changes: 22 additions & 0 deletions
22
src/i_introduction/_12_Extensions_On_Collections/ExtensionsOnCollections.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package i_introduction._12_Extensions_On_Collections | ||
|
||
import util.* | ||
|
||
fun todoTask12() = TODO( | ||
""" | ||
Task 12. | ||
In Kotlin standard library there are lots of extension functions that make the work with collections more convenient. | ||
Rewrite the previous example once more using an extension function 'sortedDescending'. | ||
Kotlin code can be easily mixed with Java code. | ||
Thus in Kotlin we don't introduce our own collections, but use standard Java ones (slightly improved). | ||
Read about read-only and mutable views on Java collections. | ||
""", | ||
documentation = doc12_1() | ||
) | ||
|
||
fun task12(): List<Int> { | ||
todoTask12() | ||
return arrayListOf(1, 5, 2) | ||
} | ||
|
2 changes: 1 addition & 1 deletion
2
..._introduction/_1_Functions/JavaCode1.java → ...1_Java_To_Kotlin_Converter/JavaCode1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package i_introduction._1_Functions; | ||
package i_introduction._1_Java_To_Kotlin_Converter; | ||
|
||
import util.JavaCode; | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
src/i_introduction/_1_Functions/Functions.kt → ...Kotlin_Converter/JavaToKotlinConverter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package i_introduction._1_Functions | ||
package i_introduction._1_Java_To_Kotlin_Converter | ||
|
||
import util.TODO | ||
|
||
|
23 changes: 0 additions & 23 deletions
23
src/i_introduction/_2_Default_And_Named_Params/DefaultAndNamedParams.kt
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
src/i_introduction/_2_Default_And_Named_Params/DefaultAndNamedParamsUsage.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package i_introduction._2_Named_Arguments | ||
|
||
import util.* | ||
import i_introduction._1_Java_To_Kotlin_Converter.task1 | ||
|
||
fun todoTask2() = TODO( | ||
""" | ||
Task 2. | ||
Implement the same logic as in 'task1' again through the library method 'joinToString()'. | ||
Change values of some of the 'joinToString' arguments. | ||
Use default and named arguments to improve the readability of the function invocation. | ||
""", | ||
documentation = doc2(), | ||
references = { collection: Collection<Int> -> task1(collection); collection.joinToString() }) | ||
|
||
fun task2(collection: Collection<Int>): String { | ||
todoTask2() | ||
return collection.joinToString() | ||
} |
24 changes: 24 additions & 0 deletions
24
src/i_introduction/_3_Default_Arguments/DefaultAndNamedParams.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package i_introduction._3_Default_Arguments | ||
|
||
import util.* | ||
|
||
fun todoTask3_() = TODO( | ||
""" | ||
Task 3. | ||
Several overloads of 'JavaCode2.foo()' can be replaced with one function in Kotlin. | ||
Change the declaration of the function 'foo' in a way that makes the code using 'foo' compile. | ||
You have to add parameters and replace 'todoTask3()' with a real body. | ||
Uncomment the commented code and make it compile. | ||
""", | ||
documentation = doc2(), | ||
references = { name: String -> JavaCode2().foo(name); foo(name) }) | ||
|
||
fun foo(name: String): String = todoTask3_() | ||
|
||
fun task3_(): String { | ||
todoTask3_() | ||
// return (foo("a") + | ||
// foo("b", number = 1) + | ||
// foo("c", toUpperCase = true) + | ||
// foo(name = "d", number = 2, toUpperCase = true)) | ||
} |
2 changes: 1 addition & 1 deletion
2
...2_Default_And_Named_Params/JavaCode2.java → ...ction/_3_Default_Arguments/JavaCode2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/i_introduction/_3_Lambdas/JavaCode3.java → src/i_introduction/_4_Lambdas/JavaCode3.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/i_introduction/_3_Lambdas/Lambdas.kt → src/i_introduction/_4_Lambdas/Lambdas.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package i_introduction._3_Lambdas | ||
package i_introduction._4_Lambdas | ||
|
||
import util.* | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...on/_4_String_Templates/StringTemplates.kt → ...on/_5_String_Templates/StringTemplates.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...troduction/_7_Data_Classes/DataClasses.kt → ...troduction/_6_Data_Classes/DataClasses.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package i_introduction._7_Data_Classes | ||
package i_introduction._6_Data_Classes | ||
|
||
import util.* | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...troduction/_7_Data_Classes/JavaCode7.java → ...troduction/_6_Data_Classes/JavaCode7.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package i_introduction._7_Data_Classes; | ||
package i_introduction._6_Data_Classes; | ||
|
||
import util.JavaCode; | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...oduction/_5_Nullable_Types/JavaCode5.java → ...oduction/_7_Nullable_Types/JavaCode5.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...uction/_5_Nullable_Types/NullableTypes.kt → ...uction/_7_Nullable_Types/NullableTypes.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package i_introduction._5_Nullable_Types | ||
package i_introduction._7_Nullable_Types | ||
|
||
import util.* | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...ntroduction/_6_Smart_Casts/JavaCode6.java → ...ntroduction/_8_Smart_Casts/JavaCode6.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package i_introduction._6_Smart_Casts; | ||
package i_introduction._8_Smart_Casts; | ||
|
||
import util.JavaCode; | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...introduction/_6_Smart_Casts/SmartCasts.kt → ...introduction/_8_Smart_Casts/SmartCasts.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package i_introduction._6_Smart_Casts | ||
package i_introduction._8_Smart_Casts | ||
|
||
import util.* | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...Extension_Functions/ExtensionFunctions.kt → ...Extension_Functions/ExtensionFunctions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 0 additions & 61 deletions
61
src/i_introduction/_9_Extensions_On_Collections/ExtensionsOnCollections.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.