├── .idea ├── .name ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── vcs.xml ├── modules.xml ├── libraries │ ├── KotlinJavaRuntime.xml │ ├── Maven__junit_junit_4_12.xml │ ├── Maven__org_ow2_asm_asm_5_0_3.xml │ ├── Maven__org_pegdown_pegdown_1_6_0.xml │ ├── Maven__com_google_code_gson_gson_2_5.xml │ ├── Maven__org_ow2_asm_asm_tree_5_0_3.xml │ ├── Maven__org_ow2_asm_asm_util_5_0_3.xml │ ├── Maven__org_hamcrest_hamcrest_core_1_3.xml │ ├── Maven__org_jetbrains_annotations_13_0.xml │ └── Maven__org_ow2_asm_asm_analysis_5_0_3.xml └── misc.xml ├── .gitignore ├── Kotlin Koans EE plugin ├── lesson1 │ ├── task1 │ │ ├── Task.kt │ │ ├── tests.kt │ │ └── task.html │ ├── task2 │ │ ├── Task.kt │ │ ├── tests.kt │ │ └── task.html │ ├── task3 │ │ ├── Task.kt │ │ └── tests.kt │ ├── task5 │ │ ├── Task.kt │ │ ├── tests.kt │ │ └── task.html │ ├── task6 │ │ └── Task.kt │ ├── task13 │ │ ├── Task.kt │ │ └── tests.kt │ ├── task7 │ │ ├── Task.kt │ │ └── tests.kt │ ├── task10 │ │ ├── Task.kt │ │ ├── task.html │ │ └── tests.kt │ ├── task11 │ │ ├── Task.kt │ │ ├── tests.kt │ │ └── task.html │ ├── task12 │ │ ├── Task.kt │ │ ├── tests.kt │ │ └── task.html │ ├── task4 │ │ ├── tests.kt │ │ └── Task.kt │ ├── task8 │ │ └── Task.kt │ └── task9 │ │ ├── Task.kt │ │ └── tests.kt ├── lesson3 │ ├── task1 │ │ ├── Task.kt │ │ ├── tests.kt │ │ └── Shop.kt │ ├── task8 │ │ ├── Task.kt │ │ ├── tests.kt │ │ ├── Shop.kt │ │ └── task.html │ ├── task10 │ │ ├── Task.kt │ │ ├── task.html │ │ └── Shop.kt │ ├── task6 │ │ ├── Task.kt │ │ ├── tests.kt │ │ └── Shop.kt │ ├── task9 │ │ ├── Task.kt │ │ ├── tests.kt │ │ └── Shop.kt │ ├── task7 │ │ ├── Task.kt │ │ ├── Shop.kt │ │ └── tests.kt │ ├── task11 │ │ ├── task.html │ │ ├── Task.kt │ │ └── Shop.kt │ ├── task2 │ │ ├── Task.kt │ │ ├── Shop.kt │ │ ├── task.html │ │ └── tests.kt │ ├── task4 │ │ ├── Task.kt │ │ ├── task.html │ │ ├── tests.kt │ │ └── Shop.kt │ ├── task5 │ │ ├── Task.kt │ │ ├── Shop.kt │ │ └── tests.kt │ ├── task12 │ │ ├── Task.kt │ │ └── Shop.kt │ └── task3 │ │ ├── Shop.kt │ │ └── Task.kt ├── lesson4 │ ├── task3 │ │ ├── Task.kt │ │ └── task.html │ ├── task1 │ │ ├── Task.kt │ │ └── task.html │ ├── task2 │ │ ├── Task.kt │ │ └── task.html │ └── task4 │ │ ├── task.html │ │ ├── tests.kt │ │ ├── Task.kt │ │ └── MyDate.kt ├── lesson5 │ ├── task5 │ │ └── Task.kt │ ├── task1 │ │ ├── Task.kt │ │ ├── task.html │ │ └── tests.kt │ ├── task2 │ │ └── Task.kt │ ├── task3 │ │ ├── task.html │ │ └── Task.kt │ └── task4 │ │ ├── demo.kt │ │ ├── task.html │ │ └── tests.kt ├── lesson2 │ ├── task1 │ │ ├── Task.kt │ │ └── task.html │ ├── task2 │ │ ├── Task.kt │ │ ├── MyDate.kt │ │ └── task.html │ ├── task6 │ │ ├── task.html │ │ ├── Task.kt │ │ └── tests.kt │ ├── task7 │ │ ├── Task.kt │ │ └── task.html │ ├── task4 │ │ ├── Task.kt │ │ ├── task.html │ │ ├── MyDate.kt │ │ └── DateUtil.kt │ ├── task3 │ │ ├── Task.kt │ │ ├── MyDate.kt │ │ └── task.html │ └── task5 │ │ ├── Task.kt │ │ ├── task.html │ │ └── DateUtil.kt └── util │ └── koansTestUtil.kt ├── Kotlin Koans ├── Collections │ ├── Fold │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ ├── task.md │ │ └── Solution.kt │ ├── Sort │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ ├── Solution.kt │ │ ├── task.md │ │ └── Test.kt │ ├── Sum │ │ ├── manifest.json │ │ ├── Solution.kt │ │ ├── Task.kt.txt │ │ ├── task.md │ │ └── Test.kt │ ├── Filter map │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ ├── Solution.kt │ │ └── task.md │ ├── FlatMap │ │ ├── manifest.json │ │ ├── task.md │ │ ├── Task.kt.txt │ │ ├── Solution.kt │ │ └── Test.kt │ ├── GroupBy │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ ├── Solution.kt │ │ ├── task.md │ │ └── Test.kt │ ├── Max min │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ ├── task.md │ │ └── Solution.kt │ ├── Partition │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ ├── Solution.kt │ │ ├── task.md │ │ └── Test.kt │ ├── Compound tasks │ │ ├── manifest.json │ │ ├── task.md │ │ └── Task.kt.txt │ ├── Introduction │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ ├── Solution.kt │ │ ├── Test.kt │ │ └── task.md │ ├── Get used to new style │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ └── Solution.kt │ ├── All Any and other predicates │ │ ├── manifest.json │ │ └── task.md │ └── Shop.kt ├── Conventions │ ├── Invoke │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ ├── task.md │ │ └── Solution.kt │ ├── Comparison │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ ├── task.md │ │ └── Solution.kt │ ├── Destructuring declarations │ │ ├── manifest.json │ │ ├── task.md │ │ ├── Task.kt.txt │ │ ├── Test.kt │ │ └── Solution.kt │ ├── manifest.json │ ├── In range │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ ├── MyDate.kt │ │ ├── Solution.kt │ │ └── task.md │ ├── Range to │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ ├── MyDate.kt │ │ ├── task.md │ │ └── Solution.kt │ ├── For loop │ │ ├── Task.kt.txt │ │ ├── task.md │ │ ├── manifest.json │ │ └── MyDate.kt │ └── Operators overloading │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ └── task.md ├── Introduction │ ├── Strings │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ └── Solution.kt │ ├── Data classes │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ ├── Solution.kt │ │ └── Test.kt │ ├── Hello, world! │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ ├── Solution.kt │ │ ├── Test.kt │ │ └── task.md │ ├── Named arguments │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ ├── Solution.kt │ │ └── Test.kt │ ├── Nullable types │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ └── Solution.kt │ ├── Smart casts │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ ├── task.md │ │ ├── Solution.kt │ │ └── Test.kt │ ├── Default arguments │ │ ├── manifest.json │ │ ├── Test.kt │ │ ├── Task.kt.txt │ │ └── Solution.kt │ ├── Extension functions │ │ ├── manifest.json │ │ ├── task.md │ │ ├── Task.kt.txt │ │ ├── Solution.kt │ │ └── Test.kt │ ├── Object expressions │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ ├── Test.kt │ │ ├── Solution.kt │ │ └── task.md │ ├── Extensions on collections │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ ├── Solution.kt │ │ ├── Test.kt │ │ └── task.md │ ├── Java to Kotlin conversion │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ ├── task-ee.md │ │ ├── Test.kt │ │ └── Solution.kt │ ├── SAM conversions │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ ├── task.md │ │ ├── Solution.kt │ │ └── Test.kt │ ├── Lambdas │ │ ├── Task.kt.txt │ │ ├── manifest.json │ │ ├── Solution.kt │ │ ├── task.md │ │ └── Test.kt │ └── manifest.json ├── Properties │ ├── Properties │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ ├── task.md │ │ └── Solution.kt │ ├── Lazy property │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ ├── task.md │ │ └── Solution.kt │ ├── Delegates examples │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ ├── task.md │ │ └── Solution.kt │ ├── manifest.json │ └── Delegates how it works │ │ ├── manifest.json │ │ ├── task.md │ │ ├── Test.kt │ │ ├── Task.kt.txt │ │ └── MyDate.kt ├── Builders │ ├── The function apply │ │ ├── manifest.json │ │ ├── task.md │ │ └── Task.kt.txt │ ├── Builders how it works │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ └── Solution.kt │ ├── Function literals with receiver │ │ ├── manifest.json │ │ ├── task.md │ │ ├── Task.kt.txt │ │ ├── Solution.kt │ │ └── Test.kt │ ├── String and map builders │ │ ├── manifest.json │ │ ├── Task.kt.txt │ │ └── Solution.kt │ ├── Html builders │ │ ├── main-wb.kt.txt │ │ ├── task-wb.md │ │ ├── task-ee.md │ │ ├── demo-ee.kt │ │ └── Test.kt │ └── manifest.json ├── Generics │ ├── Generic functions │ │ ├── manifest.json │ │ └── task.md │ └── manifest.json └── koansTestUtil.kt ├── Kotlin Koans WebDemo ├── Collections │ ├── Sum │ │ ├── manifest.json │ │ ├── Solution.kt │ │ ├── Task.kt │ │ ├── Test.kt │ │ └── task.md │ ├── FlatMap │ │ ├── manifest.json │ │ ├── Solution.kt │ │ ├── Task.kt │ │ ├── task.md │ │ └── Test.kt │ ├── Fold │ │ ├── manifest.json │ │ ├── Task.kt │ │ ├── task.md │ │ └── Solution.kt │ ├── GroupBy │ │ ├── manifest.json │ │ ├── Solution.kt │ │ ├── Task.kt │ │ ├── Test.kt │ │ └── task.md │ ├── Max min │ │ ├── manifest.json │ │ ├── Solution.kt │ │ └── Task.kt │ ├── Sort │ │ ├── manifest.json │ │ ├── Solution.kt │ │ ├── Task.kt │ │ ├── Test.kt │ │ └── task.md │ ├── Filter map │ │ ├── manifest.json │ │ ├── Solution.kt │ │ ├── Task.kt │ │ └── task.md │ ├── Introduction │ │ ├── manifest.json │ │ ├── Solution.kt │ │ ├── Task.kt │ │ └── Test.kt │ ├── Partition │ │ ├── manifest.json │ │ ├── Task.kt │ │ ├── Solution.kt │ │ ├── Test.kt │ │ └── task.md │ ├── Compound tasks │ │ ├── manifest.json │ │ ├── task.md │ │ ├── Solution.kt │ │ └── Task.kt │ ├── Get used to new style │ │ ├── manifest.json │ │ ├── Solution.kt │ │ └── Task.kt │ ├── All Any and other predicates │ │ ├── manifest.json │ │ └── Solution.kt │ └── Shop.kt ├── Conventions │ ├── Invoke │ │ ├── manifest.json │ │ ├── Task.kt │ │ ├── Solution.kt │ │ └── task.md │ ├── Comparison │ │ ├── manifest.json │ │ ├── Task.kt │ │ ├── Solution.kt │ │ └── task.md │ ├── Destructuring declarations │ │ ├── manifest.json │ │ ├── task.md │ │ ├── Task.kt │ │ ├── Solution.kt │ │ └── Test.kt │ ├── In range │ │ ├── Task.kt │ │ ├── manifest.json │ │ ├── Solution.kt │ │ ├── MyDate.kt │ │ └── task.md │ ├── manifest.json │ ├── Range to │ │ ├── manifest.json │ │ ├── Task.kt │ │ ├── Solution.kt │ │ ├── MyDate.kt │ │ └── task.md │ ├── For loop │ │ ├── Task.kt │ │ ├── MyDate.kt │ │ ├── manifest.json │ │ └── task.md │ └── Operators overloading │ │ ├── manifest.json │ │ ├── Task.kt │ │ ├── DateUtil.kt │ │ └── task.md ├── Introduction │ ├── Hello, world! │ │ ├── Solution.kt │ │ ├── manifest.json │ │ ├── Task.kt │ │ ├── Test.kt │ │ └── task.md │ ├── Strings │ │ ├── manifest.json │ │ ├── Task.kt │ │ └── Solution.kt │ ├── Data classes │ │ ├── manifest.json │ │ ├── Task.kt │ │ ├── Solution.kt │ │ └── Test.kt │ ├── Smart casts │ │ ├── manifest.json │ │ ├── Solution.kt │ │ ├── Task.kt │ │ └── Test.kt │ ├── Default arguments │ │ ├── manifest.json │ │ ├── Test.kt │ │ ├── Task.kt │ │ └── Solution.kt │ ├── Named arguments │ │ ├── manifest.json │ │ ├── Solution.kt │ │ ├── Task.kt │ │ └── Test.kt │ ├── Nullable types │ │ ├── manifest.json │ │ ├── Task.kt │ │ └── Solution.kt │ ├── Extension functions │ │ ├── manifest.json │ │ ├── Task.kt │ │ ├── Solution.kt │ │ ├── task.md │ │ └── Test.kt │ ├── Object expressions │ │ ├── manifest.json │ │ ├── Task.kt │ │ ├── Test.kt │ │ ├── Solution.kt │ │ └── task.md │ ├── Extensions on collections │ │ ├── manifest.json │ │ ├── Solution.kt │ │ ├── Task.kt │ │ └── Test.kt │ ├── Java to Kotlin conversion │ │ ├── manifest.json │ │ ├── Task.kt │ │ ├── Test.kt │ │ └── Solution.kt │ ├── SAM conversions │ │ ├── manifest.json │ │ ├── Solution.kt │ │ ├── Task.kt │ │ ├── Test.kt │ │ └── task.md │ ├── Lambdas │ │ ├── Solution.kt │ │ ├── Task.kt │ │ ├── manifest.json │ │ ├── Test.kt │ │ └── task.md │ └── manifest.json ├── Properties │ ├── Lazy property │ │ ├── manifest.json │ │ ├── Task.kt │ │ ├── task.md │ │ └── Solution.kt │ ├── Properties │ │ ├── manifest.json │ │ ├── Task.kt │ │ ├── Solution.kt │ │ └── task.md │ ├── Delegates examples │ │ ├── manifest.json │ │ ├── Solution.kt │ │ ├── Task.kt │ │ └── task.md │ ├── manifest.json │ └── Delegates how it works │ │ ├── manifest.json │ │ ├── task.md │ │ ├── Test.kt │ │ ├── MyDate.kt │ │ ├── Task.kt │ │ └── Solution.kt ├── Builders │ ├── Builders how it works │ │ ├── manifest.json │ │ ├── Solution.kt │ │ └── Task.kt │ ├── The function apply │ │ ├── manifest.json │ │ ├── task.md │ │ ├── Task.kt │ │ └── Solution.kt │ ├── Function literals with receiver │ │ ├── manifest.json │ │ ├── Solution.kt │ │ ├── Task.kt │ │ ├── task.md │ │ └── Test.kt │ ├── String and map builders │ │ ├── manifest.json │ │ ├── Task.kt │ │ └── Solution.kt │ ├── Html builders │ │ ├── main.kt │ │ ├── task.md │ │ └── Test.kt │ └── manifest.json ├── Generics │ ├── Generic functions │ │ └── manifest.json │ └── manifest.json └── koansTestUtil.kt └── course.properties /.idea/.name: -------------------------------------------------------------------------------- 1 | KoansConverter -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | out 2 | workspace.xml 3 | Kotlin Koans.zip -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task1/Task.kt: -------------------------------------------------------------------------------- 1 | fun start(): String = TODO() -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Fold/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Sort/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Sum/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/Invoke/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Sum/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Filter map/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/FlatMap/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/GroupBy/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Max min/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Partition/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/Comparison/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Strings/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Properties/Properties/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/FlatMap/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Fold/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/GroupBy/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Max min/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Sort/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/Invoke/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Hello, world!/Solution.kt: -------------------------------------------------------------------------------- 1 | fun start(): String = "OK" -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Strings/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Builders/The function apply/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Compound tasks/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Introduction/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Generics/Generic functions/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Generics/manifest.json: -------------------------------------------------------------------------------- 1 | {"sequential":false,"examples":["Generic functions"]} -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Data classes/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Hello, world!/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Named arguments/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Nullable types/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Smart casts/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Properties/Lazy property/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Filter map/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Introduction/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Partition/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/Comparison/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Data classes/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Smart casts/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Properties/Lazy property/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Properties/Properties/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Builders/Builders how it works/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Get used to new style/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Default arguments/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Extension functions/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Object expressions/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Properties/Delegates examples/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task1/Task.kt: -------------------------------------------------------------------------------- 1 | fun Shop.getSetOfCustomers(): Set = TODO() -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Builders/Builders how it works/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Builders/The function apply/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Compound tasks/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Generics/Generic functions/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Generics/manifest.json: -------------------------------------------------------------------------------- 1 | {"sequential":false,"examples":["Generic functions"]} -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Default arguments/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Hello, world!/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Named arguments/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Nullable types/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Properties/Delegates examples/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/Destructuring declarations/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Extensions on collections/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Java to Kotlin conversion/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Get used to new style/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Extension functions/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Object expressions/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Builders/Function literals with receiver/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/All Any and other predicates/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task2/Task.kt: -------------------------------------------------------------------------------- 1 | fun toJSON(collection: Collection): String = TODO() 2 | -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Builders/Function literals with receiver/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/All Any and other predicates/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/Destructuring declarations/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Extensions on collections/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Hello, world!/Task.kt: -------------------------------------------------------------------------------- 1 | fun start(): String = TODO() -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Java to Kotlin conversion/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit" 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Hello, world!/Task.kt.txt: -------------------------------------------------------------------------------- 1 | fun start(): String = TODO() -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task3/Task.kt: -------------------------------------------------------------------------------- 1 | fun joinOptions(options: Collection) = options.joinToString(TODO()) -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Introduction/Solution.kt: -------------------------------------------------------------------------------- 1 | fun Shop.getSetOfCustomers(): Set = customers.toSet() -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/SAM conversions/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name":"SAM conversions", 3 | "confType": "junit" 4 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task5/Task.kt: -------------------------------------------------------------------------------- 1 | fun containsEven(collection: Collection): Boolean = collection.any { TODO() } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/SAM conversions/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name":"SAM conversions", 3 | "confType": "junit" 4 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Introduction/Task.kt.txt: -------------------------------------------------------------------------------- 1 | fun Shop.getSetOfCustomers(): Set = TODO() -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Introduction/Task.kt: -------------------------------------------------------------------------------- 1 | fun Shop.getSetOfCustomers(): Set = TODO() -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson4/task3/Task.kt: -------------------------------------------------------------------------------- 1 | class LazyProperty(val initializer: () -> Int) { 2 | val lazyValue: Int by TODO() 3 | } 4 | 5 | -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Lambdas/Solution.kt: -------------------------------------------------------------------------------- 1 | fun containsEven(collection: Collection): Boolean = collection.any { it % 2 == 0 } 2 | -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task6/Task.kt: -------------------------------------------------------------------------------- 1 | val month = "(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)" 2 | 3 | fun getPattern(): String = TODO() -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/GroupBy/Solution.kt: -------------------------------------------------------------------------------- 1 | fun Shop.groupCustomersByCity(): Map> = 2 | customers.groupBy { it.city } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Java to Kotlin conversion/Task.kt: -------------------------------------------------------------------------------- 1 | fun toJSON(collection: Collection): String = TODO() 2 | -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Hello, world!/Solution.kt: -------------------------------------------------------------------------------- 1 | package introduction.hello_world 2 | 3 | fun start(): String = /**/"OK"/**/ -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Java to Kotlin conversion/Task.kt.txt: -------------------------------------------------------------------------------- 1 | fun toJSON(collection: Collection): String = TODO() 2 | -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Lambdas/Task.kt.txt: -------------------------------------------------------------------------------- 1 | fun containsEven(collection: Collection): Boolean = collection.any { TODO() } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Named arguments/Task.kt.txt: -------------------------------------------------------------------------------- 1 | fun joinOptions(options: Collection) = options.joinToString(TODO()) -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Sort/Solution.kt: -------------------------------------------------------------------------------- 1 | fun Shop.getCustomersSortedByNumberOfOrders(): List = 2 | customers.sortedBy { it.orders.size } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Sum/Solution.kt: -------------------------------------------------------------------------------- 1 | fun Customer.getTotalOrderPrice(): Double = 2 | orders.flatMap { it.products }.sumByDouble { it.price } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Lambdas/Task.kt: -------------------------------------------------------------------------------- 1 | fun containsEven(collection: Collection): Boolean = collection.any { TODO() } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Named arguments/Solution.kt: -------------------------------------------------------------------------------- 1 | fun joinOptions(options: Collection) = options.joinToString(prefix = "[", postfix = "]") -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Named arguments/Task.kt: -------------------------------------------------------------------------------- 1 | fun joinOptions(options: Collection) = options.joinToString(TODO()) -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson4/task1/Task.kt: -------------------------------------------------------------------------------- 1 | class PropertyExample() { 2 | var counter = 0 3 | var propertyWithCounter: Int? = null 4 | set 5 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task13/Task.kt: -------------------------------------------------------------------------------- 1 | fun getList(): List { 2 | return arrayListOf(1, 5, 2)//TODO("return the list sorted in descending order") 3 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task7/Task.kt: -------------------------------------------------------------------------------- 1 | class Person 2 | 3 | fun getPeople(): List { 4 | return listOf(Person("Alice", 29), Person("Bob", 31)) 5 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task8/Task.kt: -------------------------------------------------------------------------------- 1 | // Return a map of the customers living in each city 2 | fun Shop.groupCustomersByCity(): Map> = TODO() -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Properties/Delegates examples/Solution.kt: -------------------------------------------------------------------------------- 1 | class LazyProperty(val initializer: () -> Int) { 2 | val lazyValue: Int by lazy(initializer) 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Lambdas/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Lambdas", 3 | "containsHelp": false, 4 | "args": "", 5 | "confType": "junit", 6 | "help": "" 7 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Extensions on collections/Solution.kt: -------------------------------------------------------------------------------- 1 | fun getList(): List { 2 | return arrayListOf(1, 5, 2).sortedDescending() 3 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Lambdas/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Lambdas", 3 | "containsHelp": false, 4 | "args": "", 5 | "confType": "junit", 6 | "help": "" 7 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Strings/Task.kt.txt: -------------------------------------------------------------------------------- 1 | val month = "(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)" 2 | 3 | fun getPattern(): String = TODO() -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Strings/Task.kt: -------------------------------------------------------------------------------- 1 | val month = "(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)" 2 | 3 | fun getPattern(): String = TODO() -------------------------------------------------------------------------------- /Kotlin Koans/Properties/Delegates examples/Task.kt.txt: -------------------------------------------------------------------------------- 1 | class LazyProperty(val initializer: () -> Int) { 2 | val lazyValue: Int by TODO() 3 | } 4 | 5 | -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Properties/Delegates examples/Task.kt: -------------------------------------------------------------------------------- 1 | class LazyProperty(val initializer: () -> Int) { 2 | val lazyValue: Int by TODO() 3 | } 4 | 5 | -------------------------------------------------------------------------------- /Kotlin Koans/Properties/Delegates examples/task.md: -------------------------------------------------------------------------------- 1 | ## Delegates example 2 | 3 | Read about [delegated properties](delegated_properties) 4 | and make the property lazy by using delegates. -------------------------------------------------------------------------------- /Kotlin Koans/Properties/Properties/Task.kt.txt: -------------------------------------------------------------------------------- 1 | class PropertyExample() { 2 | var counter = 0 3 | var propertyWithCounter: Int? = null 4 | set 5 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Properties/Properties/Task.kt: -------------------------------------------------------------------------------- 1 | class PropertyExample() { 2 | var counter = 0 3 | var propertyWithCounter: Int? = null 4 | set 5 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/GroupBy/Task.kt.txt: -------------------------------------------------------------------------------- 1 | // Return a map of the customers living in each city 2 | fun Shop.groupCustomersByCity(): Map> = TODO() -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/GroupBy/Task.kt: -------------------------------------------------------------------------------- 1 | // Return a map of the customers living in each city 2 | fun Shop.groupCustomersByCity(): Map> = TODO() -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Strings/Solution.kt: -------------------------------------------------------------------------------- 1 | val month = "(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)" 2 | 3 | fun getPattern(): String = """\d{2} ${month} \d{4}""" -------------------------------------------------------------------------------- /Kotlin Koans/Builders/String and map builders/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "String and map builders", 3 | "containsHelp": false, 4 | "args": "", 5 | "confType": "junit", 6 | "help": "" 7 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Data classes/Task.kt.txt: -------------------------------------------------------------------------------- 1 | class Person 2 | 3 | fun getPeople(): List { 4 | return listOf(Person("Alice", 29), Person("Bob", 31)) 5 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task10/Task.kt: -------------------------------------------------------------------------------- 1 | // Return the set of products that were ordered by every customer 2 | fun Shop.getSetOfProductsOrderedByEveryCustomer(): Set { 3 | TODO() 4 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task6/Task.kt: -------------------------------------------------------------------------------- 1 | // Return a list of customers, sorted by the ascending number of orders they made 2 | fun Shop.getCustomersSortedByNumberOfOrders(): List = TODO() -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task9/Task.kt: -------------------------------------------------------------------------------- 1 | // Return customers who have more undelivered orders than delivered 2 | fun Shop.getCustomersWithMoreUndeliveredOrdersThanDelivered(): Set = TODO() -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson4/task2/Task.kt: -------------------------------------------------------------------------------- 1 | class LazyProperty(val initializer: () -> Int) { 2 | /* TODO */ 3 | val lazy: Int 4 | get() { 5 | TODO() 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson5/task5/Task.kt: -------------------------------------------------------------------------------- 1 | import Answer.* 2 | 3 | enum class Answer { a, b, c } 4 | 5 | val answers = mapOf( 6 | 1 to null, 2 to null, 3 to null, 4 to null 7 | ) -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Data classes/Task.kt: -------------------------------------------------------------------------------- 1 | class Person 2 | 3 | fun getPeople(): List { 4 | return listOf(Person("Alice", 29), Person("Bob", 31)) 5 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Builders/String and map builders/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "String and map builders", 3 | "containsHelp": false, 4 | "args": "", 5 | "confType": "junit", 6 | "help": "" 7 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Extensions on collections/Task.kt.txt: -------------------------------------------------------------------------------- 1 | fun getList(): List { 2 | return arrayListOf(1, 5, 2)//TODO("return the list sorted in descending order") 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Lambdas/Solution.kt: -------------------------------------------------------------------------------- 1 | package introduction.lambdas 2 | 3 | fun containsEven(collection: Collection): Boolean = collection.any { /**/it % 2 == 0/**/ } 4 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task1/tests.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Assert 2 | import org.junit.Test 3 | 4 | class TestStart { 5 | @Test fun testOk() { 6 | Assert.assertEquals("OK", start()) 7 | } 8 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task10/Task.kt: -------------------------------------------------------------------------------- 1 | fun Int.r(): RationalNumber = TODO() 2 | fun Pair.r(): RationalNumber = TODO() 3 | 4 | data class RationalNumber(val numerator: Int, val denominator: Int) -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Extensions on collections/Task.kt: -------------------------------------------------------------------------------- 1 | fun getList(): List { 2 | return arrayListOf(1, 5, 2)//TODO("return the list sorted in descending order") 3 | } -------------------------------------------------------------------------------- /Kotlin Koans/Properties/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "sequential": false, 3 | "examples": [ 4 | "Properties", 5 | "Lazy property", 6 | "Delegates examples", 7 | "Delegates how it works" 8 | ] 9 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Properties/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "sequential": false, 3 | "examples": [ 4 | "Properties", 5 | "Lazy property", 6 | "Delegates examples", 7 | "Delegates how it works" 8 | ] 9 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Introduction/Solution.kt: -------------------------------------------------------------------------------- 1 | package collections.introduction 2 | 3 | import collections.* 4 | 5 | fun Shop.getSetOfCustomers(): Set = /**/customers.toSet()/**/ -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task11/Task.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | fun getList(): List { 4 | val arrayList = arrayListOf(1, 5, 2) 5 | Collections.sort(arrayList, object {}) 6 | return arrayList 7 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Hello, world!/Test.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Assert 2 | import org.junit.Test 3 | 4 | class TestStart { 5 | @Test fun testOk() { 6 | Assert.assertEquals("OK", start()) 7 | } 8 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Fold/Task.kt.txt: -------------------------------------------------------------------------------- 1 | // Return the set of products that were ordered by every customer 2 | fun Shop.getSetOfProductsOrderedByEveryCustomer(): Set { 3 | TODO() 4 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Sort/Task.kt.txt: -------------------------------------------------------------------------------- 1 | // Return a list of customers, sorted by the ascending number of orders they made 2 | fun Shop.getCustomersSortedByNumberOfOrders(): List = TODO() -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson2/task1/Task.kt: -------------------------------------------------------------------------------- 1 | data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable { 2 | /* TODO */ 3 | } 4 | 5 | fun compare(date1: MyDate, date2: MyDate) = date1 < date2 -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Builders/Builders how it works/Solution.kt: -------------------------------------------------------------------------------- 1 | import Answer.* 2 | 3 | enum class Answer { a, b, c } 4 | 5 | val answers = mapOf( 6 | 1 to c, 2 to b, 3 to b, 4 to c 7 | ) -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Fold/Task.kt: -------------------------------------------------------------------------------- 1 | // Return the set of products that were ordered by every customer 2 | fun Shop.getSetOfProductsOrderedByEveryCustomer(): Set { 3 | TODO() 4 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Sort/Task.kt: -------------------------------------------------------------------------------- 1 | // Return a list of customers, sorted by the ascending number of orders they made 2 | fun Shop.getCustomersSortedByNumberOfOrders(): List = TODO() -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/Comparison/Task.kt: -------------------------------------------------------------------------------- 1 | data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable { 2 | 3 | } 4 | 5 | fun compare(date1: MyDate, date2: MyDate) = date1 < date2 -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Properties/Lazy property/Task.kt: -------------------------------------------------------------------------------- 1 | class LazyProperty(val initializer: () -> Int) { 2 | 3 | val lazy: Int 4 | get() { 5 | TODO() 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Partition/Task.kt.txt: -------------------------------------------------------------------------------- 1 | // Return customers who have more undelivered orders than delivered 2 | fun Shop.getCustomersWithMoreUndeliveredOrdersThanDelivered(): Set = TODO() -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/Destructuring declarations/task.md: -------------------------------------------------------------------------------- 1 | ## Destructuring declarations 2 | 3 | Read about [destructuring declarations](destructuring_declarations) 4 | and make the following code compile by adding one word. -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Named arguments/Solution.kt: -------------------------------------------------------------------------------- 1 | package introduction.named_arguments 2 | 3 | fun joinOptions(options: Collection) = options.joinToString(/**/prefix = "[", postfix = "]"/**/) -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task12/Task.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | fun getList(): List { 4 | val arrayList = arrayListOf(1, 5, 2) 5 | Collections.sort(arrayList, { x, y -> TODO() }) 6 | return arrayList 7 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Builders/Html builders/main.kt: -------------------------------------------------------------------------------- 1 | import kotlin.browser.document 2 | 3 | fun main(args: Array){ 4 | document.body!!.style.overflowY = "" 5 | document.body!!.innerHTML = renderProductTable() 6 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Partition/Task.kt: -------------------------------------------------------------------------------- 1 | // Return customers who have more undelivered orders than delivered 2 | fun Shop.getCustomersWithMoreUndeliveredOrdersThanDelivered(): Set = TODO() -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Data classes/Solution.kt: -------------------------------------------------------------------------------- 1 | data class Person(val name: String, val age: Int) 2 | 3 | fun getPeople(): List { 4 | return listOf(Person("Alice", 29), Person("Bob", 31)) 5 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Compound tasks/task.md: -------------------------------------------------------------------------------- 1 | ## Compound tasks 2 | 3 | Implement `Customer.getMostExpensiveDeliveredProduct()` and 4 | `Shop.getNumberOfTimesProductWasOrdered()` using functions from the Kotlin standard library. -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Compound tasks/task.md: -------------------------------------------------------------------------------- 1 | ## Compound tasks 2 | 3 | Implement `Customer.getMostExpensiveDeliveredProduct()` and 4 | `Shop.getNumberOfTimesProductWasOrdered()` using functions from the Kotlin standard library. -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Properties/Delegates examples/task.md: -------------------------------------------------------------------------------- 1 | ## Delegates example 2 | 3 | Read about [delegated properties](http://kotlinlang.org/docs/reference/delegated-properties.html) 4 | and make the property lazy by using delegates. -------------------------------------------------------------------------------- /Kotlin Koans/Properties/Delegates examples/Solution.kt: -------------------------------------------------------------------------------- 1 | package properties.delegates_examples 2 | 3 | class LazyProperty(val initializer: () -> Int) { 4 | val lazyValue: Int by /**/lazy(initializer)/**/ 5 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task7/Task.kt: -------------------------------------------------------------------------------- 1 | // Return the sum of prices of all products that a customer has ordered. 2 | // Note: the customer may order the same product for several times. 3 | fun Customer.getTotalOrderPrice(): Double = TODO() -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson4/task3/task.html: -------------------------------------------------------------------------------- 1 |

Delegates example

2 |

Read about delegated properties and make the property lazy by using delegates.

-------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Builders/Builders how it works/Task.kt: -------------------------------------------------------------------------------- 1 | import Answer.* 2 | 3 | enum class Answer { a, b, c } 4 | 5 | val answers = mapOf( 6 | 1 to null, 2 to null, 3 to null, 4 to null 7 | ) -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/In range/Task.kt: -------------------------------------------------------------------------------- 1 | class DateRange(val start: MyDate, val endInclusive: MyDate) 2 | 3 | fun checkInRange(date: MyDate, first: MyDate, last: MyDate): Boolean { 4 | return date in DateRange(first, last) 5 | } -------------------------------------------------------------------------------- /Kotlin Koans/Builders/Builders how it works/Task.kt.txt: -------------------------------------------------------------------------------- 1 | import Answer.* 2 | 3 | enum class Answer { a, b, c } 4 | 5 | val answers = mapOf( 6 | 1 to null, 2 to null, 3 to null, 4 to null 7 | ) -------------------------------------------------------------------------------- /Kotlin Koans/Collections/GroupBy/Solution.kt: -------------------------------------------------------------------------------- 1 | package collections.groupby 2 | 3 | import collections.* 4 | 5 | fun Shop.groupCustomersByCity(): Map> = 6 | /**/customers.groupBy { it.city }/**/ -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Sum/Solution.kt: -------------------------------------------------------------------------------- 1 | package collections.sum 2 | 3 | import collections.* 4 | 5 | fun Customer.getTotalOrderPrice(): Double = 6 | /**/orders.flatMap { it.products }.sumByDouble { it.price }/**/ -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task13/tests.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Test 2 | import org.junit.Assert 3 | 4 | class TestExtensionsOnCollections { 5 | @Test fun testSort() { 6 | Assert.assertEquals(listOf(5, 2, 1), getList()) 7 | } 8 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson2/task2/Task.kt: -------------------------------------------------------------------------------- 1 | class DateRange(val start: MyDate, val endInclusive: MyDate)/* TODO */ 2 | 3 | fun checkInRange(date: MyDate, first: MyDate, last: MyDate): Boolean { 4 | return date in DateRange(first, last) 5 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Properties/Properties/Solution.kt: -------------------------------------------------------------------------------- 1 | class PropertyExample() { 2 | var counter = 0 3 | var propertyWithCounter: Int? = null 4 | set(v: Int?) { 5 | field = v 6 | counter++ 7 | } 8 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Sort/Solution.kt: -------------------------------------------------------------------------------- 1 | package collections.sort 2 | 3 | import collections.* 4 | 5 | fun Shop.getCustomersSortedByNumberOfOrders(): List = 6 | /**/customers.sortedBy { it.orders.size }/**/ -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson5/task1/Task.kt: -------------------------------------------------------------------------------- 1 | fun task(): List { 2 | val isEven: Int.() -> Boolean = { TODO() } 3 | val isOdd: Int.() -> Boolean = { TODO() } 4 | 5 | return listOf(42.isOdd(), 239.isOdd(), 294823098.isEven()) 6 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Extension functions/task.md: -------------------------------------------------------------------------------- 1 | ## Extension functions 2 | 3 | Read about [extension functions](extensions). 4 | Then implement extension functions `Int.r()` and `Pair.r()` and make them convert Int and Pair to `RationalNumber`. -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task11/task.html: -------------------------------------------------------------------------------- 1 |

Compound tasks

2 |

Implement Customer.getMostExpensiveDeliveredProduct() and Shop.getNumberOfTimesProductWasOrdered() using functions from the Kotlin standard library.

-------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Filter map/Solution.kt: -------------------------------------------------------------------------------- 1 | fun Shop.getCitiesCustomersAreFrom(): Set = 2 | customers.map { it.city }.toSet() 3 | 4 | fun Shop.getCustomersFrom(city: City): List = 5 | customers.filter { it.city == city } -------------------------------------------------------------------------------- /Kotlin Koans/Builders/The function apply/task.md: -------------------------------------------------------------------------------- 1 | ## The function apply 2 | 3 | The previous examples can be rewritten using the library function 4 | [`apply`](apply) (see examples below). 5 | Write your own implementation of this function named 'myApply'. -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/Comparison/Task.kt.txt: -------------------------------------------------------------------------------- 1 | data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable { 2 | /* TODO */ 3 | } 4 | 5 | fun compare(date1: MyDate, date2: MyDate) = date1 < date2 -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "examples": [ 3 | "Comparison", 4 | "In range", 5 | "Range to", 6 | "For loop", 7 | "Operators overloading", 8 | "Destructuring declarations", 9 | "Invoke" 10 | ] 11 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Object expressions/Task.kt.txt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | fun getList(): List { 4 | val arrayList = arrayListOf(1, 5, 2) 5 | Collections.sort(arrayList, object {}) 6 | return arrayList 7 | } -------------------------------------------------------------------------------- /Kotlin Koans/Properties/Lazy property/Task.kt.txt: -------------------------------------------------------------------------------- 1 | class LazyProperty(val initializer: () -> Int) { 2 | /* TODO */ 3 | val lazy: Int 4 | get() { 5 | TODO() 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson2/task6/task.html: -------------------------------------------------------------------------------- 1 |

Destructuring declarations

2 |

Read about destructuring declarations and make the following code compile by adding one word.

-------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "examples": [ 3 | "Comparison", 4 | "In range", 5 | "Range to", 6 | "For loop", 7 | "Operators overloading", 8 | "Destructuring declarations", 9 | "Invoke" 10 | ] 11 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Object expressions/Task.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | fun getList(): List { 4 | val arrayList = arrayListOf(1, 5, 2) 5 | Collections.sort(arrayList, object {}) 6 | return arrayList 7 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/SAM conversions/Solution.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | fun getList(): List { 4 | val arrayList = arrayListOf(1, 5, 2) 5 | Collections.sort(arrayList, { x, y -> y - x }) 6 | return arrayList 7 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Extensions on collections/Solution.kt: -------------------------------------------------------------------------------- 1 | package introduction.extensions_on_collections 2 | 3 | fun getList(): List { 4 | return /**/arrayListOf(1, 5, 2)/**/.sortedDescending()/**/ 5 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Strings/Solution.kt: -------------------------------------------------------------------------------- 1 | package introduction.strings 2 | 3 | val month = "(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)" 4 | 5 | /**/fun getPattern(): String = /**/"""\d{2} ${month} \d{4}"""/**/ -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson2/task7/Task.kt: -------------------------------------------------------------------------------- 1 | class Invokable { 2 | var numberOfInvocations: Int = 0 3 | private set 4 | operator fun invoke(): Invokable { 5 | TODO() 6 | } 7 | } 8 | 9 | fun invokeTwice(invokable: Invokable) = invokable()() -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Sum/Task.kt: -------------------------------------------------------------------------------- 1 | // Return the sum of prices of all products that a customer has ordered. 2 | // Note: the customer may order the same product for several times. 3 | fun Customer.getTotalOrderPrice(): Double = TODO() -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/Destructuring declarations/task.md: -------------------------------------------------------------------------------- 1 | ## Destructuring declarations 2 | 3 | Read about [destructuring declarations](http://kotlinlang.org/docs/reference/multi-declarations.html) 4 | and make the following code compile by adding one word. -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Extensions on collections/Test.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Test 2 | import org.junit.Assert 3 | 4 | class TestExtensionsOnCollections { 5 | @Test fun testSort() { 6 | Assert.assertEquals(listOf(5, 2, 1), getList()) 7 | } 8 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/SAM conversions/Task.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | fun getList(): List { 4 | val arrayList = arrayListOf(1, 5, 2) 5 | Collections.sort(arrayList, { x, y -> TODO() }) 6 | return arrayList 7 | } -------------------------------------------------------------------------------- /Kotlin Koans/Builders/Html builders/main-wb.kt.txt: -------------------------------------------------------------------------------- 1 | package builders.html_builders 2 | 3 | import kotlin.browser.document 4 | 5 | fun main(args: Array){ 6 | document.body!!.style.overflowY = "" 7 | document.body!!.innerHTML = renderProductTable() 8 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Sum/Task.kt.txt: -------------------------------------------------------------------------------- 1 | // Return the sum of prices of all products that a customer has ordered. 2 | // Note: the customer may order the same product for several times. 3 | fun Customer.getTotalOrderPrice(): Double = TODO() -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/In range/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit", 3 | "files": [ 4 | { 5 | "modifiable": false, 6 | "hidden": false, 7 | "skipInTestVersion": false, 8 | "filename": "MyDate.kt" 9 | } 10 | ] 11 | } -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/Range to/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit", 3 | "files": [ 4 | { 5 | "modifiable": false, 6 | "hidden": false, 7 | "skipInTestVersion": false, 8 | "filename": "MyDate.kt" 9 | } 10 | ] 11 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Hello, world!/Test.kt: -------------------------------------------------------------------------------- 1 | package introduction.hello_world 2 | 3 | import org.junit.Assert 4 | import org.junit.Test 5 | 6 | class TestStart { 7 | @Test fun testOk() { 8 | Assert.assertEquals("OK", start()) 9 | } 10 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/SAM conversions/Task.kt.txt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | fun getList(): List { 4 | val arrayList = arrayListOf(1, 5, 2) 5 | Collections.sort(arrayList, { x, y -> TODO() }) 6 | return arrayList 7 | } -------------------------------------------------------------------------------- /Kotlin Koans/Builders/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "sequential": false, 3 | "examples": [ 4 | "Function literals with receiver", 5 | "String and map builders", 6 | "The function apply", 7 | "Html builders", 8 | "Builders how it works" 9 | ] 10 | } -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/In range/Task.kt.txt: -------------------------------------------------------------------------------- 1 | class DateRange(val start: MyDate, val endInclusive: MyDate)/* TODO */ 2 | 3 | fun checkInRange(date: MyDate, first: MyDate, last: MyDate): Boolean { 4 | return date in DateRange(first, last) 5 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Extension functions/Task.kt.txt: -------------------------------------------------------------------------------- 1 | fun Int.r(): RationalNumber = TODO() 2 | fun Pair.r(): RationalNumber = TODO() 3 | 4 | data class RationalNumber(val numerator: Int, val denominator: Int) -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/In range/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit", 3 | "files": [ 4 | { 5 | "modifiable": false, 6 | "hidden": false, 7 | "skipInTestVersion": false, 8 | "filename": "MyDate.kt" 9 | } 10 | ] 11 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/Range to/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit", 3 | "files": [ 4 | { 5 | "modifiable": false, 6 | "hidden": false, 7 | "skipInTestVersion": false, 8 | "filename": "MyDate.kt" 9 | } 10 | ] 11 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Extension functions/Task.kt: -------------------------------------------------------------------------------- 1 | fun Int.r(): RationalNumber = TODO() 2 | fun Pair.r(): RationalNumber = TODO() 3 | 4 | data class RationalNumber(val numerator: Int, val denominator: Int) -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson5/task2/Task.kt: -------------------------------------------------------------------------------- 1 | import java.util.HashMap 2 | 3 | /* TODO */ 4 | 5 | fun usage(): Map { 6 | return buildMap { 7 | put(0, "0") 8 | for (i in 1..10) { 9 | put(i, "$i") 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Builders/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "sequential": false, 3 | "examples": [ 4 | "Function literals with receiver", 5 | "String and map builders", 6 | "The function apply", 7 | "Html builders", 8 | "Builders how it works" 9 | ] 10 | } -------------------------------------------------------------------------------- /Kotlin Koans/Properties/Delegates how it works/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit", 3 | "files": [ 4 | { 5 | "modifiable": false, 6 | "hidden": false, 7 | "skipInTestVersion": false, 8 | "filename": "MyDate.kt" 9 | } 10 | ] 11 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task4/tests.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Test 2 | import org.junit.Assert 3 | 4 | class TestDefaultAndNamedParams() { 5 | 6 | @Test fun testDefaultAndNamedParams() { 7 | Assert.assertEquals(listOf("a42", "b1", "C42", "D2"), useFoo()) 8 | } 9 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson2/task4/Task.kt: -------------------------------------------------------------------------------- 1 | class DateRange(val start: MyDate, val end: MyDate) 2 | 3 | fun iterateOverDateRange(firstDate: MyDate, secondDate: MyDate, handler: (MyDate) -> Unit) { 4 | for (date in firstDate..secondDate) { 5 | handler(date) 6 | } 7 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Builders/String and map builders/Task.kt: -------------------------------------------------------------------------------- 1 | import java.util.HashMap 2 | 3 | 4 | 5 | fun usage(): Map { 6 | return buildMap { 7 | put(0, "0") 8 | for (i in 1..10) { 9 | put(i, "$i") 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Max min/Solution.kt: -------------------------------------------------------------------------------- 1 | fun Shop.getCustomerWithMaximumNumberOfOrders(): Customer? = 2 | customers.maxBy { it.orders.size } 3 | 4 | fun Customer.getMostExpensiveOrderedProduct(): Product? = 5 | orders.flatMap { it.products }.maxBy { it.price } 6 | -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Partition/Solution.kt: -------------------------------------------------------------------------------- 1 | fun Shop.getCustomersWithMoreUndeliveredOrdersThanDelivered(): Set = customers.filter { 2 | val (delivered, undelivered) = it.orders.partition { it.isDelivered } 3 | undelivered.size > delivered.size 4 | }.toSet() -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Extension functions/Solution.kt: -------------------------------------------------------------------------------- 1 | fun Int.r(): RationalNumber = RationalNumber(this, 1) 2 | fun Pair.r(): RationalNumber = RationalNumber(first, second) 3 | 4 | data class RationalNumber(val numerator: Int, val denominator: Int) -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Properties/Delegates how it works/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit", 3 | "files": [ 4 | { 5 | "modifiable": false, 6 | "hidden": false, 7 | "skipInTestVersion": false, 8 | "filename": "MyDate.kt" 9 | } 10 | ] 11 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task2/Task.kt: -------------------------------------------------------------------------------- 1 | // Return the set of cities the customers are from 2 | fun Shop.getCitiesCustomersAreFrom(): Set = TODO() 3 | 4 | // Return a list of the customers who live in the given city 5 | fun Shop.getCustomersFrom(city: City): List = TODO() -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/FlatMap/Solution.kt: -------------------------------------------------------------------------------- 1 | val Customer.orderedProducts: Set get() { 2 | return orders.flatMap { it.products }.toSet() 3 | } 4 | 5 | val Shop.allOrderedProducts: Set get() { 6 | return customers.flatMap { it.orderedProducts }.toSet() 7 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Fold/task.md: -------------------------------------------------------------------------------- 1 | ## Fold 2 | 3 | Implement `Shop.getSetOfProductsOrderedByEveryCustomer()` using 4 | [`fold`](fold). 5 | 6 | ```kotlin 7 | listOf(1, 2, 3, 4).fold(1, { 8 | partProduct, element -> 9 | element * partProduct 10 | }) == 24 11 | ``` 12 | -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/Invoke/Task.kt.txt: -------------------------------------------------------------------------------- 1 | class Invokable { 2 | var numberOfInvocations: Int = 0 3 | private set 4 | operator fun invoke(): Invokable { 5 | TODO() 6 | } 7 | } 8 | 9 | fun invokeTwice(invokable: Invokable) = invokable()() -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/Invoke/Task.kt: -------------------------------------------------------------------------------- 1 | class Invokable { 2 | var numberOfInvocations: Int = 0 3 | private set 4 | operator fun invoke(): Invokable { 5 | TODO() 6 | } 7 | } 8 | 9 | fun invokeTwice(invokable: Invokable) = invokable()() -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Default arguments/Test.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Test 2 | import org.junit.Assert 3 | 4 | class TestDefaultAndNamedParams() { 5 | 6 | @Test fun testDefaultAndNamedParams() { 7 | Assert.assertEquals(listOf("a42", "b1", "C42", "D2"), useFoo()) 8 | } 9 | } -------------------------------------------------------------------------------- /Kotlin Koans/Properties/Properties/task.md: -------------------------------------------------------------------------------- 1 | ## Properties 2 | 3 | Read about [properties](properties_and_fields) in Kotlin. 4 | 5 | Add a custom setter to PropertyExample.propertyWithCounter so that 6 | the `counter` property is incremented every time `propertyWithCounter` is assigned to. 7 | -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task7/tests.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Test 2 | import org.junit.Assert 3 | 4 | 5 | class TestDataClasses { 6 | @Test fun testListOfPeople() { 7 | Assert.assertEquals("[Person(name=Alice, age=29), Person(name=Bob, age=31)]", getPeople().toString()) 8 | } 9 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Extension functions/task.md: -------------------------------------------------------------------------------- 1 | ## Extension functions 2 | 3 | Read about [extension functions](http://kotlinlang.org/docs/reference/extensions.html). 4 | Then implement extension functions `Int.r()` and `Pair.r()` and make them convert Int and Pair to `RationalNumber`. -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Data classes/Solution.kt: -------------------------------------------------------------------------------- 1 | package introduction.data_classes 2 | 3 | /**/data class Person(val name: String, val age: Int)/**/ 4 | 5 | fun getPeople(): List { 6 | return listOf(Person("Alice", 29), Person("Bob", 31)) 7 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Builders/Function literals with receiver/Solution.kt: -------------------------------------------------------------------------------- 1 | fun task(): List { 2 | 3 | val isEven: Int.() -> Boolean = { this % 2 == 0 } 4 | val isOdd: Int.() -> Boolean = { this % 2 != 0 } 5 | 6 | return listOf(42.isOdd(), 239.isOdd(), 294823098.isEven()) 7 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/FlatMap/task.md: -------------------------------------------------------------------------------- 1 | ## FlatMap 2 | 3 | Implement `Customer.getOrderedProducts()` and `Shop.getAllOrderedProducts()` using 4 | [`flatMap`](flat-map). 5 | 6 | ```kotlin 7 | val result = listOf("abc", "12").flatMap { it.toCharList() } 8 | result == listOf('a', 'b', 'c', '1', '2') 9 | ``` -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Data classes/Test.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Test 2 | import org.junit.Assert 3 | 4 | 5 | class TestDataClasses { 6 | @Test fun testListOfPeople() { 7 | Assert.assertEquals("[Person(name=Alice, age=29), Person(name=Bob, age=31)]", getPeople().toString()) 8 | } 9 | } -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/For loop/Task.kt.txt: -------------------------------------------------------------------------------- 1 | class DateRange(val start: MyDate, val end: MyDate) 2 | 3 | fun iterateOverDateRange(firstDate: MyDate, secondDate: MyDate, handler: (MyDate) -> Unit) { 4 | for (date in firstDate..secondDate) { 5 | handler(date) 6 | } 7 | } -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task4/Task.kt: -------------------------------------------------------------------------------- 1 | // Return all products this customer has ordered 2 | val Customer.orderedProducts: Set get() { 3 | TODO() 4 | } 5 | 6 | // Return all products that were ordered by at least one customer 7 | val Shop.allOrderedProducts: Set get() { 8 | TODO() 9 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/For loop/Task.kt: -------------------------------------------------------------------------------- 1 | class DateRange(val start: MyDate, val end: MyDate) 2 | 3 | fun iterateOverDateRange(firstDate: MyDate, secondDate: MyDate, handler: (MyDate) -> Unit) { 4 | for (date in firstDate..secondDate) { 5 | handler(date) 6 | } 7 | } -------------------------------------------------------------------------------- /Kotlin Koans/Builders/Function literals with receiver/task.md: -------------------------------------------------------------------------------- 1 | ## Extension function literals 2 | 3 | Read about [function literals with receiver](function-literals-with-receiver). 4 | 5 | You can declare `isEven` and `isOdd` as values, that can be called as extension functions. 6 | Complete the declarations below. -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Builders/The function apply/task.md: -------------------------------------------------------------------------------- 1 | ## The function apply 2 | 3 | The previous examples can be rewritten using the library function 4 | [`apply`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/apply.html) (see examples below). 5 | Write your own implementation of this function named 'myApply'. -------------------------------------------------------------------------------- /Kotlin Koans/Builders/Function literals with receiver/Task.kt.txt: -------------------------------------------------------------------------------- 1 | fun task(): List { 2 | val isEven: Int.() -> Boolean = { TODO() } 3 | val isOdd: Int.() -> Boolean = { TODO() } 4 | 5 | return listOf(42.isOdd(), 239.isOdd(), 294823098.isEven()) 6 | } -------------------------------------------------------------------------------- /Kotlin Koans/Builders/String and map builders/Task.kt.txt: -------------------------------------------------------------------------------- 1 | import java.util.HashMap 2 | 3 | /* TODO */ 4 | 5 | fun usage(): Map { 6 | return buildMap { 7 | put(0, "0") 8 | for (i in 1..10) { 9 | put(i, "$i") 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/SAM conversions/task.md: -------------------------------------------------------------------------------- 1 | ## SAM conversions 2 | 3 | When an object implements a SAM interface (one with a Single Abstract Method), 4 | you can pass a lambda instead. 5 | Read more about [SAM-conversions](sam-conversions). 6 | 7 | In the previous example change an object expression to a lambda. -------------------------------------------------------------------------------- /Kotlin Koans/Properties/Properties/Solution.kt: -------------------------------------------------------------------------------- 1 | package properties.properties 2 | 3 | class PropertyExample() { 4 | var counter = 0 5 | var propertyWithCounter: Int? = null 6 | /**/set(v: Int?) { 7 | field = v 8 | counter++ 9 | }/**/ 10 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task11/tests.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Test 2 | import org.junit.Assert 3 | import koans.util.toMessageInEquals 4 | 5 | class TestObjectExpressions { 6 | @Test fun testSort() { 7 | Assert.assertEquals("getList".toMessageInEquals(), listOf(5, 2, 1), getList()) 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task12/tests.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Test 2 | import org.junit.Assert 3 | import koans.util.toMessageInEquals 4 | 5 | class TestSamConversions { 6 | @Test fun testSort() { 7 | Assert.assertEquals("getList".toMessageInEquals(), listOf(5, 2, 1), getList()) 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson2/task3/Task.kt: -------------------------------------------------------------------------------- 1 | operator fun MyDate.rangeTo(other: MyDate) = TODO() 2 | 3 | class DateRange(override val start: MyDate, override val endInclusive: MyDate): ClosedRange 4 | 5 | fun checkInRange(date: MyDate, first: MyDate, last: MyDate): Boolean { 6 | return date in first..last 7 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson2/task6/Task.kt: -------------------------------------------------------------------------------- 1 | /* TODO */class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) 2 | 3 | fun isLeapDay(date: MyDate): Boolean { 4 | 5 | val (year, month, dayOfMonth) = date 6 | 7 | // 29 February of a leap year 8 | return year % 4 == 0 && month == 2 && dayOfMonth == 29 9 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task5/Task.kt: -------------------------------------------------------------------------------- 1 | // Return a customer whose order count is the highest among all customers 2 | fun Shop.getCustomerWithMaximumNumberOfOrders(): Customer? = TODO() 3 | 4 | // Return the most expensive product which has been ordered 5 | fun Customer.getMostExpensiveOrderedProduct(): Product? = TODO() -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Builders/Function literals with receiver/Task.kt: -------------------------------------------------------------------------------- 1 | fun task(): List { 2 | val isEven: Int.() -> Boolean = { TODO() } 3 | val isOdd: Int.() -> Boolean = { TODO() } 4 | 5 | return listOf(42.isOdd(), 239.isOdd(), 294823098.isEven()) 6 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/GroupBy/task.md: -------------------------------------------------------------------------------- 1 | ## Group By 2 | 3 | Implement `Shop.groupCustomersByCity()` using 4 | [`groupBy`](group-by). 5 | 6 | ```kotlin 7 | val result = listOf("a", "b", "ba", "ccc", "ad").groupBy { it.length() } 8 | result == mapOf(1 to listOf("a", "b"), 2 to listOf("ba", "ad"), 3 to listOf("ccc")) 9 | ``` -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Extensions on collections/Test.kt: -------------------------------------------------------------------------------- 1 | package introduction.extensions_on_collections 2 | 3 | import org.junit.Test 4 | import org.junit.Assert 5 | 6 | class TestExtensionsOnCollections { 7 | @Test fun testSort() { 8 | Assert.assertEquals(listOf(5, 2, 1), getList()) 9 | } 10 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson5/task3/task.html: -------------------------------------------------------------------------------- 1 |

The function apply

2 |

The previous examples can be rewritten using the library function apply (see examples below). Write your own implementation of this function named 'myApply'.

-------------------------------------------------------------------------------- /Kotlin Koans/Builders/Builders how it works/Solution.kt: -------------------------------------------------------------------------------- 1 | package builders.builders_how_it_works 2 | 3 | import builders.builders_how_it_works.Answer.* 4 | 5 | enum class Answer { a, b, c } 6 | 7 | val answers = mapOf( 8 | /**/1 to c, 2 to b, 3 to b, 4 to c/**/ 9 | ) -------------------------------------------------------------------------------- /Kotlin Koans/Properties/Lazy property/task.md: -------------------------------------------------------------------------------- 1 | ## Lazy property 2 | 3 | Add a custom getter to make the 'lazy' val really lazy. 4 | It should be initialized by the invocation of 'initializer()' at the moment of the first access. 5 | 6 | You can add as many additional properties as you need. 7 | 8 | Do not use delegated properties! -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task10/task.html: -------------------------------------------------------------------------------- 1 |

Extension functions

2 |

Read about extension functions. Then implement extension functions Int.r() and Pair.r() and make them convert Int and Pair to RationalNumber.

-------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task2/tests.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Test 2 | import org.junit.Assert 3 | import koans.util.toMessageInEquals 4 | 5 | class TestFunctions() { 6 | @Test fun collection() { 7 | Assert.assertEquals("toJSON".toMessageInEquals(), "[1, 2, 3, 42, 555]", toJSON(listOf(1, 2, 3, 42, 555))) 8 | } 9 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/SAM conversions/Test.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Test 2 | import org.junit.Assert 3 | import koans.util.toMessageInEquals 4 | 5 | class TestSamConversions { 6 | @Test fun testSort() { 7 | Assert.assertEquals("getList".toMessageInEquals(), listOf(5, 2, 1), getList()) 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson4/task2/task.html: -------------------------------------------------------------------------------- 1 |

Lazy property

2 |

Add a custom getter to make the 'lazy' val really lazy. It should be initialized by the invocation of 'initializer()' at the moment of the first access.

3 |

You can add as many additional properties as you need.

4 |

Do not use delegated properties!

-------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/Destructuring declarations/Task.kt: -------------------------------------------------------------------------------- 1 | class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) 2 | 3 | fun isLeapDay(date: MyDate): Boolean { 4 | 5 | val (year, month, dayOfMonth) = date 6 | 7 | // 29 February of a leap year 8 | return year % 4 == 0 && month == 2 && dayOfMonth == 29 9 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/Invoke/Solution.kt: -------------------------------------------------------------------------------- 1 | class Invokable { 2 | var numberOfInvocations: Int = 0 3 | private set 4 | operator fun invoke(): Invokable { 5 | numberOfInvocations++ 6 | return this 7 | } 8 | } 9 | 10 | fun invokeTwice(invokable: Invokable) = invokable()() -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Object expressions/Test.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Test 2 | import org.junit.Assert 3 | import koans.util.toMessageInEquals 4 | 5 | class TestObjectExpressions { 6 | @Test fun testSort() { 7 | Assert.assertEquals("getList".toMessageInEquals(), listOf(5, 2, 1), getList()) 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Properties/Lazy property/task.md: -------------------------------------------------------------------------------- 1 | ## Lazy property 2 | 3 | Add a custom getter to make the 'lazy' val really lazy. 4 | It should be initialized by the invocation of 'initializer()' at the moment of the first access. 5 | 6 | You can add as many additional properties as you need. 7 | 8 | Do not use delegated properties! -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/SAM conversions/Solution.kt: -------------------------------------------------------------------------------- 1 | package introduction.sam_conversions 2 | 3 | import java.util.* 4 | 5 | fun getList(): List { 6 | val arrayList = arrayListOf(1, 5, 2) 7 | /**/Collections.sort(arrayList, { x, y -> /**/y - x/**/ })/**/ 8 | return arrayList 9 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson2/task6/tests.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Assert 2 | import org.junit.Test 3 | 4 | class TestMultiAssignment { 5 | @Test fun testIsLeapDay() { 6 | Assert.assertTrue("The test failed", isLeapDay(MyDate(2016, 2, 29))) 7 | Assert.assertFalse("The test failed", isLeapDay(MyDate(2015, 2, 29))) 8 | } 9 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Filter map/Task.kt.txt: -------------------------------------------------------------------------------- 1 | // Return the set of cities the customers are from 2 | fun Shop.getCitiesCustomersAreFrom(): Set = TODO() 3 | 4 | // Return a list of the customers who live in the given city 5 | fun Shop.getCustomersFrom(city: City): List = TODO() -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/For loop/task.md: -------------------------------------------------------------------------------- 1 | ## For loop 2 | 3 | Kotlin [for loop](for_loop) 4 | iterates through anything that provides an iterator. 5 | Make the class `DateRange` implement [`Iterable`](iterable), 6 | so that it could be iterated over. 7 | You can use the function `MyDate.nextDay()` defined in [DateUtil.kt](date_util_file1) -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Filter map/Task.kt: -------------------------------------------------------------------------------- 1 | // Return the set of cities the customers are from 2 | fun Shop.getCitiesCustomersAreFrom(): Set = TODO() 3 | 4 | // Return a list of the customers who live in the given city 5 | fun Shop.getCustomersFrom(city: City): List = TODO() -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Object expressions/Solution.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | fun getList(): List { 4 | val arrayList = arrayListOf(1, 5, 2) 5 | 6 | Collections.sort(arrayList, object : Comparator { 7 | override fun compare(x: Int, y: Int) = y - x 8 | }) 9 | 10 | return arrayList 11 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Properties/Lazy property/Solution.kt: -------------------------------------------------------------------------------- 1 | class LazyProperty(val initializer: () -> Int) { 2 | var value: Int? = null 3 | val lazy: Int 4 | get() { 5 | if (value == null) { 6 | value = initializer() 7 | } 8 | return value!! 9 | } 10 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Sort/task.md: -------------------------------------------------------------------------------- 1 | ## Sort 2 | 3 | Implement `Shop.getCustomersSortedByNumberOfOrders()` using 4 | [`sorted`](sorted) or 5 | [`sortedBy`](sorted-by). 6 | 7 | ```kotlin 8 | listOf("bbb", "a", "cc").sorted() == listOf("a", "bbb", "cc") 9 | listOf("bbb", "a", "cc").sortedBy { it.length } == listOf("a", "cc", "bbb") 10 | ``` -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/Range to/Task.kt.txt: -------------------------------------------------------------------------------- 1 | operator fun MyDate.rangeTo(other: MyDate) = TODO() 2 | 3 | class DateRange(override val start: MyDate, override val endInclusive: MyDate): ClosedRange 4 | 5 | fun checkInRange(date: MyDate, first: MyDate, last: MyDate): Boolean { 6 | return date in first..last 7 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Default arguments/Test.kt: -------------------------------------------------------------------------------- 1 | package introduction.default_arguments 2 | 3 | import org.junit.Test 4 | import org.junit.Assert 5 | 6 | class TestDefaultAndNamedParams() { 7 | 8 | @Test fun testDefaultAndNamedParams() { 9 | Assert.assertEquals(listOf("a42", "b1", "C42", "D2"), useFoo()) 10 | } 11 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task8/Task.kt: -------------------------------------------------------------------------------- 1 | fun sendMessageToClient( 2 | client: Client?, message: String?, mailer: Mailer 3 | ){ 4 | TODO() 5 | } 6 | 7 | class Client (val personalInfo: PersonalInfo?) 8 | class PersonalInfo (val email: String?) 9 | interface Mailer { 10 | fun sendMessage(email: String, message: String) 11 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/Range to/Task.kt: -------------------------------------------------------------------------------- 1 | operator fun MyDate.rangeTo(other: MyDate) = TODO() 2 | 3 | class DateRange(override val start: MyDate, override val endInclusive: MyDate): ClosedRange 4 | 5 | fun checkInRange(date: MyDate, first: MyDate, last: MyDate): Boolean { 6 | return date in first..last 7 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Java to Kotlin conversion/Test.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Test 2 | import org.junit.Assert 3 | import koans.util.toMessageInEquals 4 | 5 | class TestFunctions() { 6 | @Test fun collection() { 7 | Assert.assertEquals("toJSON".toMessageInEquals(), "[1, 2, 3, 42, 555]", toJSON(listOf(1, 2, 3, 42, 555))) 8 | } 9 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Properties/Properties/task.md: -------------------------------------------------------------------------------- 1 | ## Properties 2 | 3 | Read about [properties](http://kotlinlang.org/docs/reference/properties.html#properties-and-fields) in Kotlin. 4 | 5 | Add a custom setter to PropertyExample.propertyWithCounter so that 6 | the `counter` property is incremented every time `propertyWithCounter` is assigned to. 7 | -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Sum/task.md: -------------------------------------------------------------------------------- 1 | ## Sum 2 | 3 | Implement `Customer.getTotalOrderPrice()` using 4 | [`sum`](sum) or 5 | [`sumBy`](sum-by). 6 | 7 | ```kotlin 8 | listOf(1, 5, 3).sum() == 9 9 | listOf("a", "b", "cc").sumBy { it.length() } == 4 10 | ``` 11 | 12 | If you want to sum the double values, use 13 | [`sumByDouble`](sum-by-double). -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Data classes/Test.kt: -------------------------------------------------------------------------------- 1 | package introduction.data_classes 2 | 3 | import org.junit.Test 4 | import org.junit.Assert 5 | 6 | 7 | class TestDataClasses { 8 | @Test fun testListOfPeople() { 9 | Assert.assertEquals("[Person(name=Alice, age=29), Person(name=Bob, age=31)]", getPeople().toString()) 10 | } 11 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/In range/Solution.kt: -------------------------------------------------------------------------------- 1 | class DateRange(val start: MyDate, val endInclusive: MyDate) { 2 | operator fun contains(item: MyDate): Boolean = start <= item && item <= endInclusive 3 | } 4 | 5 | fun checkInRange(date: MyDate, first: MyDate, last: MyDate): Boolean { 6 | return date in DateRange(first, last) 7 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task4/Task.kt: -------------------------------------------------------------------------------- 1 | fun foo(name: String, number: Int, toUpperCase: Boolean) = 2 | (if (toUpperCase) name.toUpperCase() else name) + number 3 | 4 | fun useFoo() = listOf( 5 | foo("a"), 6 | foo("b", number = 1), 7 | foo("c", toUpperCase = true), 8 | foo(name = "d", number = 2, toUpperCase = true) 9 | ) -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task8/tests.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Assert 2 | import org.junit.Test 3 | import koans.util.toMessage 4 | 5 | class TestGroupBy { 6 | @Test fun testGroupCustomersByCity() { 7 | Assert.assertEquals("groupCustomersByCity".toMessage(), 8 | groupedByCities, shop.groupCustomersByCity()) 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/Destructuring declarations/Solution.kt: -------------------------------------------------------------------------------- 1 | data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) 2 | 3 | fun isLeapDay(date: MyDate): Boolean { 4 | 5 | val (year, month, dayOfMonth) = date 6 | 7 | // 29 February of a leap year 8 | return year % 4 == 0 && month == 2 && dayOfMonth == 29 9 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/Destructuring declarations/Test.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Assert 2 | import org.junit.Test 3 | 4 | class TestMultiAssignment { 5 | @Test fun testIsLeapDay() { 6 | Assert.assertTrue("The test failed", isLeapDay(MyDate(2016, 2, 29))) 7 | Assert.assertFalse("The test failed", isLeapDay(MyDate(2015, 2, 29))) 8 | } 9 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/Range to/Solution.kt: -------------------------------------------------------------------------------- 1 | operator fun MyDate.rangeTo(other: MyDate) = DateRange(this, other) 2 | 3 | class DateRange(override val start: MyDate, override val endInclusive: MyDate): ClosedRange 4 | 5 | fun checkInRange(date: MyDate, first: MyDate, last: MyDate): Boolean { 6 | return date in first..last 7 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/FlatMap/Task.kt.txt: -------------------------------------------------------------------------------- 1 | // Return all products this customer has ordered 2 | val Customer.orderedProducts: Set get() { 3 | TODO() 4 | } 5 | 6 | // Return all products that were ordered by at least one customer 7 | val Shop.allOrderedProducts: Set get() { 8 | TODO() 9 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task1/tests.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Assert 2 | import org.junit.Test 3 | import koans.util.toMessage 4 | 5 | class TestIntroduction { 6 | @Test fun testSetOfCustomers(){ 7 | Assert.assertEquals("getSetOfCustomers".toMessage(), 8 | customers.values.toSet(), shop.getSetOfCustomers()) 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/FlatMap/Task.kt: -------------------------------------------------------------------------------- 1 | // Return all products this customer has ordered 2 | val Customer.orderedProducts: Set get() { 3 | TODO() 4 | } 5 | 6 | // Return all products that were ordered by at least one customer 7 | val Shop.allOrderedProducts: Set get() { 8 | TODO() 9 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/GroupBy/Test.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Assert 2 | import org.junit.Test 3 | import koans.util.toMessage 4 | 5 | class TestGroupBy { 6 | @Test fun testGroupCustomersByCity() { 7 | Assert.assertEquals("groupCustomersByCity".toMessage(), 8 | groupedByCities, shop.groupCustomersByCity()) 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/In range/MyDate.kt: -------------------------------------------------------------------------------- 1 | data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable { 2 | override fun compareTo(other: MyDate) = when { 3 | year != other.year -> year - other.year 4 | month != other.month -> month - other.month 5 | else -> dayOfMonth - other.dayOfMonth 6 | } 7 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/Range to/MyDate.kt: -------------------------------------------------------------------------------- 1 | data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable { 2 | override fun compareTo(other: MyDate) = when { 3 | year != other.year -> year - other.year 4 | month != other.month -> month - other.month 5 | else -> dayOfMonth - other.dayOfMonth 6 | } 7 | } -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/Destructuring declarations/Task.kt.txt: -------------------------------------------------------------------------------- 1 | /* TODO */class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) 2 | 3 | fun isLeapDay(date: MyDate): Boolean { 4 | 5 | val (year, month, dayOfMonth) = date 6 | 7 | // 29 February of a leap year 8 | return year % 4 == 0 && month == 2 && dayOfMonth == 29 9 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Java to Kotlin conversion/task-ee.md: -------------------------------------------------------------------------------- 1 | ## Java to Kotlin conversion 2 | We have a handy tool for Java developers: Java to Kotlin converter. 3 | To become familiar with it, please convert the java function `toJSON` in the file `JavaCode.java`. 4 | You can just copy-paste the code to Kotlin file and agree to automatically convert it to Kotlin. 5 | -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task2/task.html: -------------------------------------------------------------------------------- 1 |

Java to Kotlin conversion

2 |

We have a handy tool for Java developers: Java to Kotlin converter. To become familiar with it, please convert the java function toJSON in the file JavaCode.java. You can just copy-paste the code to Kotlin file and agree to automatically convert it to Kotlin.

-------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task12/Task.kt: -------------------------------------------------------------------------------- 1 | fun doSomethingStrangeWithCollection(collection: Collection): Collection? { 2 | 3 | val groupsByLength = collection. groupBy { s -> TODO() } 4 | 5 | val maximumSizeOfGroup = groupsByLength.values.map { group -> TODO() }.max() 6 | 7 | return groupsByLength.values.firstOrNull { group -> TODO() } 8 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Builders/Function literals with receiver/task.md: -------------------------------------------------------------------------------- 1 | ## Extension function literals 2 | 3 | Read about [function literals with receiver](https://kotlinlang.org/docs/reference/lambdas.html#function-literals-with-receiver). 4 | 5 | You can declare `isEven` and `isOdd` as values, that can be called as extension functions. 6 | Complete the declarations below. -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Introduction/Test.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Assert 2 | import org.junit.Test 3 | import koans.util.toMessage 4 | 5 | class TestIntroduction { 6 | @Test fun testSetOfCustomers(){ 7 | Assert.assertEquals("getSetOfCustomers".toMessage(), 8 | customers.values.toSet(), shop.getSetOfCustomers()) 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Max min/Task.kt.txt: -------------------------------------------------------------------------------- 1 | // Return a customer whose order count is the highest among all customers 2 | fun Shop.getCustomerWithMaximumNumberOfOrders(): Customer? = TODO() 3 | 4 | // Return the most expensive product which has been ordered 5 | fun Customer.getMostExpensiveOrderedProduct(): Product? = TODO() -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/SAM conversions/Test.kt: -------------------------------------------------------------------------------- 1 | package introduction.sam_conversions 2 | 3 | import org.junit.Test 4 | import org.junit.Assert 5 | import koans.util.toMessageInEquals 6 | 7 | class TestSamConversions { 8 | @Test fun testSort() { 9 | Assert.assertEquals("getList".toMessageInEquals(), listOf(5, 2, 1), getList()) 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task3/tests.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Test 2 | import org.junit.Assert 3 | import koans.util.toMessageInEquals 4 | 5 | class TestNamedArguments() { 6 | 7 | @Test fun testJoinToString() { 8 | Assert.assertEquals("joinOptions".toMessageInEquals(), "[yes, no, may be]", joinOptions(listOf("yes", "no", "may be"))) 9 | } 10 | 11 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Fold/task.md: -------------------------------------------------------------------------------- 1 | ## Fold 2 | 3 | Implement `Shop.getSetOfProductsOrderedByEveryCustomer()` using 4 | [`fold`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/kotlin.-iterable/fold.html). 5 | 6 | ```kotlin 7 | listOf(1, 2, 3, 4).fold(1, { 8 | partProduct, element -> 9 | element * partProduct 10 | }) == 24 11 | ``` 12 | -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Max min/Task.kt: -------------------------------------------------------------------------------- 1 | // Return a customer whose order count is the highest among all customers 2 | fun Shop.getCustomerWithMaximumNumberOfOrders(): Customer? = TODO() 3 | 4 | // Return the most expensive product which has been ordered 5 | fun Customer.getMostExpensiveOrderedProduct(): Product? = TODO() -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Max min/task.md: -------------------------------------------------------------------------------- 1 | ## Max; min 2 | 3 | Implement `Shop.getCustomerWithMaximumNumberOfOrders()` and `Customer.getMostExpensiveOrderedProduct()` using 4 | [`max`](max), 5 | [`min`](min), 6 | [`maxBy`](max-by), or 7 | [`minBy`](min-by). 8 | 9 | ```kotlin 10 | listOf(1, 42, 4).max() == 42 11 | listOf("a", "ab").minBy { it.length } == "a" 12 | ``` 13 | -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Partition/Solution.kt: -------------------------------------------------------------------------------- 1 | package collections.partition 2 | 3 | import collections.* 4 | 5 | fun Shop.getCustomersWithMoreUndeliveredOrdersThanDelivered(): Set = /**/customers.filter { 6 | val (delivered, undelivered) = it.orders.partition { it.isDelivered } 7 | undelivered.size > delivered.size 8 | }.toSet()/**/ -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task12/task.html: -------------------------------------------------------------------------------- 1 |

SAM conversions

2 |

When an object implements a SAM interface (one with a Single Abstract Method), you can pass a lambda instead. Read more about SAM-conversions.

3 |

In the previous example change an object expression to a lambda.

-------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task9/Task.kt: -------------------------------------------------------------------------------- 1 | fun eval(expr: Expr): Int = 2 | when (expr) { 3 | is Num -> TODO() 4 | is Sum -> TODO() 5 | else -> throw IllegalArgumentException("Unknown expression") 6 | } 7 | 8 | interface Expr 9 | class Num(val value: Int) : Expr 10 | class Sum(val left: Expr, val right: Expr) : Expr 11 | -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson4/task1/task.html: -------------------------------------------------------------------------------- 1 |

Properties

2 |

Read about properties in Kotlin.

3 |

Add a custom setter to PropertyExample.propertyWithCounter so that the counter property is incremented every time propertyWithCounter is assigned to.

-------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Fold/Solution.kt: -------------------------------------------------------------------------------- 1 | fun Shop.getSetOfProductsOrderedByEveryCustomer(): Set { 2 | val allProducts = customers.flatMap { it.orders.flatMap { it.products }}.toSet() 3 | return customers.fold(allProducts, { 4 | orderedByAll, customer -> 5 | orderedByAll.intersect(customer.orders.flatMap { it.products }.toSet()) 6 | }) 7 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/SAM conversions/task.md: -------------------------------------------------------------------------------- 1 | ## SAM conversions 2 | 3 | When an object implements a SAM interface (one with a Single Abstract Method), 4 | you can pass a lambda instead. 5 | Read more about [SAM-conversions](https://kotlinlang.org/docs/reference/java-interop.html#sam-conversions). 6 | 7 | In the previous example change an object expression to a lambda. -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Filter map/Solution.kt: -------------------------------------------------------------------------------- 1 | package collections.filter_map 2 | 3 | import collections.* 4 | 5 | fun Shop.getCitiesCustomersAreFrom(): Set = 6 | /**/customers.map { it.city }.toSet()/**/ 7 | 8 | fun Shop.getCustomersFrom(city: City): List = 9 | /**/customers.filter { it.city == city }/**/ -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Filter map/task.md: -------------------------------------------------------------------------------- 1 | ## Filter; map 2 | 3 | Implement extension functions `Shop.getCitiesCustomersAreFrom()` and `Shop.getCustomersFrom()` using functions 4 | [`map`](map) and 5 | [`filter`](filter). 6 | 7 | ```kotlin 8 | val numbers = listOf(1, -1, 2) 9 | numbers.filter { it > 0 } == listOf(1, 2) 10 | numbers.map { it * it } == listOf(1, 1, 4) 11 | ``` -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Nullable types/Task.kt.txt: -------------------------------------------------------------------------------- 1 | fun sendMessageToClient( 2 | client: Client?, message: String?, mailer: Mailer 3 | ){ 4 | TODO() 5 | } 6 | 7 | class Client (val personalInfo: PersonalInfo?) 8 | class PersonalInfo (val email: String?) 9 | interface Mailer { 10 | fun sendMessage(email: String, message: String) 11 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Object expressions/Test.kt: -------------------------------------------------------------------------------- 1 | package introduction.object_expressions 2 | 3 | import org.junit.Test 4 | import org.junit.Assert 5 | import koans.util.toMessageInEquals 6 | 7 | class TestObjectExpressions { 8 | @Test fun testSort() { 9 | Assert.assertEquals("getList".toMessageInEquals(), listOf(5, 2, 1), getList()) 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson5/task1/task.html: -------------------------------------------------------------------------------- 1 |

Extension function literals

2 |

Read about function literals with receiver.

3 |

You can declare isEven and isOdd as values, that can be called as extension functions. Complete the declarations below.

-------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Named arguments/Test.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Test 2 | import org.junit.Assert 3 | import koans.util.toMessageInEquals 4 | 5 | class TestNamedArguments() { 6 | 7 | @Test fun testJoinToString() { 8 | Assert.assertEquals("joinOptions".toMessageInEquals(), "[yes, no, may be]", joinOptions(listOf("yes", "no", "may be"))) 9 | } 10 | 11 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Nullable types/Task.kt: -------------------------------------------------------------------------------- 1 | fun sendMessageToClient( 2 | client: Client?, message: String?, mailer: Mailer 3 | ){ 4 | TODO() 5 | } 6 | 7 | class Client (val personalInfo: PersonalInfo?) 8 | class PersonalInfo (val email: String?) 9 | interface Mailer { 10 | fun sendMessage(email: String, message: String) 11 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Hello, world!/task.md: -------------------------------------------------------------------------------- 1 | ## Simple Functions 2 | 3 | Take a look at [function syntax](function_syntax) 4 | and make the function `start` return the string `"OK"`. 5 | 6 | In the tasks the function `TODO()` is used that throws an exception. 7 | Your job during the koans will be to replace this function invocation with a meaningful code according to the problem. 8 | -------------------------------------------------------------------------------- /Kotlin Koans/Properties/Delegates how it works/task.md: -------------------------------------------------------------------------------- 1 | ## Delegates 2 | 3 | You may declare your own [delegates](delegates). 4 | Implement the methods of the class 'EffectiveDate' so it can be delegated to. 5 | Store only the time in milliseconds in 'timeInMillis' property. 6 | 7 | Use the extension functions `MyDate.toMillis()` and `Long.toDate()`, defined at 8 | [MyDate.kt](my_date_file) -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/FlatMap/task.md: -------------------------------------------------------------------------------- 1 | ## FlatMap 2 | 3 | Implement `Customer.getOrderedProducts()` and `Shop.getAllOrderedProducts()` using 4 | [`flatMap`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/kotlin.-iterable/flat-map.html). 5 | 6 | ```kotlin 7 | val result = listOf("abc", "12").flatMap { it.toCharList() } 8 | result == listOf('a', 'b', 'c', '1', '2') 9 | ``` -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/In range/MyDate.kt: -------------------------------------------------------------------------------- 1 | package conventions.in_range 2 | 3 | data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable { 4 | override fun compareTo(other: MyDate) = when { 5 | year != other.year -> year - other.year 6 | month != other.month -> month - other.month 7 | else -> dayOfMonth - other.dayOfMonth 8 | } 9 | } -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/Range to/MyDate.kt: -------------------------------------------------------------------------------- 1 | package conventions.range_to 2 | 3 | data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable { 4 | override fun compareTo(other: MyDate) = when { 5 | year != other.year -> year - other.year 6 | month != other.month -> month - other.month 7 | else -> dayOfMonth - other.dayOfMonth 8 | } 9 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Default arguments/Task.kt.txt: -------------------------------------------------------------------------------- 1 | fun foo(name: String, number: Int, toUpperCase: Boolean) = 2 | (if (toUpperCase) name.toUpperCase() else name) + number 3 | 4 | fun useFoo() = listOf( 5 | foo("a"), 6 | foo("b", number = 1), 7 | foo("c", toUpperCase = true), 8 | foo(name = "d", number = 2, toUpperCase = true) 9 | ) -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Extension functions/Solution.kt: -------------------------------------------------------------------------------- 1 | package introduction.extension_functions 2 | 3 | /**/fun Int.r(): RationalNumber = /**/RationalNumber(this, 1)/**/ 4 | fun Pair.r(): RationalNumber = /**/RationalNumber(first, second)/**/ 5 | 6 | data class RationalNumber(val numerator: Int, val denominator: Int) -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Default arguments/Task.kt: -------------------------------------------------------------------------------- 1 | fun foo(name: String, number: Int, toUpperCase: Boolean) = 2 | (if (toUpperCase) name.toUpperCase() else name) + number 3 | 4 | fun useFoo() = listOf( 5 | foo("a"), 6 | foo("b", number = 1), 7 | foo("c", toUpperCase = true), 8 | foo(name = "d", number = 2, toUpperCase = true) 9 | ) -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/Invoke/task.md: -------------------------------------------------------------------------------- 1 | ## Invoke 2 | 3 | Objects with `invoke()` method can be invoked as a function. 4 | 5 | You can add `invoke` extension for any class, but it's better not to overuse it: 6 | 7 | ```kotlin 8 | fun Int.invoke() { println(this) } 9 | 10 | 1() //huh?.. 11 | ``` 12 | 13 | Implement the function `Invokable.invoke()` so it would count a number of invocations. -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task6/tests.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Assert 2 | import org.junit.Test 3 | import koans.util.toMessage 4 | 5 | class TestSort { 6 | @Test fun testGetCustomersSortedByNumberOfOrders() { 7 | Assert.assertEquals("getCustomersSortedByNumberOfOrders".toMessage(), 8 | sortedCustomers, shop.getCustomersSortedByNumberOfOrders()) 9 | 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson5/task1/tests.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Assert 2 | import org.junit.Test 3 | import koans.util.inEquals 4 | 5 | class TestExtensionFunctionLiterals { 6 | @Test fun testIsOddAndIsEven() { 7 | Assert.assertEquals("The functions 'isOdd' and 'isEven' should be implemented correctly".inEquals(), 8 | listOf(false, true, true), task()) 9 | 10 | } 11 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Sort/Test.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Assert 2 | import org.junit.Test 3 | import koans.util.toMessage 4 | 5 | class TestSort { 6 | @Test fun testGetCustomersSortedByNumberOfOrders() { 7 | Assert.assertEquals("getCustomersSortedByNumberOfOrders".toMessage(), 8 | sortedCustomers, shop.getCustomersSortedByNumberOfOrders()) 9 | 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/Invoke/task.md: -------------------------------------------------------------------------------- 1 | ## Invoke 2 | 3 | Objects with `invoke()` method can be invoked as a function. 4 | 5 | You can add `invoke` extension for any class, but it's better not to overuse it: 6 | 7 | ```kotlin 8 | fun Int.invoke() { println(this) } 9 | 10 | 1() //huh?.. 11 | ``` 12 | 13 | Implement the function `Invokable.invoke()` so it would count a number of invocations. -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/Invoke/Solution.kt: -------------------------------------------------------------------------------- 1 | package conventions.invoke 2 | 3 | /**/class Invokable { 4 | var numberOfInvocations: Int = 0 5 | private set 6 | operator fun invoke(): Invokable { 7 | /**/numberOfInvocations++ 8 | return this/**/ 9 | } 10 | }/**/ 11 | 12 | fun invokeTwice(invokable: Invokable) = invokable()() -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Default arguments/Solution.kt: -------------------------------------------------------------------------------- 1 | fun foo(name: String, number: Int = 42, toUpperCase: Boolean = false) = 2 | (if (toUpperCase) name.toUpperCase() else name) + number 3 | 4 | fun useFoo() = listOf( 5 | foo("a"), 6 | foo("b", number = 1), 7 | foo("c", toUpperCase = true), 8 | foo(name = "d", number = 2, toUpperCase = true) 9 | ) -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Java to Kotlin conversion/Test.kt: -------------------------------------------------------------------------------- 1 | package introduction.java_to_kotlin_conversion 2 | 3 | import org.junit.Test 4 | import org.junit.Assert 5 | import koans.util.toMessageInEquals 6 | 7 | class TestFunctions() { 8 | @Test fun collection() { 9 | Assert.assertEquals("toJSON".toMessageInEquals(), "[1, 2, 3, 42, 555]", toJSON(listOf(1, 2, 3, 42, 555))) 10 | } 11 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/GroupBy/task.md: -------------------------------------------------------------------------------- 1 | ## Group By 2 | 3 | Implement `Shop.groupCustomersByCity()` using 4 | [`groupBy`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/kotlin.-iterable/group-by.html). 5 | 6 | ```kotlin 7 | val result = listOf("a", "b", "ba", "ccc", "ad").groupBy { it.length() } 8 | result == mapOf(1 to listOf("a", "b"), 2 to listOf("ba", "ad"), 3 to listOf("ccc")) 9 | ``` -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Max min/Solution.kt: -------------------------------------------------------------------------------- 1 | package collections.max_min 2 | 3 | import collections.* 4 | 5 | fun Shop.getCustomerWithMaximumNumberOfOrders(): Customer? = 6 | /**/customers.maxBy { it.orders.size }/**/ 7 | 8 | fun Customer.getMostExpensiveOrderedProduct(): Product? = 9 | /**/orders.flatMap { it.products }.maxBy { it.price }/**/ 10 | -------------------------------------------------------------------------------- /Kotlin Koans/koansTestUtil.kt: -------------------------------------------------------------------------------- 1 | package koans.util 2 | 3 | fun String.toMessage() = "The function '$this' is implemented incorrectly" 4 | 5 | fun String.toMessageInEquals() = toMessage().inEquals() 6 | 7 | fun String.inEquals() = this + ":" + if (mode == Mode.WEB_DEMO) " " else "

" 8 | 9 | private enum class Mode { WEB_DEMO, EDUCATIONAL_PLUGIN, UNDEFINED } 10 | private val mode = Mode.UNDEFINED 11 | -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/koansTestUtil.kt: -------------------------------------------------------------------------------- 1 | package koans.util 2 | 3 | fun String.toMessage() = "The function '$this' is implemented incorrectly" 4 | 5 | fun String.toMessageInEquals() = toMessage().inEquals() 6 | 7 | fun String.inEquals() = this + ":" + if (mode == Mode.WEB_DEMO) " " else "

" 8 | 9 | private enum class Mode { WEB_DEMO, EDUCATIONAL_PLUGIN, UNDEFINED } 10 | private val mode = Mode.WEB_DEMO 11 | -------------------------------------------------------------------------------- /Kotlin Koans/Collections/FlatMap/Solution.kt: -------------------------------------------------------------------------------- 1 | package collections.flatmap 2 | 3 | import collections.* 4 | 5 | val Customer.orderedProducts: Set get() { 6 | /**/return orders.flatMap { it.products }.toSet()/**/ 7 | } 8 | 9 | val Shop.allOrderedProducts: Set get() { 10 | /**/return customers.flatMap { it.orderedProducts }.toSet()/**/ 11 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/GroupBy/Test.kt: -------------------------------------------------------------------------------- 1 | package collections.groupby 2 | 3 | import org.junit.Assert 4 | import org.junit.Test 5 | import koans.util.toMessage 6 | import collections.* 7 | 8 | class TestGroupBy { 9 | @Test fun testGroupCustomersByCity() { 10 | Assert.assertEquals("groupCustomersByCity".toMessage(), 11 | groupedByCities, shop.groupCustomersByCity()) 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/Destructuring declarations/Test.kt: -------------------------------------------------------------------------------- 1 | package conventions.destructuring_declarations 2 | 3 | import org.junit.Assert 4 | import org.junit.Test 5 | 6 | class TestMultiAssignment { 7 | @Test fun testIsLeapDay() { 8 | Assert.assertTrue("The test failed", isLeapDay(MyDate(2016, 2, 29))) 9 | Assert.assertFalse("The test failed", isLeapDay(MyDate(2015, 2, 29))) 10 | } 11 | } -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/Operators overloading/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Operators overloading", 3 | "containsHelp": false, 4 | "args": "", 5 | "confType": "junit", 6 | "help": "", 7 | "files": [ 8 | { 9 | "modifiable": false, 10 | "hidden": false, 11 | "skipInTestVersion": false, 12 | "filename": "DateUtil.kt", 13 | "type": "kotlin-test" 14 | } 15 | ] 16 | } -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/Range to/task.md: -------------------------------------------------------------------------------- 1 | ## Range to 2 | 3 | Implement the function `MyDate.rangeTo()`. 4 | This allows you to create a range of dates using the following syntax: 5 | 6 | ```kotlin 7 | MyDate(2015, 5, 11)..MyDate(2015, 5, 12) 8 | ``` 9 | 10 | Note that now the class `DateRange` implements the standard 11 | [`ClosedRange`](closed_range) 12 | interface and inherits `contains` method implementation. -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task10/task.html: -------------------------------------------------------------------------------- 1 |

Fold

2 |

Implement Shop.getSetOfProductsOrderedByEveryCustomer() using fold.

listOf(1, 2, 3, 4).fold(1, {
3 |     partProduct, element ->
4 |     element * partProduct
5 | }) == 24
6 | 
-------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Get used to new style/Solution.kt: -------------------------------------------------------------------------------- 1 | fun doSomethingStrangeWithCollection(collection: Collection): Collection? { 2 | 3 | val groupsByLength = collection. groupBy { s -> s.length } 4 | 5 | val maximumSizeOfGroup = groupsByLength.values.map { group -> group.size }.max() 6 | 7 | return groupsByLength.values.firstOrNull { group -> group.size == maximumSizeOfGroup } 8 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/Operators overloading/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Operators overloading", 3 | "containsHelp": false, 4 | "args": "", 5 | "confType": "junit", 6 | "help": "", 7 | "files": [ 8 | { 9 | "modifiable": false, 10 | "hidden": false, 11 | "skipInTestVersion": false, 12 | "filename": "DateUtil.kt", 13 | "type": "kotlin-test" 14 | } 15 | ] 16 | } -------------------------------------------------------------------------------- /Kotlin Koans/Builders/Function literals with receiver/Solution.kt: -------------------------------------------------------------------------------- 1 | package builders.function_literals_with_receiver 2 | 3 | fun task(): List { 4 | 5 | /**/val isEven: Int.() -> Boolean = { /**/this % 2 == 0/**/ } 6 | val isOdd: Int.() -> Boolean = { /**/this % 2 != 0/**/ }/**/ 7 | 8 | return listOf(42.isOdd(), 239.isOdd(), 294823098.isEven()) 9 | } -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/In range/Solution.kt: -------------------------------------------------------------------------------- 1 | package conventions.in_range 2 | 3 | /**/class DateRange(val start: MyDate, val endInclusive: MyDate)/**/ { 4 | operator fun contains(item: MyDate): Boolean = start <= item && item <= endInclusive 5 | }/**/ 6 | 7 | fun checkInRange(date: MyDate, first: MyDate, last: MyDate): Boolean { 8 | return date in DateRange(first, last) 9 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Named arguments/Test.kt: -------------------------------------------------------------------------------- 1 | package introduction.named_arguments 2 | 3 | import org.junit.Test 4 | import org.junit.Assert 5 | import koans.util.toMessageInEquals 6 | 7 | class TestNamedArguments() { 8 | 9 | @Test fun testJoinToString() { 10 | Assert.assertEquals("joinOptions".toMessageInEquals(), "[yes, no, may be]", joinOptions(listOf("yes", "no", "may be"))) 11 | } 12 | 13 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Builders/Function literals with receiver/Test.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Assert 2 | import org.junit.Test 3 | import koans.util.inEquals 4 | 5 | class TestExtensionFunctionLiterals { 6 | @Test fun testIsOddAndIsEven() { 7 | Assert.assertEquals("The functions 'isOdd' and 'isEven' should be implemented correctly".inEquals(), 8 | listOf(false, true, true), task()) 9 | 10 | } 11 | } -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/In range/task.md: -------------------------------------------------------------------------------- 1 | ## In range 2 | 3 | In Kotlin `in` checks are translated to the corresponding `contains` calls: 4 | 5 | ```kotlin 6 | val list = listOf("a", "b") 7 | "a" in list // list.contains("a") 8 | "a" !in list // !list.contains("a") 9 | ``` 10 | 11 | Read about [ranges](ranges). 12 | Add a method `fun contains(d: MyDate)` to the class `DateRange` to allow `in` checks with a range of dates. -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/Range to/Solution.kt: -------------------------------------------------------------------------------- 1 | package conventions.range_to 2 | 3 | /**/operator fun MyDate.rangeTo(other: MyDate) = /**/DateRange(this, other)/**/ 4 | 5 | class DateRange(override val start: MyDate, override val endInclusive: MyDate): ClosedRange 6 | 7 | fun checkInRange(date: MyDate, first: MyDate, last: MyDate): Boolean { 8 | return date in first..last 9 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Lambdas/task.md: -------------------------------------------------------------------------------- 1 | ## Lambdas 2 | 3 | Kotlin supports a functional style of programming. 4 | Read about [higher-order functions and function literals (lambdas)](lambdas) in Kotlin. 5 | 6 | Pass a lambda to [`any`](any) 7 | function to check if the collection contains an even number. 8 | The function `any` gets a predicate as an argument and returns true if there is at least one element satisfying the predicate. -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Object expressions/Solution.kt: -------------------------------------------------------------------------------- 1 | package introduction.object_expressions 2 | 3 | import java.util.* 4 | 5 | fun getList(): List { 6 | val arrayList = arrayListOf(1, 5, 2) 7 | 8 | /**/Collections.sort(arrayList, /**/object : Comparator { 9 | override fun compare(x: Int, y: Int) = y - x 10 | }/**/)/**/ 11 | 12 | return arrayList 13 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Introduction/Test.kt: -------------------------------------------------------------------------------- 1 | package collections.introduction 2 | 3 | import org.junit.Assert 4 | import org.junit.Test 5 | import koans.util.toMessage 6 | import collections.* 7 | 8 | class TestIntroduction { 9 | @Test fun testSetOfCustomers(){ 10 | Assert.assertEquals("getSetOfCustomers".toMessage(), 11 | customers.values.toSet(), shop.getSetOfCustomers()) 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Smart casts/Task.kt.txt: -------------------------------------------------------------------------------- 1 | fun eval(expr: Expr): Int = 2 | when (expr) { 3 | is Num -> TODO() 4 | is Sum -> TODO() 5 | else -> throw IllegalArgumentException("Unknown expression") 6 | } 7 | 8 | interface Expr 9 | class Num(val value: Int) : Expr 10 | class Sum(val left: Expr, val right: Expr) : Expr 11 | -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/util/koansTestUtil.kt: -------------------------------------------------------------------------------- 1 | package koans.util 2 | 3 | fun String.toMessage() = "The function '$this' is implemented incorrectly" 4 | 5 | fun String.toMessageInEquals() = toMessage().inEquals() 6 | 7 | fun String.inEquals() = this + ":" + if (mode == Mode.WEB_DEMO) " " else "

" 8 | 9 | private enum class Mode { WEB_DEMO, EDUCATIONAL_PLUGIN, UNDEFINED } 10 | private val mode = Mode.EDUCATIONAL_PLUGIN 11 | -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Smart casts/Solution.kt: -------------------------------------------------------------------------------- 1 | fun eval(expr: Expr): Int = 2 | when (expr) { 3 | is Num -> expr.value 4 | is Sum -> eval(expr.left) + eval(expr.right) 5 | else -> throw IllegalArgumentException("Unknown expression") 6 | } 7 | 8 | interface Expr 9 | class Num(val value: Int) : Expr 10 | class Sum(val left: Expr, val right: Expr) : Expr 11 | -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Smart casts/Task.kt: -------------------------------------------------------------------------------- 1 | fun eval(expr: Expr): Int = 2 | when (expr) { 3 | is Num -> TODO() 4 | is Sum -> TODO() 5 | else -> throw IllegalArgumentException("Unknown expression") 6 | } 7 | 8 | interface Expr 9 | class Num(val value: Int) : Expr 10 | class Sum(val left: Expr, val right: Expr) : Expr 11 | -------------------------------------------------------------------------------- /Kotlin Koans/Builders/Html builders/task-wb.md: -------------------------------------------------------------------------------- 1 | ## Html builder 2 | 3 | _1._ Fill the table with the proper values from the product list. 4 | The products are declared in [data.kt](data_file). 5 | 6 | _2._ Color the table like a chess board (using getTitleColor() and getCellColor() functions above). 7 | Pass a color as an argument to the functions `tr`, `td`. 8 | 9 | You can run 'JavaScript(Canvas)' configuration to see the rendered table. -------------------------------------------------------------------------------- /Kotlin Koans/Builders/Html builders/task-ee.md: -------------------------------------------------------------------------------- 1 | ## Html builder 2 | 3 | _1._ Fill the table with the proper values from the product list. 4 | The products are declared in [data.kt](data_file). 5 | 6 | _2._ Color the table like a chess board (using getTitleColor() and getCellColor() functions above). 7 | Pass a color as an argument to the functions `tr`, `td`. 8 | 9 | Open the file `demo.kt` and run the main function there to see the rendered table. -------------------------------------------------------------------------------- /course.properties: -------------------------------------------------------------------------------- 1 | COURSE_NAME=Kotlin Koans 2 | COURSE_DESCRIPTION=Kotlin Koans 3 | COURSE_LANGUAGE=Kotlin 4 | CONVERT_WB=true 5 | CONVERT_EE=true 6 | CONVERT_CG=true 7 | PACK_ZIP=false 8 | ZIP_FILE_NAME=Kotlin Koans.zip 9 | SOURCE_DIR=Kotlin Koans 10 | CONVERTED_DIR_EE=Kotlin Koans EE Plugin 11 | CONVERTED_DIR_WB=Kotlin Koans WebDemo 12 | CONVERTED_DIR_CG=Kotlin Koans CodinGame 13 | LINKS_ENABLED=true 14 | LINKS_PROPERTIES=links.properties -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task9/tests.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Assert 2 | import org.junit.Test 3 | import koans.util.toMessage 4 | 5 | class TestPartition { 6 | @Test fun testGetCustomersWhoHaveMoreUndeliveredOrdersThanDelivered() { 7 | Assert.assertEquals("getCustomerWithMaximumNumberOfOrders".toMessage(), 8 | setOf(customers[reka]), shop.getCustomersWithMoreUndeliveredOrdersThanDelivered()) 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Builders/String and map builders/Solution.kt: -------------------------------------------------------------------------------- 1 | import java.util.HashMap 2 | 3 | fun buildMap(build: HashMap.() -> Unit): Map { 4 | val map = HashMap() 5 | map.build() 6 | return map 7 | } 8 | 9 | fun usage(): Map { 10 | return buildMap { 11 | put(0, "0") 12 | for (i in 1..10) { 13 | put(i, "$i") 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Hello, world!/task.md: -------------------------------------------------------------------------------- 1 | ## Simple Functions 2 | 3 | Take a look at [function syntax](http://kotlinlang.org/docs/reference/basic-syntax.html#defining-functions) 4 | and make the function `start` return the string `"OK"`. 5 | 6 | In the tasks the function `TODO()` is used that throws an exception. 7 | Your job during the koans will be to replace this function invocation with a meaningful code according to the problem. 8 | -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task10/tests.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Test 2 | import org.junit.Assert 3 | 4 | class TestExtensionFunctions() { 5 | @Test fun testIntExtension() { 6 | Assert.assertEquals("Rational number creation error: ", RationalNumber(4, 1), 4.r()) 7 | } 8 | 9 | @Test fun testPairExtension() { 10 | Assert.assertEquals("Rational number creation error: ", RationalNumber(2, 3), Pair(2, 3).r()) 11 | } 12 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Partition/Test.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Assert 2 | import org.junit.Test 3 | import koans.util.toMessage 4 | 5 | class TestPartition { 6 | @Test fun testGetCustomersWhoHaveMoreUndeliveredOrdersThanDelivered() { 7 | Assert.assertEquals("getCustomerWithMaximumNumberOfOrders".toMessage(), 8 | setOf(customers[reka]), shop.getCustomersWithMoreUndeliveredOrdersThanDelivered()) 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/Comparison/Solution.kt: -------------------------------------------------------------------------------- 1 | data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable { 2 | override fun compareTo(other: MyDate) = when { 3 | year != other.year -> year - other.year 4 | month != other.month -> month - other.month 5 | else -> dayOfMonth - other.dayOfMonth 6 | } 7 | } 8 | 9 | fun compare(date1: MyDate, date2: MyDate) = date1 < date2 -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/Destructuring declarations/Solution.kt: -------------------------------------------------------------------------------- 1 | package conventions.destructuring_declarations 2 | 3 | /**/data /**/class MyDate(val year: Int, val month: Int, val dayOfMonth: Int)/**/ 4 | 5 | fun isLeapDay(date: MyDate): Boolean { 6 | 7 | val (year, month, dayOfMonth) = date 8 | 9 | // 29 February of a leap year 10 | return year % 4 == 0 && month == 2 && dayOfMonth == 29 11 | } -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/For loop/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit", 3 | "files": [ 4 | { 5 | "modifiable": false, 6 | "hidden": false, 7 | "skipInTestVersion": false, 8 | "filename": "MyDate.kt" 9 | }, 10 | { 11 | "modifiable": false, 12 | "hidden": false, 13 | "skipInTestVersion": false, 14 | "filename": "DateUtil.kt", 15 | "type": "kotlin-test" 16 | } 17 | ] 18 | } -------------------------------------------------------------------------------- /Kotlin Koans/Properties/Lazy property/Solution.kt: -------------------------------------------------------------------------------- 1 | package properties.lazy_property 2 | 3 | /**/class LazyProperty(val initializer: () -> Int) { 4 | /**/var value: Int? = null/**/ 5 | val lazy: Int 6 | get() { 7 | /**/if (value == null) { 8 | value = initializer() 9 | } 10 | return value!!/**/ 11 | } 12 | }/**/ -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson2/task7/task.html: -------------------------------------------------------------------------------- 1 |

Invoke

2 |

Objects with invoke() method can be invoked as a function.

3 |

You can add invoke extension for any class, but it's better not to overuse it:

fun Int.invoke() { println(this) }
4 | 
5 | 1() //huh?..
6 | 
7 |

Implement the function Invokable.invoke() so it would count a number of invocations.

-------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Get used to new style/Task.kt: -------------------------------------------------------------------------------- 1 | fun doSomethingStrangeWithCollection(collection: Collection): Collection? { 2 | 3 | val groupsByLength = collection. groupBy { s -> TODO() } 4 | 5 | val maximumSizeOfGroup = groupsByLength.values.map { group -> TODO() }.max() 6 | 7 | return groupsByLength.values.firstOrNull { group -> TODO() } 8 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/For loop/MyDate.kt: -------------------------------------------------------------------------------- 1 | data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable { 2 | override fun compareTo(other: MyDate) = when { 3 | year != other.year -> year - other.year 4 | month != other.month -> month - other.month 5 | else -> dayOfMonth - other.dayOfMonth 6 | } 7 | } 8 | 9 | operator fun MyDate.rangeTo(other: MyDate) = DateRange(this, other) 10 | -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/For loop/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "confType": "junit", 3 | "files": [ 4 | { 5 | "modifiable": false, 6 | "hidden": false, 7 | "skipInTestVersion": false, 8 | "filename": "MyDate.kt" 9 | }, 10 | { 11 | "modifiable": false, 12 | "hidden": false, 13 | "skipInTestVersion": false, 14 | "filename": "DateUtil.kt", 15 | "type": "kotlin-test" 16 | } 17 | ] 18 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Get used to new style/Task.kt.txt: -------------------------------------------------------------------------------- 1 | fun doSomethingStrangeWithCollection(collection: Collection): Collection? { 2 | 3 | val groupsByLength = collection. groupBy { s -> TODO() } 4 | 5 | val maximumSizeOfGroup = groupsByLength.values.map { group -> TODO() }.max() 6 | 7 | return groupsByLength.values.firstOrNull { group -> TODO() } 8 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Sort/Test.kt: -------------------------------------------------------------------------------- 1 | package collections.sort 2 | 3 | import org.junit.Assert 4 | import org.junit.Test 5 | import koans.util.toMessage 6 | import collections.* 7 | 8 | class TestSort { 9 | @Test fun testGetCustomersSortedByNumberOfOrders() { 10 | Assert.assertEquals("getCustomersSortedByNumberOfOrders".toMessage(), 11 | sortedCustomers, shop.getCustomersSortedByNumberOfOrders()) 12 | 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/Comparison/task.md: -------------------------------------------------------------------------------- 1 | ## Comparison 2 | 3 | Read about [operator overloading](operator_overloading) 4 | to learn how different conventions for operations like `==`, `<`, `+` work in Kotlin. 5 | Add the function `compareTo` to the class MyDate to make it comparable. 6 | After that the code below `date1 < date2` will start to compile. 7 | 8 | In Kotlin when you override a member, the modifier 9 | [`override`](override) is mandatory. 10 | -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "sequential": false, 3 | "examples": [ 4 | "Hello, world!", 5 | "Java to Kotlin conversion", 6 | "Named arguments", 7 | "Default arguments", 8 | "Lambdas", 9 | "Strings", 10 | "Data classes", 11 | "Nullable types", 12 | "Smart casts", 13 | "Extension functions", 14 | "Object expressions", 15 | "SAM conversions", 16 | "Extensions on collections" 17 | ] 18 | } -------------------------------------------------------------------------------- /.idea/libraries/KotlinJavaRuntime.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson5/task4/demo.kt: -------------------------------------------------------------------------------- 1 | import javax.swing.JFrame 2 | import javax.swing.JLabel 3 | import javax.swing.JScrollPane 4 | import javax.swing.SwingConstants.CENTER 5 | 6 | fun main(args: Array) { 7 | with (JFrame("Product popularity")) { 8 | setSize(600, 600) 9 | defaultCloseOperation = JFrame.EXIT_ON_CLOSE 10 | add(JScrollPane(JLabel(renderProductTable(), CENTER))) 11 | isVisible = true 12 | } 13 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Extension functions/Test.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Test 2 | import org.junit.Assert 3 | 4 | class TestExtensionFunctions() { 5 | @Test fun testIntExtension() { 6 | Assert.assertEquals("Rational number creation error: ", RationalNumber(4, 1), 4.r()) 7 | } 8 | 9 | @Test fun testPairExtension() { 10 | Assert.assertEquals("Rational number creation error: ", RationalNumber(2, 3), Pair(2, 3).r()) 11 | } 12 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/All Any and other predicates/task.md: -------------------------------------------------------------------------------- 1 | ## All, Any and other predicates 2 | 3 | Implement all the functions below using 4 | [`all`](all), 5 | [`any`](any), 6 | [`count`](count), 7 | [`find`](find). 8 | 9 | ```kotlin 10 | val numbers = listOf(-1, 0, 2) 11 | val isZero: (Int) -> Boolean = { it == 0 } 12 | numbers.any(isZero) == true 13 | numbers.all(isZero) == false 14 | numbers.count(isZero) == 1 15 | numbers.find { it > 0 } == 2 16 | ``` -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Partition/task.md: -------------------------------------------------------------------------------- 1 | ## Partition 2 | Implement `Shop.getCustomersWithMoreUndeliveredOrdersThanDelivered()` using 3 | [`partition`](partition). 4 | 5 | ```kotlin 6 | val numbers = listOf(1, 3, -4, 2, -11) 7 | val (positive, negative) = numbers.partition { it > 0 } 8 | positive == listOf(1, 3, 2) 9 | negative == listOf(-4, -11) 10 | ``` 11 | 12 | Note that [destructuring declaration](destructuring_declarations) syntax is used in this example. -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "sequential": false, 3 | "examples": [ 4 | "Hello, world!", 5 | "Java to Kotlin conversion", 6 | "Named arguments", 7 | "Default arguments", 8 | "Lambdas", 9 | "Strings", 10 | "Data classes", 11 | "Nullable types", 12 | "Smart casts", 13 | "Extension functions", 14 | "Object expressions", 15 | "SAM conversions", 16 | "Extensions on collections" 17 | ] 18 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/All Any and other predicates/Solution.kt: -------------------------------------------------------------------------------- 1 | fun Shop.checkAllCustomersAreFrom(city: City): Boolean = 2 | customers.all { it.city == city } 3 | 4 | fun Shop.hasCustomerFrom(city: City): Boolean = 5 | customers.any { it.city == city } 6 | 7 | fun Shop.countCustomersFrom(city: City): Int = 8 | customers.count { it.city == city } 9 | 10 | fun Shop.findAnyCustomerFrom(city: City): Customer? = 11 | customers.find { it.city == city } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/In range/task.md: -------------------------------------------------------------------------------- 1 | ## In range 2 | 3 | In Kotlin `in` checks are translated to the corresponding `contains` calls: 4 | 5 | ```kotlin 6 | val list = listOf("a", "b") 7 | "a" in list // list.contains("a") 8 | "a" !in list // !list.contains("a") 9 | ``` 10 | 11 | Read about [ranges](http://kotlinlang.org/docs/reference/ranges.html). 12 | Add a method `fun contains(d: MyDate)` to the class `DateRange` to allow `in` checks with a range of dates. -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Fold/Solution.kt: -------------------------------------------------------------------------------- 1 | package collections.fold 2 | 3 | import collections.* 4 | 5 | fun Shop.getSetOfProductsOrderedByEveryCustomer(): Set { 6 | /**/val allProducts = customers.flatMap { it.orders.flatMap { it.products }}.toSet() 7 | return customers.fold(allProducts, { 8 | orderedByAll, customer -> 9 | orderedByAll.intersect(customer.orders.flatMap { it.products }.toSet()) 10 | })/**/ 11 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Builders/Html builders/task.md: -------------------------------------------------------------------------------- 1 | ## Html builder 2 | 3 | _1._ Fill the table with the proper values from the product list. 4 | The products are declared in [data.kt](/#/Kotlin%20Koans/Builders/Html%20builders/data.kt). 5 | 6 | _2._ Color the table like a chess board (using getTitleColor() and getCellColor() functions above). 7 | Pass a color as an argument to the functions `tr`, `td`. 8 | 9 | You can run 'JavaScript(Canvas)' configuration to see the rendered table. -------------------------------------------------------------------------------- /Kotlin Koans/Builders/Function literals with receiver/Test.kt: -------------------------------------------------------------------------------- 1 | package builders.function_literals_with_receiver 2 | 3 | import org.junit.Assert 4 | import org.junit.Test 5 | import koans.util.inEquals 6 | 7 | class TestExtensionFunctionLiterals { 8 | @Test fun testIsOddAndIsEven() { 9 | Assert.assertEquals("The functions 'isOdd' and 'isEven' should be implemented correctly".inEquals(), 10 | listOf(false, true, true), task()) 11 | 12 | } 13 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task11/Task.kt: -------------------------------------------------------------------------------- 1 | // Return the most expensive product among all delivered products 2 | // (use the Order.isDelivered flag) 3 | fun Customer.getMostExpensiveDeliveredProduct(): Product? { 4 | TODO() 5 | } 6 | 7 | // Return how many times the given product was ordered. 8 | // Note: a customer may order the same product for several times. 9 | fun Shop.getNumberOfTimesProductWasOrdered(product: Product): Int { 10 | TODO() 11 | } 12 | 13 | /* TODO */ -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson4/task4/task.html: -------------------------------------------------------------------------------- 1 |

Delegates

2 |

You may declare your own delegates. Implement the methods of the class 'EffectiveDate' so it can be delegated to. Store only the time in milliseconds in 'timeInMillis' property.

3 |

Use the extension functions MyDate.toMillis() and Long.toDate(), defined at MyDate.kt

-------------------------------------------------------------------------------- /Kotlin Koans/Conventions/For loop/MyDate.kt: -------------------------------------------------------------------------------- 1 | package conventions.for_loop 2 | 3 | data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable { 4 | override fun compareTo(other: MyDate) = when { 5 | year != other.year -> year - other.year 6 | month != other.month -> month - other.month 7 | else -> dayOfMonth - other.dayOfMonth 8 | } 9 | } 10 | 11 | operator fun MyDate.rangeTo(other: MyDate) = DateRange(this, other) 12 | -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task1/task.html: -------------------------------------------------------------------------------- 1 |

Simple Functions

2 |

Take a look at function syntax and make the function start return the string "OK".

3 |

In the tasks the function TODO() is used that throws an exception. Your job during the koans will be to replace this function invocation with a meaningful code according to the problem.

-------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson2/task5/Task.kt: -------------------------------------------------------------------------------- 1 | import TimeInterval.* 2 | 3 | data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) 4 | 5 | enum class TimeInterval { DAY, WEEK, YEAR } 6 | 7 | operator fun MyDate.plus(timeInterval: TimeInterval): MyDate = TODO() 8 | 9 | fun task1(today: MyDate): MyDate { 10 | return today + YEAR + WEEK 11 | } 12 | 13 | fun task2(today: MyDate): MyDate { 14 | TODO("Uncomment") //return today + YEAR * 2 + WEEK * 3 + DAY * 5 15 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Object expressions/task.md: -------------------------------------------------------------------------------- 1 | ## Object expressions 2 | 3 | Read about [object expressions](object_declarations) 4 | that play the same role in Kotlin as anonymous classes in Java. 5 | 6 | Add an object expression that provides a comparator to sort a list in a descending order using java.util.Collections class. 7 | In Kotlin you use Kotlin library extensions instead of java.util.Collections, 8 | but this example is still a good demonstration of mixing Kotlin and Java code. -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson5/task4/task.html: -------------------------------------------------------------------------------- 1 |

Html builder

2 |

1. Fill the table with the proper values from the product list. The products are declared in data.kt.

3 |

2. Color the table like a chess board (using getTitleColor() and getCellColor() functions above). Pass a color as an argument to the functions tr, td.

4 |

Open the file demo.kt and run the main function there to see the rendered table.

-------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Default arguments/Solution.kt: -------------------------------------------------------------------------------- 1 | package introduction.default_arguments 2 | 3 | /**/fun foo(name: String, number: Int = 42, toUpperCase: Boolean = false)/**/ = 4 | (if (toUpperCase) name.toUpperCase() else name) + number/**/ 5 | 6 | fun useFoo() = listOf( 7 | foo("a"), 8 | foo("b", number = 1), 9 | foo("c", toUpperCase = true), 10 | foo(name = "d", number = 2, toUpperCase = true) 11 | ) -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/Range to/task.md: -------------------------------------------------------------------------------- 1 | ## Range to 2 | 3 | Implement the function `MyDate.rangeTo()`. 4 | This allows you to create a range of dates using the following syntax: 5 | 6 | ```kotlin 7 | MyDate(2015, 5, 11)..MyDate(2015, 5, 12) 8 | ``` 9 | 10 | Note that now the class `DateRange` implements the standard 11 | [`ClosedRange`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.ranges/kotlin.-closed-range/index.html) 12 | interface and inherits `contains` method implementation. -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task5/tests.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Test 2 | import org.junit.Assert 3 | 4 | class TestLambdas() { 5 | @Test fun contains() { 6 | Assert.assertTrue("The result should be true if the collection contains an even number", containsEven(listOf(1, 2, 3, 126, 555))) 7 | } 8 | 9 | @Test fun notContains() { 10 | Assert.assertFalse("The result should be false if the collection doesn't contain an even number", containsEven(listOf(43, 33))) 11 | } 12 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Compound tasks/Solution.kt: -------------------------------------------------------------------------------- 1 | fun Customer.getMostExpensiveDeliveredProduct(): Product? { 2 | return orders.filter { it.isDelivered }.flatMap { it.products }.maxBy { it.price } 3 | } 4 | 5 | fun Shop.getNumberOfTimesProductWasOrdered(product: Product): Int { 6 | return customers.flatMap { it.getOrderedProductsList() }.count { it == product } 7 | } 8 | 9 | fun Customer.getOrderedProductsList(): List { 10 | return orders.flatMap { it.products } 11 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/For loop/task.md: -------------------------------------------------------------------------------- 1 | ## For loop 2 | 3 | Kotlin [for loop](http://kotlinlang.org/docs/reference/control-flow.html#for-loops) 4 | iterates through anything that provides an iterator. 5 | Make the class `DateRange` implement [`Iterable`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/kotlin.-iterable/), 6 | so that it could be iterated over. 7 | You can use the function `MyDate.nextDay()` defined in [DateUtil.kt](/#/Kotlin%20Koans/Conventions/For%20loop/DateUtil.kt) -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Lambdas/Test.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Test 2 | import org.junit.Assert 3 | 4 | class TestLambdas() { 5 | @Test fun contains() { 6 | Assert.assertTrue("The result should be true if the collection contains an even number", containsEven(listOf(1, 2, 3, 126, 555))) 7 | } 8 | 9 | @Test fun notContains() { 10 | Assert.assertFalse("The result should be false if the collection doesn't contain an even number", containsEven(listOf(43, 33))) 11 | } 12 | } -------------------------------------------------------------------------------- /Kotlin Koans/Builders/Html builders/demo-ee.kt: -------------------------------------------------------------------------------- 1 | package builders.html_builders 2 | 3 | import javax.swing.JFrame 4 | import javax.swing.JLabel 5 | import javax.swing.JScrollPane 6 | import javax.swing.SwingConstants.CENTER 7 | 8 | fun main(args: Array) { 9 | with (JFrame("Product popularity")) { 10 | setSize(600, 600) 11 | defaultCloseOperation = JFrame.EXIT_ON_CLOSE 12 | add(JScrollPane(JLabel(renderProductTable(), CENTER))) 13 | isVisible = true 14 | } 15 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Partition/Test.kt: -------------------------------------------------------------------------------- 1 | package collections.partition 2 | 3 | import org.junit.Assert 4 | import org.junit.Test 5 | import koans.util.toMessage 6 | import collections.* 7 | 8 | class TestPartition { 9 | @Test fun testGetCustomersWhoHaveMoreUndeliveredOrdersThanDelivered() { 10 | Assert.assertEquals("getCustomerWithMaximumNumberOfOrders".toMessage(), 11 | setOf(customers[reka]), shop.getCustomersWithMoreUndeliveredOrdersThanDelivered()) 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Extension functions/Test.kt: -------------------------------------------------------------------------------- 1 | package introduction.extension_functions 2 | 3 | import org.junit.Test 4 | import org.junit.Assert 5 | 6 | class TestExtensionFunctions() { 7 | @Test fun testIntExtension() { 8 | Assert.assertEquals("Rational number creation error: ", RationalNumber(4, 1), 4.r()) 9 | } 10 | 11 | @Test fun testPairExtension() { 12 | Assert.assertEquals("Rational number creation error: ", RationalNumber(2, 3), Pair(2, 3).r()) 13 | } 14 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Java to Kotlin conversion/Solution.kt: -------------------------------------------------------------------------------- 1 | fun toJSON(collection: Collection): String { 2 | val sb = StringBuilder() 3 | sb.append("[") 4 | val iterator = collection.iterator() 5 | while (iterator.hasNext()) 6 | { 7 | val element = iterator.next() 8 | sb.append(element) 9 | if (iterator.hasNext()) 10 | { 11 | sb.append(", ") 12 | } 13 | } 14 | sb.append("]") 15 | return sb.toString() 16 | } 17 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson2/task4/task.html: -------------------------------------------------------------------------------- 1 |

For loop

2 |

Kotlin for loop iterates through anything that provides an iterator. Make the class DateRange implement Iterable<MyDate>, so that it could be iterated over. You can use the function MyDate.nextDay() defined in DateUtil.kt

-------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task4/task.html: -------------------------------------------------------------------------------- 1 |

FlatMap

2 |

Implement Customer.getOrderedProducts() and Shop.getAllOrderedProducts() using flatMap.

val result = listOf("abc", "12").flatMap { it.toCharList() }
3 | result == listOf('a', 'b', 'c', '1', '2')
4 | 
-------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson5/task3/Task.kt: -------------------------------------------------------------------------------- 1 | fun T.myApply(f: T.() -> Unit): T { TODO() } 2 | 3 | fun createString(): String { 4 | return StringBuilder().myApply { 5 | append("Numbers: ") 6 | for (i in 1..10) { 7 | append(i) 8 | } 9 | }.toString() 10 | } 11 | 12 | fun createMap(): Map { 13 | return hashMapOf().myApply { 14 | put(0, "0") 15 | for (i in 1..10) { 16 | put(i, "$i") 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Sort/task.md: -------------------------------------------------------------------------------- 1 | ## Sort 2 | 3 | Implement `Shop.getCustomersSortedByNumberOfOrders()` using 4 | [`sorted`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/kotlin.-iterable/sorted.html) or 5 | [`sortedBy`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/kotlin.-iterable/sorted-by.html). 6 | 7 | ```kotlin 8 | listOf("bbb", "a", "cc").sorted() == listOf("a", "bbb", "cc") 9 | listOf("bbb", "a", "cc").sortedBy { it.length } == listOf("a", "cc", "bbb") 10 | ``` -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Nullable types/Solution.kt: -------------------------------------------------------------------------------- 1 | fun sendMessageToClient( 2 | client: Client?, message: String?, mailer: Mailer 3 | ) { 4 | val email = client?.personalInfo?.email 5 | if (email != null && message != null) { 6 | mailer.sendMessage(email, message) 7 | } 8 | } 9 | 10 | class Client (val personalInfo: PersonalInfo?) 11 | class PersonalInfo (val email: String?) 12 | interface Mailer { 13 | fun sendMessage(email: String, message: String) 14 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson4/task4/tests.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Assert 2 | import org.junit.Test 3 | 4 | class TestDelegatesHowItWorks { 5 | @Test fun testDate() { 6 | val d = D() 7 | d.date = MyDate(2014, 1, 13) 8 | val message = "The methods 'getValue' and 'setValue' are implemented incorrectly" 9 | Assert.assertTrue(message, 2014 == d.date.year) 10 | Assert.assertTrue(message, 1 == d.date.month) 11 | Assert.assertTrue(message, 13 == d.date.dayOfMonth) 12 | } 13 | } -------------------------------------------------------------------------------- /Kotlin Koans/Builders/String and map builders/Solution.kt: -------------------------------------------------------------------------------- 1 | package builders.string_and_map_builders 2 | 3 | import java.util.HashMap 4 | 5 | /**/fun buildMap(build: HashMap.() -> Unit): Map { 6 | val map = HashMap() 7 | map.build() 8 | return map 9 | }/**/ 10 | 11 | fun usage(): Map { 12 | return buildMap { 13 | put(0, "0") 14 | for (i in 1..10) { 15 | put(i, "$i") 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Properties/Delegates how it works/task.md: -------------------------------------------------------------------------------- 1 | ## Delegates 2 | 3 | You may declare your own [delegates](https://kotlinlang.org/docs/reference/delegated-properties.html#property-delegate-requirements). 4 | Implement the methods of the class 'EffectiveDate' so it can be delegated to. 5 | Store only the time in milliseconds in 'timeInMillis' property. 6 | 7 | Use the extension functions `MyDate.toMillis()` and `Long.toDate()`, defined at 8 | [MyDate.kt](/#/Kotlin%20Koans/Properties/Delegates%20how%20it%20works/MyDate.kt) -------------------------------------------------------------------------------- /.idea/libraries/Maven__junit_junit_4_12.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task11/task.html: -------------------------------------------------------------------------------- 1 |

Object expressions

2 |

Read about object expressions that play the same role in Kotlin as anonymous classes in Java.

3 |

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.

-------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Compound tasks/Task.kt: -------------------------------------------------------------------------------- 1 | // Return the most expensive product among all delivered products 2 | // (use the Order.isDelivered flag) 3 | fun Customer.getMostExpensiveDeliveredProduct(): Product? { 4 | TODO() 5 | } 6 | 7 | // Return how many times the given product was ordered. 8 | // Note: a customer may order the same product for several times. 9 | fun Shop.getNumberOfTimesProductWasOrdered(product: Product): Int { 10 | TODO() 11 | } 12 | 13 | -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/Comparison/Solution.kt: -------------------------------------------------------------------------------- 1 | package conventions.comparison 2 | 3 | /**/data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable { 4 | /**/override fun compareTo(other: MyDate) = when { 5 | year != other.year -> year - other.year 6 | month != other.month -> month - other.month 7 | else -> dayOfMonth - other.dayOfMonth 8 | }/**/ 9 | }/**/ 10 | 11 | fun compare(date1: MyDate, date2: MyDate) = date1 < date2 -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Smart casts/task.md: -------------------------------------------------------------------------------- 1 | ## Smart casts 2 | 3 | Rewrite the following Java code using [smart casts](smart_casts) 4 | and [when](when) expression: 5 | 6 | ```java 7 | public int eval(Expr expr) { 8 | if (expr instanceof Num) { 9 | return ((Num) expr).getValue(); 10 | } 11 | if (expr instanceof Sum) { 12 | Sum sum = (Sum) expr; 13 | return eval(sum.getLeft()) + eval(sum.getRight()); 14 | } 15 | throw new IllegalArgumentException("Unknown expression"); 16 | } 17 | ``` -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task9/tests.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Test 2 | import org.junit.Assert 3 | 4 | class TestSmartCasts { 5 | @Test fun testNum() { 6 | Assert.assertEquals("'eval' on Num should work:", 2, eval(Num(2))) 7 | } 8 | 9 | @Test fun testSum() { 10 | Assert.assertEquals("'eval' on Sum should work:", 3, eval(Sum(Num(2), Num(1)))) 11 | } 12 | 13 | @Test fun testRecursion() { 14 | Assert.assertEquals("'eval' should work:", 6, eval(Sum(Sum(Num(1), Num(2)), Num(3)))) 15 | } 16 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Object expressions/task.md: -------------------------------------------------------------------------------- 1 | ## Object expressions 2 | 3 | Read about [object expressions](http://kotlinlang.org/docs/reference/object-declarations.html) 4 | that play the same role in Kotlin as anonymous classes in Java. 5 | 6 | Add an object expression that provides a comparator to sort a list in a descending order using java.util.Collections class. 7 | In Kotlin you use Kotlin library extensions instead of java.util.Collections, 8 | but this example is still a good demonstration of mixing Kotlin and Java code. -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Properties/Delegates how it works/Test.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Assert 2 | import org.junit.Test 3 | 4 | class TestDelegatesHowItWorks { 5 | @Test fun testDate() { 6 | val d = D() 7 | d.date = MyDate(2014, 1, 13) 8 | val message = "The methods 'getValue' and 'setValue' are implemented incorrectly" 9 | Assert.assertTrue(message, 2014 == d.date.year) 10 | Assert.assertTrue(message, 1 == d.date.month) 11 | Assert.assertTrue(message, 13 == d.date.dayOfMonth) 12 | } 13 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Lambdas/Test.kt: -------------------------------------------------------------------------------- 1 | package introduction.lambdas 2 | 3 | import org.junit.Test 4 | import org.junit.Assert 5 | 6 | class TestLambdas() { 7 | @Test fun contains() { 8 | Assert.assertTrue("The result should be true if the collection contains an even number", containsEven(listOf(1, 2, 3, 126, 555))) 9 | } 10 | 11 | @Test fun notContains() { 12 | Assert.assertFalse("The result should be false if the collection doesn't contain an even number", containsEven(listOf(43, 33))) 13 | } 14 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Smart casts/Solution.kt: -------------------------------------------------------------------------------- 1 | package introduction.smart_casts 2 | 3 | /**/fun eval(expr: Expr): Int = 4 | when (expr) { 5 | is Num -> /**/expr.value/**/ 6 | is Sum -> /**/eval(expr.left) + eval(expr.right)/**/ 7 | else -> throw IllegalArgumentException("Unknown expression") 8 | }/**/ 9 | 10 | interface Expr 11 | class Num(val value: Int) : Expr 12 | class Sum(val left: Expr, val right: Expr) : Expr 13 | -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson5/task4/tests.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Assert 2 | import org.junit.Test 3 | 4 | class TestHtmlBuilders { 5 | @Test fun productTableIsFilled() { 6 | val result = renderProductTable() 7 | Assert.assertTrue("Product table should contain the corresponding data", result.contains("cactus")) 8 | } 9 | 10 | @Test fun productTableIsColored() { 11 | val result = renderProductTable() 12 | Assert.assertTrue("Product table should be colored", result.contains("bgcolor")) 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Smart casts/Test.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Test 2 | import org.junit.Assert 3 | 4 | class TestSmartCasts { 5 | @Test fun testNum() { 6 | Assert.assertEquals("'eval' on Num should work:", 2, eval(Num(2))) 7 | } 8 | 9 | @Test fun testSum() { 10 | Assert.assertEquals("'eval' on Sum should work:", 3, eval(Sum(Num(2), Num(1)))) 11 | } 12 | 13 | @Test fun testRecursion() { 14 | Assert.assertEquals("'eval' should work:", 6, eval(Sum(Sum(Num(1), Num(2)), Num(3)))) 15 | } 16 | } -------------------------------------------------------------------------------- /Kotlin Koans/Builders/The function apply/Task.kt.txt: -------------------------------------------------------------------------------- 1 | fun T.myApply(f: T.() -> Unit): T { TODO() } 2 | 3 | fun createString(): String { 4 | return StringBuilder().myApply { 5 | append("Numbers: ") 6 | for (i in 1..10) { 7 | append(i) 8 | } 9 | }.toString() 10 | } 11 | 12 | fun createMap(): Map { 13 | return hashMapOf().myApply { 14 | put(0, "0") 15 | for (i in 1..10) { 16 | put(i, "$i") 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/Operators overloading/Task.kt.txt: -------------------------------------------------------------------------------- 1 | import TimeInterval.* 2 | 3 | data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) 4 | 5 | enum class TimeInterval { DAY, WEEK, YEAR } 6 | 7 | operator fun MyDate.plus(timeInterval: TimeInterval): MyDate = TODO() 8 | 9 | fun task1(today: MyDate): MyDate { 10 | return today + YEAR + WEEK 11 | } 12 | 13 | fun task2(today: MyDate): MyDate { 14 | TODO("Uncomment") //return today + YEAR * 2 + WEEK * 3 + DAY * 5 15 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task4/tests.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Assert 2 | import org.junit.Test 3 | import koans.util.toMessage 4 | 5 | class TestFlatMap { 6 | @Test fun testGetOrderedProductsSet() { 7 | Assert.assertEquals("getOrderedProducts".toMessage(), 8 | setOf(idea), customers[reka]!!.orderedProducts) 9 | } 10 | 11 | @Test fun testGetAllOrderedProducts() { 12 | Assert.assertEquals("getAllOrderedProducts".toMessage(), 13 | orderedProducts, shop.allOrderedProducts) 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson4/task4/Task.kt: -------------------------------------------------------------------------------- 1 | import kotlin.properties.ReadWriteProperty 2 | import kotlin.reflect.KProperty 3 | 4 | class D { 5 | var date: MyDate by EffectiveDate() 6 | } 7 | 8 | class EffectiveDate : ReadWriteProperty { 9 | 10 | var timeInMillis: Long? = null 11 | 12 | override fun getValue(thisRef: R, property: KProperty<*>): MyDate { 13 | TODO() 14 | } 15 | 16 | override fun setValue(thisRef: R, property: KProperty<*>, value: MyDate) { 17 | TODO() 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Builders/The function apply/Task.kt: -------------------------------------------------------------------------------- 1 | fun T.myApply(f: T.() -> Unit): T { TODO() } 2 | 3 | fun createString(): String { 4 | return StringBuilder().myApply { 5 | append("Numbers: ") 6 | for (i in 1..10) { 7 | append(i) 8 | } 9 | }.toString() 10 | } 11 | 12 | fun createMap(): Map { 13 | return hashMapOf().myApply { 14 | put(0, "0") 15 | for (i in 1..10) { 16 | put(i, "$i") 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Filter map/task.md: -------------------------------------------------------------------------------- 1 | ## Filter; map 2 | 3 | Implement extension functions `Shop.getCitiesCustomersAreFrom()` and `Shop.getCustomersFrom()` using functions 4 | [`map`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/kotlin.-iterable/map.html) and 5 | [`filter`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/kotlin.-iterable/filter.html). 6 | 7 | ```kotlin 8 | val numbers = listOf(1, -1, 2) 9 | numbers.filter { it > 0 } == listOf(1, 2) 10 | numbers.map { it * it } == listOf(1, 1, 4) 11 | ``` -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/Operators overloading/Task.kt: -------------------------------------------------------------------------------- 1 | import TimeInterval.* 2 | 3 | data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) 4 | 5 | enum class TimeInterval { DAY, WEEK, YEAR } 6 | 7 | operator fun MyDate.plus(timeInterval: TimeInterval): MyDate = TODO() 8 | 9 | fun task1(today: MyDate): MyDate { 10 | return today + YEAR + WEEK 11 | } 12 | 13 | fun task2(today: MyDate): MyDate { 14 | TODO("Uncomment") //return today + YEAR * 2 + WEEK * 3 + DAY * 5 15 | } -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_ow2_asm_asm_5_0_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Builders/Html builders/Test.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Assert 2 | import org.junit.Test 3 | 4 | class TestHtmlBuilders { 5 | @Test fun productTableIsFilled() { 6 | val result = renderProductTable() 7 | Assert.assertTrue("Product table should contain the corresponding data", result.contains("cactus")) 8 | } 9 | 10 | @Test fun productTableIsColored() { 11 | val result = renderProductTable() 12 | Assert.assertTrue("Product table should be colored", result.contains("bgcolor")) 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Builders/The function apply/Solution.kt: -------------------------------------------------------------------------------- 1 | fun T.myApply(f: T.() -> Unit): T { f(); return this } 2 | 3 | fun createString(): String { 4 | return StringBuilder().myApply { 5 | append("Numbers: ") 6 | for (i in 1..10) { 7 | append(i) 8 | } 9 | }.toString() 10 | } 11 | 12 | fun createMap(): Map { 13 | return hashMapOf().myApply { 14 | put(0, "0") 15 | for (i in 1..10) { 16 | put(i, "$i") 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/FlatMap/Test.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Assert 2 | import org.junit.Test 3 | import koans.util.toMessage 4 | 5 | class TestFlatMap { 6 | @Test fun testGetOrderedProductsSet() { 7 | Assert.assertEquals("getOrderedProducts".toMessage(), 8 | setOf(idea), customers[reka]!!.orderedProducts) 9 | } 10 | 11 | @Test fun testGetAllOrderedProducts() { 12 | Assert.assertEquals("getAllOrderedProducts".toMessage(), 13 | orderedProducts, shop.allOrderedProducts) 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Introduction/task.md: -------------------------------------------------------------------------------- 1 | ## Introduction 2 | 3 | This part was inspired by [GS Collections Kata](gs_collections_kata). 4 | 5 | Default collections in Kotlin are Java collections, 6 | but there are lots of useful extension functions for them. 7 | For example, operations that transform a collection to another one, starting with 'to': 8 | [`toSet`](to-set) or 9 | [`toList`](to-list). 10 | 11 | Implement an extension function `Shop.getSetOfCustomers()`. 12 | The class `Shop` and all related classes can be found at [Shop.kt](shop_file). -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson2/task2/MyDate.kt: -------------------------------------------------------------------------------- 1 | import java.util.Calendar 2 | 3 | data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) 4 | 5 | fun MyDate.toMillis(): Long { 6 | val c = Calendar.getInstance() 7 | c.set(year, month, dayOfMonth, 0, 0, 0) 8 | c.set(Calendar.MILLISECOND, 0) 9 | return c.getTimeInMillis() 10 | } 11 | 12 | fun Long.toDate(): MyDate { 13 | val c = Calendar.getInstance() 14 | c.setTimeInMillis(this) 15 | return MyDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE)) 16 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson2/task3/MyDate.kt: -------------------------------------------------------------------------------- 1 | import java.util.Calendar 2 | 3 | data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) 4 | 5 | fun MyDate.toMillis(): Long { 6 | val c = Calendar.getInstance() 7 | c.set(year, month, dayOfMonth, 0, 0, 0) 8 | c.set(Calendar.MILLISECOND, 0) 9 | return c.getTimeInMillis() 10 | } 11 | 12 | fun Long.toDate(): MyDate { 13 | val c = Calendar.getInstance() 14 | c.setTimeInMillis(this) 15 | return MyDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE)) 16 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson2/task3/task.html: -------------------------------------------------------------------------------- 1 |

Range to

2 |

Implement the function MyDate.rangeTo(). This allows you to create a range of dates using the following syntax:

MyDate(2015, 5, 11)..MyDate(2015, 5, 12)
3 | 
4 |

Note that now the class DateRange implements the standard ClosedRange interface and inherits contains method implementation.

-------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson2/task4/MyDate.kt: -------------------------------------------------------------------------------- 1 | import java.util.Calendar 2 | 3 | data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) 4 | 5 | fun MyDate.toMillis(): Long { 6 | val c = Calendar.getInstance() 7 | c.set(year, month, dayOfMonth, 0, 0, 0) 8 | c.set(Calendar.MILLISECOND, 0) 9 | return c.getTimeInMillis() 10 | } 11 | 12 | fun Long.toDate(): MyDate { 13 | val c = Calendar.getInstance() 14 | c.setTimeInMillis(this) 15 | return MyDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE)) 16 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson2/task5/task.html: -------------------------------------------------------------------------------- 1 |

Operators overloading

2 |

Implement a kind of date arithmetic. Support adding years, weeks and days to a date. You could be able to write the code like this: date + YEAR * 2 + WEEK * 3 + DAY * 15.

3 |

At first, add an extension function 'plus()' to MyDate, taking a TimeInterval as an argument. Use an utility function MyDate.addTimeIntervals() declared in DateUtil.kt

4 |

Then, try to support adding several time intervals to a date. You may need an extra class.

-------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task1/Shop.kt: -------------------------------------------------------------------------------- 1 | data class Shop(val name: String, val customers: List) 2 | 3 | data class Customer(val name: String, val city: City, val orders: List) { 4 | override fun toString() = "$name from ${city.name}" 5 | } 6 | 7 | data class Order(val products: List, val isDelivered: Boolean) 8 | 9 | data class Product(val name: String, val price: Double) { 10 | override fun toString() = "'$name' for $price" 11 | } 12 | 13 | data class City(val name: String) { 14 | override fun toString() = name 15 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task2/Shop.kt: -------------------------------------------------------------------------------- 1 | data class Shop(val name: String, val customers: List) 2 | 3 | data class Customer(val name: String, val city: City, val orders: List) { 4 | override fun toString() = "$name from ${city.name}" 5 | } 6 | 7 | data class Order(val products: List, val isDelivered: Boolean) 8 | 9 | data class Product(val name: String, val price: Double) { 10 | override fun toString() = "'$name' for $price" 11 | } 12 | 13 | data class City(val name: String) { 14 | override fun toString() = name 15 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task3/Shop.kt: -------------------------------------------------------------------------------- 1 | data class Shop(val name: String, val customers: List) 2 | 3 | data class Customer(val name: String, val city: City, val orders: List) { 4 | override fun toString() = "$name from ${city.name}" 5 | } 6 | 7 | data class Order(val products: List, val isDelivered: Boolean) 8 | 9 | data class Product(val name: String, val price: Double) { 10 | override fun toString() = "'$name' for $price" 11 | } 12 | 13 | data class City(val name: String) { 14 | override fun toString() = name 15 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task4/Shop.kt: -------------------------------------------------------------------------------- 1 | data class Shop(val name: String, val customers: List) 2 | 3 | data class Customer(val name: String, val city: City, val orders: List) { 4 | override fun toString() = "$name from ${city.name}" 5 | } 6 | 7 | data class Order(val products: List, val isDelivered: Boolean) 8 | 9 | data class Product(val name: String, val price: Double) { 10 | override fun toString() = "'$name' for $price" 11 | } 12 | 13 | data class City(val name: String) { 14 | override fun toString() = name 15 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task5/Shop.kt: -------------------------------------------------------------------------------- 1 | data class Shop(val name: String, val customers: List) 2 | 3 | data class Customer(val name: String, val city: City, val orders: List) { 4 | override fun toString() = "$name from ${city.name}" 5 | } 6 | 7 | data class Order(val products: List, val isDelivered: Boolean) 8 | 9 | data class Product(val name: String, val price: Double) { 10 | override fun toString() = "'$name' for $price" 11 | } 12 | 13 | data class City(val name: String) { 14 | override fun toString() = name 15 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task6/Shop.kt: -------------------------------------------------------------------------------- 1 | data class Shop(val name: String, val customers: List) 2 | 3 | data class Customer(val name: String, val city: City, val orders: List) { 4 | override fun toString() = "$name from ${city.name}" 5 | } 6 | 7 | data class Order(val products: List, val isDelivered: Boolean) 8 | 9 | data class Product(val name: String, val price: Double) { 10 | override fun toString() = "'$name' for $price" 11 | } 12 | 13 | data class City(val name: String) { 14 | override fun toString() = name 15 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task7/Shop.kt: -------------------------------------------------------------------------------- 1 | data class Shop(val name: String, val customers: List) 2 | 3 | data class Customer(val name: String, val city: City, val orders: List) { 4 | override fun toString() = "$name from ${city.name}" 5 | } 6 | 7 | data class Order(val products: List, val isDelivered: Boolean) 8 | 9 | data class Product(val name: String, val price: Double) { 10 | override fun toString() = "'$name' for $price" 11 | } 12 | 13 | data class City(val name: String) { 14 | override fun toString() = name 15 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task8/Shop.kt: -------------------------------------------------------------------------------- 1 | data class Shop(val name: String, val customers: List) 2 | 3 | data class Customer(val name: String, val city: City, val orders: List) { 4 | override fun toString() = "$name from ${city.name}" 5 | } 6 | 7 | data class Order(val products: List, val isDelivered: Boolean) 8 | 9 | data class Product(val name: String, val price: Double) { 10 | override fun toString() = "'$name' for $price" 11 | } 12 | 13 | data class City(val name: String) { 14 | override fun toString() = name 15 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task8/task.html: -------------------------------------------------------------------------------- 1 |

Group By

2 |

Implement Shop.groupCustomersByCity() using groupBy.

val result = listOf("a", "b", "ba", "ccc", "ad").groupBy { it.length() }
3 | result == mapOf(1 to listOf("a", "b"), 2 to listOf("ba", "ad"), 3 to listOf("ccc"))
4 | 
-------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task9/Shop.kt: -------------------------------------------------------------------------------- 1 | data class Shop(val name: String, val customers: List) 2 | 3 | data class Customer(val name: String, val city: City, val orders: List) { 4 | override fun toString() = "$name from ${city.name}" 5 | } 6 | 7 | data class Order(val products: List, val isDelivered: Boolean) 8 | 9 | data class Product(val name: String, val price: Double) { 10 | override fun toString() = "'$name' for $price" 11 | } 12 | 13 | data class City(val name: String) { 14 | override fun toString() = name 15 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson4/task4/MyDate.kt: -------------------------------------------------------------------------------- 1 | import java.util.Calendar 2 | 3 | data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) 4 | 5 | fun MyDate.toMillis(): Long { 6 | val c = Calendar.getInstance() 7 | c.set(year, month, dayOfMonth, 0, 0, 0) 8 | c.set(Calendar.MILLISECOND, 0) 9 | return c.getTimeInMillis() 10 | } 11 | 12 | fun Long.toDate(): MyDate { 13 | val c = Calendar.getInstance() 14 | c.setTimeInMillis(this) 15 | return MyDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE)) 16 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Shop.kt: -------------------------------------------------------------------------------- 1 | data class Shop(val name: String, val customers: List) 2 | 3 | data class Customer(val name: String, val city: City, val orders: List) { 4 | override fun toString() = "$name from ${city.name}" 5 | } 6 | 7 | data class Order(val products: List, val isDelivered: Boolean) 8 | 9 | data class Product(val name: String, val price: Double) { 10 | override fun toString() = "'$name' for $price" 11 | } 12 | 13 | data class City(val name: String) { 14 | override fun toString() = name 15 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Introduction/Lambdas/task.md: -------------------------------------------------------------------------------- 1 | ## Lambdas 2 | 3 | Kotlin supports a functional style of programming. 4 | Read about [higher-order functions and function literals (lambdas)](http://kotlinlang.org/docs/reference/lambdas.html) in Kotlin. 5 | 6 | Pass a lambda to [`any`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/kotlin.-iterable/any.html) 7 | function to check if the collection contains an even number. 8 | The function `any` gets a predicate as an argument and returns true if there is at least one element satisfying the predicate. -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Compound tasks/Task.kt.txt: -------------------------------------------------------------------------------- 1 | // Return the most expensive product among all delivered products 2 | // (use the Order.isDelivered flag) 3 | fun Customer.getMostExpensiveDeliveredProduct(): Product? { 4 | TODO() 5 | } 6 | 7 | // Return how many times the given product was ordered. 8 | // Note: a customer may order the same product for several times. 9 | fun Shop.getNumberOfTimesProductWasOrdered(product: Product): Int { 10 | TODO() 11 | } 12 | 13 | /* TODO */ -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task10/Shop.kt: -------------------------------------------------------------------------------- 1 | data class Shop(val name: String, val customers: List) 2 | 3 | data class Customer(val name: String, val city: City, val orders: List) { 4 | override fun toString() = "$name from ${city.name}" 5 | } 6 | 7 | data class Order(val products: List, val isDelivered: Boolean) 8 | 9 | data class Product(val name: String, val price: Double) { 10 | override fun toString() = "'$name' for $price" 11 | } 12 | 13 | data class City(val name: String) { 14 | override fun toString() = name 15 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task11/Shop.kt: -------------------------------------------------------------------------------- 1 | data class Shop(val name: String, val customers: List) 2 | 3 | data class Customer(val name: String, val city: City, val orders: List) { 4 | override fun toString() = "$name from ${city.name}" 5 | } 6 | 7 | data class Order(val products: List, val isDelivered: Boolean) 8 | 9 | data class Product(val name: String, val price: Double) { 10 | override fun toString() = "'$name' for $price" 11 | } 12 | 13 | data class City(val name: String) { 14 | override fun toString() = name 15 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task12/Shop.kt: -------------------------------------------------------------------------------- 1 | data class Shop(val name: String, val customers: List) 2 | 3 | data class Customer(val name: String, val city: City, val orders: List) { 4 | override fun toString() = "$name from ${city.name}" 5 | } 6 | 7 | data class Order(val products: List, val isDelivered: Boolean) 8 | 9 | data class Product(val name: String, val price: Double) { 10 | override fun toString() = "'$name' for $price" 11 | } 12 | 13 | data class City(val name: String) { 14 | override fun toString() = name 15 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/Comparison/task.md: -------------------------------------------------------------------------------- 1 | ## Comparison 2 | 3 | Read about [operator overloading](http://kotlinlang.org/docs/reference/operator-overloading.html) 4 | to learn how different conventions for operations like `==`, `<`, `+` work in Kotlin. 5 | Add the function `compareTo` to the class MyDate to make it comparable. 6 | After that the code below `date1 < date2` will start to compile. 7 | 8 | In Kotlin when you override a member, the modifier 9 | [`override`](http://kotlinlang.org/docs/reference/classes.html#overriding-members) is mandatory. 10 | -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Get used to new style/Solution.kt: -------------------------------------------------------------------------------- 1 | package collections.get_used_to_new_style 2 | 3 | fun doSomethingStrangeWithCollection(collection: Collection): Collection? { 4 | 5 | val groupsByLength = collection. groupBy { s -> /**/s.length/**/ } 6 | 7 | val maximumSizeOfGroup = groupsByLength.values.map { group -> /**/group.size/**/ }.max() 8 | 9 | return groupsByLength.values.firstOrNull { group -> /**/group.size == maximumSizeOfGroup/**/ } 10 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson2/task4/DateUtil.kt: -------------------------------------------------------------------------------- 1 | import java.util.Calendar 2 | 3 | fun MyDate.addTimeIntervals(timeInterval: TimeInterval, number: Int): MyDate { 4 | val c = Calendar.getInstance() 5 | c.set(year, month, dayOfMonth) 6 | when (timeInterval) { 7 | TimeInterval.DAY -> c.add(Calendar.DAY_OF_MONTH, number) 8 | TimeInterval.WEEK -> c.add(Calendar.WEEK_OF_MONTH, number) 9 | TimeInterval.YEAR -> c.add(Calendar.YEAR, number) 10 | } 11 | return MyDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE)) 12 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson2/task5/DateUtil.kt: -------------------------------------------------------------------------------- 1 | import java.util.Calendar 2 | 3 | fun MyDate.addTimeIntervals(timeInterval: TimeInterval, number: Int): MyDate { 4 | val c = Calendar.getInstance() 5 | c.set(year, month, dayOfMonth) 6 | when (timeInterval) { 7 | TimeInterval.DAY -> c.add(Calendar.DAY_OF_MONTH, number) 8 | TimeInterval.WEEK -> c.add(Calendar.WEEK_OF_MONTH, number) 9 | TimeInterval.YEAR -> c.add(Calendar.YEAR, number) 10 | } 11 | return MyDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE)) 12 | } -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task3/Task.kt: -------------------------------------------------------------------------------- 1 | // Return true if all customers are from the given city 2 | fun Shop.checkAllCustomersAreFrom(city: City): Boolean = TODO() 3 | 4 | // Return true if there is at least one customer from the given city 5 | fun Shop.hasCustomerFrom(city: City): Boolean = TODO() 6 | 7 | // Return the number of customers from the given city 8 | fun Shop.countCustomersFrom(city: City): Int = TODO() 9 | 10 | // Return a customer who lives in the given city, or null if there is none 11 | fun Shop.findAnyCustomerFrom(city: City): Customer? = TODO() -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task7/tests.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Assert 2 | import org.junit.Test 3 | import koans.util.toMessage 4 | 5 | class TestSum { 6 | @Test fun testGetTotalOrderPrice() { 7 | Assert.assertEquals("getTotalOrderPrice".toMessage(), 8 | 148.0, customers[nathan]!!.getTotalOrderPrice(), 0.001) 9 | } 10 | 11 | @Test fun testTotalPriceForRepeatedProducts() { 12 | Assert.assertEquals("getTotalOrderPrice".toMessage(), 13 | 586.0, customers[lucas]!!.getTotalOrderPrice(), 0.001) 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Sum/Test.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Assert 2 | import org.junit.Test 3 | import koans.util.toMessage 4 | 5 | class TestSum { 6 | @Test fun testGetTotalOrderPrice() { 7 | Assert.assertEquals("getTotalOrderPrice".toMessage(), 8 | 148.0, customers[nathan]!!.getTotalOrderPrice(), 0.001) 9 | } 10 | 11 | @Test fun testTotalPriceForRepeatedProducts() { 12 | Assert.assertEquals("getTotalOrderPrice".toMessage(), 13 | 586.0, customers[lucas]!!.getTotalOrderPrice(), 0.001) 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Kotlin Koans/Conventions/Operators overloading/task.md: -------------------------------------------------------------------------------- 1 | ## Operators overloading 2 | 3 | Implement a kind of date arithmetic. Support adding years, weeks and days to a date. 4 | You could be able to write the code like this: `date + YEAR * 2 + WEEK * 3 + DAY * 15`. 5 | 6 | At first, add an extension function 'plus()' to MyDate, taking a TimeInterval as an argument. 7 | Use an utility function `MyDate.addTimeIntervals()` declared in 8 | [DateUtil.kt](date_util_file2) 9 | 10 | Then, try to support adding several time intervals to a date. 11 | You may need an extra class. -------------------------------------------------------------------------------- /Kotlin Koans/Generics/Generic functions/task.md: -------------------------------------------------------------------------------- 1 | ## Generic functions 2 | 3 | Make the following code compile by implementing a `partitionTo` function that splits 4 | a collection into two collections according to the predicate. 5 | 6 | There is a [`partition()`](partition) 7 | function in the standard library that always returns two newly created lists. 8 | You should write a function that splits the collection into two collections given as arguments. 9 | The signature of the 10 | [`toCollection()`](to-collection) 11 | function from the standard library may help you. -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Properties/Delegates how it works/MyDate.kt: -------------------------------------------------------------------------------- 1 | import java.util.Calendar 2 | 3 | data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) 4 | 5 | fun MyDate.toMillis(): Long { 6 | val c = Calendar.getInstance() 7 | c.set(year, month, dayOfMonth, 0, 0, 0) 8 | c.set(Calendar.MILLISECOND, 0) 9 | return c.getTimeInMillis() 10 | } 11 | 12 | fun Long.toDate(): MyDate { 13 | val c = Calendar.getInstance() 14 | c.setTimeInMillis(this) 15 | return MyDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE)) 16 | } -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Shop.kt: -------------------------------------------------------------------------------- 1 | package collections 2 | 3 | data class Shop(val name: String, val customers: List) 4 | 5 | data class Customer(val name: String, val city: City, val orders: List) { 6 | override fun toString() = "$name from ${city.name}" 7 | } 8 | 9 | data class Order(val products: List, val isDelivered: Boolean) 10 | 11 | data class Product(val name: String, val price: Double) { 12 | override fun toString() = "'$name' for $price" 13 | } 14 | 15 | data class City(val name: String) { 16 | override fun toString() = name 17 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Smart casts/Test.kt: -------------------------------------------------------------------------------- 1 | package introduction.smart_casts 2 | 3 | import org.junit.Test 4 | import org.junit.Assert 5 | 6 | class TestSmartCasts { 7 | @Test fun testNum() { 8 | Assert.assertEquals("'eval' on Num should work:", 2, eval(Num(2))) 9 | } 10 | 11 | @Test fun testSum() { 12 | Assert.assertEquals("'eval' on Sum should work:", 3, eval(Sum(Num(2), Num(1)))) 13 | } 14 | 15 | @Test fun testRecursion() { 16 | Assert.assertEquals("'eval' should work:", 6, eval(Sum(Sum(Num(1), Num(2)), Num(3)))) 17 | } 18 | } -------------------------------------------------------------------------------- /Kotlin Koans/Properties/Delegates how it works/Test.kt: -------------------------------------------------------------------------------- 1 | package properties.delegates_how_it_works 2 | 3 | import org.junit.Assert 4 | import org.junit.Test 5 | 6 | class TestDelegatesHowItWorks { 7 | @Test fun testDate() { 8 | val d = D() 9 | d.date = MyDate(2014, 1, 13) 10 | val message = "The methods 'getValue' and 'setValue' are implemented incorrectly" 11 | Assert.assertTrue(message, 2014 == d.date.year) 12 | Assert.assertTrue(message, 1 == d.date.month) 13 | Assert.assertTrue(message, 13 == d.date.dayOfMonth) 14 | } 15 | } -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_pegdown_pegdown_1_6_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Kotlin Koans/Builders/Html builders/Test.kt: -------------------------------------------------------------------------------- 1 | package builders.html_builders 2 | 3 | import org.junit.Assert 4 | import org.junit.Test 5 | 6 | class TestHtmlBuilders { 7 | @Test fun productTableIsFilled() { 8 | val result = renderProductTable() 9 | Assert.assertTrue("Product table should contain the corresponding data", result.contains("cactus")) 10 | } 11 | 12 | @Test fun productTableIsColored() { 13 | val result = renderProductTable() 14 | Assert.assertTrue("Product table should be colored", result.contains("bgcolor")) 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__com_google_code_gson_gson_2_5.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Partition/task.md: -------------------------------------------------------------------------------- 1 | ## Partition 2 | Implement `Shop.getCustomersWithMoreUndeliveredOrdersThanDelivered()` using 3 | [`partition`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/kotlin.-iterable/partition.html). 4 | 5 | ```kotlin 6 | val numbers = listOf(1, 3, -4, 2, -11) 7 | val (positive, negative) = numbers.partition { it > 0 } 8 | positive == listOf(1, 3, 2) 9 | negative == listOf(-4, -11) 10 | ``` 11 | 12 | Note that [destructuring declaration](http://kotlinlang.org/docs/reference/multi-declarations.html) syntax is used in this example. -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/Operators overloading/DateUtil.kt: -------------------------------------------------------------------------------- 1 | import java.util.Calendar 2 | 3 | fun MyDate.addTimeIntervals(timeInterval: TimeInterval, number: Int): MyDate { 4 | val c = Calendar.getInstance() 5 | c.set(year, month, dayOfMonth) 6 | when (timeInterval) { 7 | TimeInterval.DAY -> c.add(Calendar.DAY_OF_MONTH, number) 8 | TimeInterval.WEEK -> c.add(Calendar.WEEK_OF_MONTH, number) 9 | TimeInterval.YEAR -> c.add(Calendar.YEAR, number) 10 | } 11 | return MyDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE)) 12 | } -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Java to Kotlin conversion/Solution.kt: -------------------------------------------------------------------------------- 1 | package introduction.java_to_kotlin_conversion 2 | 3 | /**/fun toJSON(collection: Collection): String { 4 | val sb = StringBuilder() 5 | sb.append("[") 6 | val iterator = collection.iterator() 7 | while (iterator.hasNext()) 8 | { 9 | val element = iterator.next() 10 | sb.append(element) 11 | if (iterator.hasNext()) 12 | { 13 | sb.append(", ") 14 | } 15 | } 16 | sb.append("]") 17 | return sb.toString() 18 | }/**/ 19 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_ow2_asm_asm_tree_5_0_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_ow2_asm_asm_util_5_0_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson1/task5/task.html: -------------------------------------------------------------------------------- 1 |

Lambdas

2 |

Kotlin supports a functional style of programming. Read about higher-order functions and function literals (lambdas) in Kotlin.

3 |

Pass a lambda to any function to check if the collection contains an even number. The function any gets a predicate as an argument and returns true if there is at least one element satisfying the predicate.

-------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Nullable types/Solution.kt: -------------------------------------------------------------------------------- 1 | package introduction.nullable_types 2 | 3 | /**/fun sendMessageToClient( 4 | client: Client?, message: String?, mailer: Mailer 5 | ) { 6 | /**/val email = client?.personalInfo?.email 7 | if (email != null && message != null) { 8 | mailer.sendMessage(email, message) 9 | }/**/ 10 | }/**/ 11 | 12 | class Client (val personalInfo: PersonalInfo?) 13 | class PersonalInfo (val email: String?) 14 | interface Mailer { 15 | fun sendMessage(email: String, message: String) 16 | } -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Collections/Sum/task.md: -------------------------------------------------------------------------------- 1 | ## Sum 2 | 3 | Implement `Customer.getTotalOrderPrice()` using 4 | [`sum`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/kotlin.-iterable/sum.html) or 5 | [`sumBy`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/kotlin.-iterable/sum-by.html). 6 | 7 | ```kotlin 8 | listOf(1, 5, 3).sum() == 9 9 | listOf("a", "b", "cc").sumBy { it.length() } == 4 10 | ``` 11 | 12 | If you want to sum the double values, use 13 | [`sumByDouble`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/kotlin.-iterable/sum-by-double.html). -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson2/task2/task.html: -------------------------------------------------------------------------------- 1 |

In range

2 |

In Kotlin in checks are translated to the corresponding contains calls:

val list = listOf("a", "b")
3 | "a" in list  // list.contains("a")
4 | "a" !in list // !list.contains("a")
5 | 
6 |

Read about ranges. Add a method fun contains(d: MyDate) to the class DateRange to allow in checks with a range of dates.

-------------------------------------------------------------------------------- /Kotlin Koans/Collections/FlatMap/Test.kt: -------------------------------------------------------------------------------- 1 | package collections.flatmap 2 | 3 | import org.junit.Assert 4 | import org.junit.Test 5 | import koans.util.toMessage 6 | import collections.* 7 | 8 | class TestFlatMap { 9 | @Test fun testGetOrderedProductsSet() { 10 | Assert.assertEquals("getOrderedProducts".toMessage(), 11 | setOf(idea), customers[reka]!!.orderedProducts) 12 | } 13 | 14 | @Test fun testGetAllOrderedProducts() { 15 | Assert.assertEquals("getAllOrderedProducts".toMessage(), 16 | orderedProducts, shop.allOrderedProducts) 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Kotlin Koans/Properties/Delegates how it works/Task.kt.txt: -------------------------------------------------------------------------------- 1 | import kotlin.properties.ReadWriteProperty 2 | import kotlin.reflect.KProperty 3 | 4 | class D { 5 | var date: MyDate by EffectiveDate() 6 | } 7 | 8 | class EffectiveDate : ReadWriteProperty { 9 | 10 | var timeInMillis: Long? = null 11 | 12 | override fun getValue(thisRef: R, property: KProperty<*>): MyDate { 13 | TODO() 14 | } 15 | 16 | override fun setValue(thisRef: R, property: KProperty<*>, value: MyDate) { 17 | TODO() 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Properties/Delegates how it works/Task.kt: -------------------------------------------------------------------------------- 1 | import kotlin.properties.ReadWriteProperty 2 | import kotlin.reflect.KProperty 3 | 4 | class D { 5 | var date: MyDate by EffectiveDate() 6 | } 7 | 8 | class EffectiveDate : ReadWriteProperty { 9 | 10 | var timeInMillis: Long? = null 11 | 12 | override fun getValue(thisRef: R, property: KProperty<*>): MyDate { 13 | TODO() 14 | } 15 | 16 | override fun setValue(thisRef: R, property: KProperty<*>, value: MyDate) { 17 | TODO() 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_jetbrains_annotations_13_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Kotlin Koans/Introduction/Extensions on collections/task.md: -------------------------------------------------------------------------------- 1 | ## Extension functions on collections 2 | 3 | Kotlin code can be easily mixed with Java code. 4 | Thus in Kotlin we don't introduce our own collections, but use standard Java ones (slightly improved). 5 | Read about [read-only and mutable views on Java collections](read-only_and_mutable_collections). 6 | 7 | In [Kotlin standard library](kotlin_stdlib) 8 | there are lots of extension functions that make the work with collections more convenient. 9 | Rewrite the previous example once more using an extension function 10 | [`sortedDescending`](sorted_descending). -------------------------------------------------------------------------------- /Kotlin Koans/Properties/Delegates how it works/MyDate.kt: -------------------------------------------------------------------------------- 1 | package properties.delegates_how_it_works 2 | 3 | import java.util.Calendar 4 | 5 | data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) 6 | 7 | fun MyDate.toMillis(): Long { 8 | val c = Calendar.getInstance() 9 | c.set(year, month, dayOfMonth, 0, 0, 0) 10 | c.set(Calendar.MILLISECOND, 0) 11 | return c.getTimeInMillis() 12 | } 13 | 14 | fun Long.toDate(): MyDate { 15 | val c = Calendar.getInstance() 16 | c.setTimeInMillis(this) 17 | return MyDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE)) 18 | } -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_ow2_asm_asm_analysis_5_0_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Conventions/Operators overloading/task.md: -------------------------------------------------------------------------------- 1 | ## Operators overloading 2 | 3 | Implement a kind of date arithmetic. Support adding years, weeks and days to a date. 4 | You could be able to write the code like this: `date + YEAR * 2 + WEEK * 3 + DAY * 15`. 5 | 6 | At first, add an extension function 'plus()' to MyDate, taking a TimeInterval as an argument. 7 | Use an utility function `MyDate.addTimeIntervals()` declared in 8 | [DateUtil.kt](/#/Kotlin%20Koans/Conventions/Operators%20overloading/DateUtil.kt) 9 | 10 | Then, try to support adding several time intervals to a date. 11 | You may need an extra class. -------------------------------------------------------------------------------- /Kotlin Koans/Collections/Sum/Test.kt: -------------------------------------------------------------------------------- 1 | package collections.sum 2 | 3 | import org.junit.Assert 4 | import org.junit.Test 5 | import koans.util.toMessage 6 | import collections.* 7 | 8 | class TestSum { 9 | @Test fun testGetTotalOrderPrice() { 10 | Assert.assertEquals("getTotalOrderPrice".toMessage(), 11 | 148.0, customers[nathan]!!.getTotalOrderPrice(), 0.001) 12 | } 13 | 14 | @Test fun testTotalPriceForRepeatedProducts() { 15 | Assert.assertEquals("getTotalOrderPrice".toMessage(), 16 | 586.0, customers[lucas]!!.getTotalOrderPrice(), 0.001) 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task2/task.html: -------------------------------------------------------------------------------- 1 |

Filter; map

2 |

Implement extension functions Shop.getCitiesCustomersAreFrom() and Shop.getCustomersFrom() using functions map and filter.

val numbers = listOf(1, -1, 2)
3 | numbers.filter { it > 0 } == listOf(1, 2)
4 | numbers.map { it * it } == listOf(1, 1, 4)
5 | 
-------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task2/tests.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Assert 2 | import org.junit.Test 3 | import koans.util.toMessage 4 | 5 | class TestFilterMap { 6 | @Test fun testCitiesCustomersAreFrom() { 7 | Assert.assertEquals("getCitiesCustomersAreFrom".toMessage(), 8 | setOf(Canberra, Vancouver, Budapest, Ankara, Tokyo), shop.getCitiesCustomersAreFrom()) 9 | } 10 | 11 | @Test fun testCustomersFromCity() { 12 | Assert.assertEquals("getCustomersFrom".toMessage(), 13 | listOf(customers[lucas], customers[cooper]), shop.getCustomersFrom(Canberra)) 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Kotlin Koans WebDemo/Properties/Delegates how it works/Solution.kt: -------------------------------------------------------------------------------- 1 | import kotlin.properties.ReadWriteProperty 2 | import kotlin.reflect.KProperty 3 | 4 | class D { 5 | var date: MyDate by EffectiveDate() 6 | } 7 | 8 | class EffectiveDate : ReadWriteProperty { 9 | 10 | var timeInMillis: Long? = null 11 | 12 | override fun getValue(thisRef: R, property: KProperty<*>): MyDate { 13 | return timeInMillis!!.toDate() 14 | } 15 | 16 | override fun setValue(thisRef: R, property: KProperty<*>, value: MyDate) { 17 | timeInMillis = value.toMillis() 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson2/task1/task.html: -------------------------------------------------------------------------------- 1 |

Comparison

2 |

Read about operator overloading to learn how different conventions for operations like ==, <, + work in Kotlin. Add the function compareTo to the class MyDate to make it comparable. After that the code below date1 < date2 will start to compile.

3 |

In Kotlin when you override a member, the modifier override is mandatory.

-------------------------------------------------------------------------------- /Kotlin Koans EE plugin/lesson3/task5/tests.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Assert 2 | import org.junit.Test 3 | import koans.util.toMessage 4 | 5 | class TestMaxMin { 6 | @Test fun testCustomerWithMaximumNumberOfOrders() { 7 | Assert.assertEquals("getCustomerWithMaximumNumberOfOrders".toMessage(), 8 | customers[reka], shop.getCustomerWithMaximumNumberOfOrders()) 9 | } 10 | 11 | @Test fun testTheMostExpensiveOrderedProduct() { 12 | Assert.assertEquals("getMostExpensiveOrderedProduct".toMessage(), 13 | rubyMine, customers[nathan]!!.getMostExpensiveOrderedProduct()) 14 | } 15 | } 16 | --------------------------------------------------------------------------------