├── .gitattributes ├── .gitignore ├── Chapter01 ├── 01_infix.kt ├── 01_smart_casts.kt ├── 01_stdlib.kt ├── 01_strings.kt └── 01_uppercase.kt ├── Chapter02 ├── 02_break_continue.kt ├── 02_break_continue_loop1.kt ├── 02_break_continue_loop2.kt ├── 02_break_continue_loop3.kt ├── 02_range.kt ├── 02_variable_declaration.kt ├── 02_when.kt ├── 02_when_02.kt └── 02_when_03.kt ├── Chapter03 ├── 03_Unit_type_function.kt ├── 03_default_args_function.kt ├── 03_function_declaration.kt ├── 03_local_function.kt ├── 03_retrun_type.kt ├── 03_returning_function.kt ├── 03_vararg.kt ├── 03_vararg_02.kt └── 03_vararg_03.kt ├── Chapter04 ├── 04_constructor.kt ├── 04_copy_method.kt ├── 04_custom_getters_setters.kt ├── 04_destructive_declaration.kt ├── 04_equals_hashcode.kt ├── 04_inheritance ├── 04_interface.kt ├── 04_multiple_interface.kt ├── 04_operator_overloading.kt └── 04_property_default.kt ├── Chapter05 ├── 05_companion_object.kt ├── 05_extension_function.kt ├── 05_member_function.kt └── 05_member_function_superclass.kt ├── Chapter06 ├── 06_covariance.kt ├── 06_filter_function.kt ├── 06_non-nullable_type_arg.kt ├── 06_reified_type_param.kt ├── 06_star_projections.kt ├── 06_type_erasure.kt ├── 06_type_mismatch_dec_example.kt └── 06_variance.kt ├── Chapter07 ├── 07_companion_object.kt ├── 07_data_class ├── 07_extensions.kt ├── 07_functions.kt ├── 07_let_function.kt ├── 07_no_method_overriding.kt ├── 07_standard_library_functions.kt └── 07_superclass.kt ├── Chapter08 ├── 08_delegation_pattern.kt ├── 08_delgation_pattern_02.kt ├── 08_lazy_function_Java.java ├── 08_map_type.kt ├── 08_observable_delgates.kt └── 08_property_delegation.kt ├── Chapter09 └── MarvelGallery_Source-code │ ├── .gitignore │ ├── .travis.yml │ ├── ColorImages │ ├── image_09_001.png │ ├── image_09_002.png │ ├── image_09_003.png │ ├── image_09_004.png │ ├── image_09_005.png │ ├── image_09_006.png │ ├── image_09_007.png │ ├── image_09_008.png │ ├── image_09_009.png │ ├── image_09_010.png │ ├── image_09_011.png │ ├── image_09_012.png │ ├── image_09_013.png │ ├── image_09_014.png │ ├── image_09_015.png │ ├── image_09_016.png │ ├── image_09_017.png │ ├── image_09_018.png │ ├── image_09_019.png │ ├── image_09_020.png │ └── image_09_021.png │ ├── app │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ │ ├── androidTest │ │ └── java │ │ │ └── com │ │ │ └── sample │ │ │ └── marvelgallery │ │ │ ├── CharacterProfileActivityTest.kt │ │ │ ├── CharacterProfileOpeningTest.kt │ │ │ └── MainActivityTest.kt │ │ ├── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── sample │ │ │ │ └── marvelgallery │ │ │ │ ├── data │ │ │ │ ├── MarvelRepository.kt │ │ │ │ ├── MarvelRepositoryImpl.kt │ │ │ │ ├── Provider.kt │ │ │ │ ├── RxExt.kt │ │ │ │ └── network │ │ │ │ │ ├── MarvelApi.kt │ │ │ │ │ ├── dto │ │ │ │ │ ├── CharacterMarvelDto.kt │ │ │ │ │ ├── DataContainer.kt │ │ │ │ │ ├── DataWrapper.kt │ │ │ │ │ ├── ImageDto.kt │ │ │ │ │ ├── ListWrapper.kt │ │ │ │ │ └── OccurrenceDto.kt │ │ │ │ │ └── providers │ │ │ │ │ ├── HeadersInterceptor.kt │ │ │ │ │ ├── LoggingInterceptor.kt │ │ │ │ │ ├── MD5.kt │ │ │ │ │ ├── QueryInterceptor.kt │ │ │ │ │ └── Retrofit.kt │ │ │ │ ├── model │ │ │ │ └── MarvelCharacter.kt │ │ │ │ ├── presenter │ │ │ │ ├── BasePresenter.kt │ │ │ │ ├── MainPresenter.kt │ │ │ │ └── Presenter.kt │ │ │ │ └── view │ │ │ │ ├── character │ │ │ │ └── CharacterProfileActivity.kt │ │ │ │ ├── common │ │ │ │ ├── BaseActivity.kt │ │ │ │ ├── BaseActivityWithPresenter.kt │ │ │ │ ├── BindToSwipeRefresh.kt │ │ │ │ ├── ItemAdapter.kt │ │ │ │ ├── RecyclerExt.kt │ │ │ │ ├── RecyclerListAdapter.kt │ │ │ │ ├── TextChangedListener.kt │ │ │ │ └── ViewExt.kt │ │ │ │ ├── main │ │ │ │ ├── CharacterItemAdapter.kt │ │ │ │ ├── MainActivity.kt │ │ │ │ ├── MainListAdapter.kt │ │ │ │ └── MainView.kt │ │ │ │ └── views │ │ │ │ └── SquareFrameLayout.kt │ │ └── res │ │ │ ├── layout │ │ │ ├── activity_character_profile.xml │ │ │ ├── activity_main.xml │ │ │ └── item_character.xml │ │ │ ├── mipmap-hdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-mdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xhdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxhdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxxhdpi │ │ │ └── ic_launcher.png │ │ │ └── values │ │ │ ├── colors.xml │ │ │ ├── dimen.xml │ │ │ ├── strings.xml │ │ │ └── styles.xml │ │ ├── sharedTest │ │ └── java │ │ │ └── com │ │ │ └── sample │ │ │ └── marvelgallery │ │ │ └── helpers │ │ │ ├── BaseMainView.kt │ │ │ ├── BaseMarvelRepository.kt │ │ │ ├── Example.kt │ │ │ └── TestHelpers.kt │ │ └── test │ │ └── java │ │ └── com │ │ └── sample │ │ └── marvelgallery │ │ ├── AuthParamTest.kt │ │ ├── MainPresenterSearchTest.kt │ │ └── MainPresenterTest.kt │ ├── build.gradle │ ├── circle.yml │ ├── files │ └── flow.gif │ ├── gradle.properties │ ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ └── settings.gradle ├── LICENSE └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /Chapter01/01_infix.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val capitols = listOf("England" to "London", "Poland" to "Warsaw") 3 | for ((country, city) in capitols) { 4 | println("Capitol of $country is $city") 5 | } 6 | } -------------------------------------------------------------------------------- /Chapter01/01_smart_casts.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | var x = String; 3 | if (x is String) { 4 | print(x.length) // x is automatically casted to String 5 | } 6 | 7 | x.length //error, x is not casted to a String outside if block 8 | 9 | if (x !is String) 10 | return 11 | 12 | x.length // x is automatically casted to String 13 | } -------------------------------------------------------------------------------- /Chapter01/01_stdlib.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val text = capitols.map { (country, _) -> country.toUpperCase() } 3 | .onEach { println(it) } 4 | .filter { it.startsWith("P") } 5 | .joinToString (prefix = "Countries starting from P are: ") 6 | // Prints: ENGLAND POLAND 7 | println(text) // Prints: Countries starting from P are POLAND 8 | } -------------------------------------------------------------------------------- /Chapter01/01_strings.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val ball = Ball(12, "Red") 3 | println(ball) // prints: Ball(size=12, color=Red) 4 | val smallBall = ball.copy(size = 3) 5 | println(smallBall) // prints: Ball(size=3, color=Red) 6 | smallBall.size++ 7 | println(smallBall) // prints: Ball(size=4, color=Red) 8 | println(ball) // prints: Ball(size=12, color=Red) 9 | } -------------------------------------------------------------------------------- /Chapter01/01_uppercase.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val name = "Igor" 3 | println("My name is ${name.toUpperCase()}") 4 | // Prints: My name is IGOR 5 | } -------------------------------------------------------------------------------- /Chapter02/02_break_continue.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val range = 1..6 3 | 4 | for(i in range) { 5 | print("$i ") 6 | } 7 | } -------------------------------------------------------------------------------- /Chapter02/02_break_continue_loop1.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val intRange = 1..6 3 | val charRange = 'A'..'B' 4 | 5 | for(value in intRange) { 6 | if(value == 3) 7 | break 8 | 9 | println("Outer loop: $value ") 10 | 11 | for (char in charRange) { 12 | println("\tInner loop: $char ") 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /Chapter02/02_break_continue_loop2.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val intRange = 1..5 3 | 4 | for(value in intRange) { 5 | if(value == 3) 6 | continue 7 | 8 | println("Outer loop: $value ") 9 | 10 | for (char in charRange) { 11 | println("\tInner loop: $char ") 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter02/02_break_continue_loop3.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | fun doSth() { 3 | val charRange = 'A'..'B' 4 | val intRange = 1..6 5 | 6 | for(value in intRange) { 7 | println("Outer loop: $value ") 8 | 9 | for (char in charRange) { 10 | println("\tInner loop: $char ") 11 | 12 | return 13 | } 14 | } 15 | } 16 | 17 | //usage 18 | println("Before method call") 19 | doSth() 20 | println("After method call") 21 | } -------------------------------------------------------------------------------- /Chapter02/02_range.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val range = 1..6 3 | 4 | for(i in range) { 5 | print("$i ") 6 | 7 | if (i == 3) 8 | break 9 | } 10 | } -------------------------------------------------------------------------------- /Chapter02/02_variable_declaration.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | var title1: String = "Kotlin" 3 | var title2: Any = "Kotlin" 4 | title2 = 12 5 | var pair = "Everest" to 8848 // Create pair using to infix method 6 | var pair2 = Pair("Everest", 8848) // Create Pair using constructor 7 | var map = mapOf("Mount Everest" to 8848, "K2" to "4017") 8 | var age_i: Int = 18 9 | var age_s: Short = 18 10 | var age_l1: Long = 18 // Explicitly define variable type 11 | var age_l2 = 18L // Use literal constant to specify value type 12 | } -------------------------------------------------------------------------------- /Chapter02/02_when.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val vehicle = "Bike" 3 | 4 | val message= when (vehicle) { 5 | "Car" -> { 6 | // Some code 7 | "Four wheels" 8 | } 9 | "Bike" -> { 10 | // Some code 11 | "Two wheels" 12 | } 13 | else -> { 14 | //some code 15 | "Unknown number of wheels" 16 | } 17 | } 18 | 19 | println(message) //Prints: Two wheels 20 | } -------------------------------------------------------------------------------- /Chapter02/02_when_02.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val riskAssessment = 47 3 | 4 | val risk = when (riskAssessment) { 5 | in 1..20 -> "negligible risk" 6 | !in 21..40 -> "minor risk" 7 | !in 41..60 -> "major risk" 8 | else -> "undefined risk" 9 | } 10 | 11 | println(risk) 12 | } -------------------------------------------------------------------------------- /Chapter02/02_when_03.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val large:Boolean = true 3 | 4 | when(large){ 5 | true -> println("Big") 6 | false -> println("Big") 7 | } 8 | } -------------------------------------------------------------------------------- /Chapter03/03_Unit_type_function.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val p = printSum(1, 2) 3 | println(p is Unit) 4 | } 5 | fun printSum(a: Int, b: Int): Unit{ 6 | val sum = a + b 7 | } -------------------------------------------------------------------------------- /Chapter03/03_default_args_function.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | printValue("str", true, "","") 3 | printValue("str") 4 | printValue("str", false) 5 | } 6 | 7 | fun printValue(value: String, inBracket: Boolean = true, prefix: String = "", suffix: String = "") { 8 | print(prefix) 9 | if (inBracket) { 10 | print("(${value})") 11 | } else { 12 | print(value) 13 | } 14 | println(suffix) 15 | } -------------------------------------------------------------------------------- /Chapter03/03_function_declaration.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | presentGently(42) 3 | presentGently("Pavan") 4 | } 5 | fun presentGently(v: Any) { 6 | println("Hello. I would like to present you: $v") 7 | } -------------------------------------------------------------------------------- /Chapter03/03_local_function.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | printTwoThreeTimes() 3 | } 4 | fun printTwoThreeTimes() { 5 | fun printThree() { // 1 6 | print(3) 7 | } 8 | printThree() // 2 9 | printThree() // 2 10 | } -------------------------------------------------------------------------------- /Chapter03/03_retrun_type.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val p = sumPositive(2,3) 3 | print(p) 4 | } 5 | fun sumPositive(a: Int, b: Int): Int { 6 | if(a >= 0 && b >= 0) { 7 | return a + b 8 | } 9 | return 0 10 | } -------------------------------------------------------------------------------- /Chapter03/03_returning_function.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | printSum(2,3) 3 | } 4 | fun printSum(a: Int, b: Int): Unit { 5 | val sum = a + b 6 | print(sum) 7 | } -------------------------------------------------------------------------------- /Chapter03/03_vararg.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | printSum(1,2,3,4,5) 3 | printSum() 4 | } 5 | 6 | fun printSum(vararg numbers: Int) { 7 | val sum = numbers.sum() 8 | print(sum) 9 | } -------------------------------------------------------------------------------- /Chapter03/03_vararg_02.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | printAll("A", "B", "C") 3 | } 4 | 5 | fun printAll(vararg texts: String) { // 1 6 | val allTexts = texts.joinToString(",") 7 | println("Texts are $allTexts") 8 | } -------------------------------------------------------------------------------- /Chapter03/03_vararg_03.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val texts = arrayOf("B", "C", "D") 3 | printAll(*texts) 4 | printAll("A", *texts, "E") 5 | } 6 | fun printAll(vararg texts: Any) { 7 | val allTexts = texts.joinToString(",") // 1 8 | println(allTexts) 9 | } -------------------------------------------------------------------------------- /Chapter04/04_constructor.kt: -------------------------------------------------------------------------------- 1 | class Person { 2 | var name: String 3 | var age: Int 4 | 5 | constructor(name: String, age: Int) { 6 | this.name = name 7 | this.age = age 8 | } 9 | } 10 | fun main(args: Array) { 11 | Person() 12 | } -------------------------------------------------------------------------------- /Chapter04/04_copy_method.kt: -------------------------------------------------------------------------------- 1 | data class Product(var name: String, var price: Double) 2 | 3 | fun main(args: Array) { 4 | val productA = Product("Spoon", 30.2) 5 | print(productA) 6 | val productB = productA.copy() 7 | print(productB) 8 | } -------------------------------------------------------------------------------- /Chapter04/04_custom_getters_setters.kt: -------------------------------------------------------------------------------- 1 | class Fruit(var weight: Double) { 2 | var ecoRating: Int = 3 3 | get() { 4 | println("getter value retrieved") 5 | return field 6 | } 7 | set(value) { 8 | field = if (value < 0) 0 else value 9 | println("setter new value assigned $field") 10 | } 11 | } 12 | 13 | fun main(args: Array) { 14 | val fruit = Fruit(12.0) 15 | val ecoRating = fruit.ecoRating 16 | fruit.ecoRating = 3; 17 | fruit.ecoRating = -5; 18 | } -------------------------------------------------------------------------------- /Chapter04/04_destructive_declaration.kt: -------------------------------------------------------------------------------- 1 | data class Person(val firstName: String, val lastName: String, val height: Int) 2 | 3 | fun main(args: Array) { 4 | val person = Person("Igor", "Wojda", 180) 5 | var (firstName, lastName, height) = person 6 | println(firstName) 7 | println(height) 8 | } -------------------------------------------------------------------------------- /Chapter04/04_equals_hashcode.kt: -------------------------------------------------------------------------------- 1 | data class Product(var name:String, var price:Double) 2 | 3 | fun main(args: Array) { 4 | val productA = Product("Spoon", 30.2) 5 | val productB = Product("Spoon", 30.2) 6 | val productC = Product("Fork", 17.4) 7 | print(productA == productA) 8 | print(productA == productB) 9 | print(productB == productA) 10 | print(productA == productC) 11 | print(productB == productC) 12 | } -------------------------------------------------------------------------------- /Chapter04/04_inheritance: -------------------------------------------------------------------------------- 1 | open class Plant 2 | class Tree : Plant() 3 | 4 | fun main(args: Array) { 5 | Tree() 6 | } -------------------------------------------------------------------------------- /Chapter04/04_interface.kt: -------------------------------------------------------------------------------- 1 | interface EmailProvider { 2 | 3 | val email: String 4 | 5 | val nickname: String 6 | get() = email.substringBefore("@") 7 | 8 | fun validateEmail() = nickname.isNotEmpty() 9 | } 10 | 11 | class User(override val email: String) : EmailProvider 12 | 13 | fun main(args: Array) { 14 | val user = User("joey@test.com") 15 | print(user.validateEmail()) 16 | print(user.nickname) 17 | } -------------------------------------------------------------------------------- /Chapter04/04_multiple_interface.kt: -------------------------------------------------------------------------------- 1 | interface A { 2 | fun foo() { 3 | println("A") 4 | } 5 | } 6 | 7 | interface B { 8 | fun foo() { 9 | println("B") 10 | } 11 | } 12 | class Item : A, B { 13 | override fun foo() { 14 | println("Item") 15 | } 16 | } 17 | 18 | fun main(args: Array) { 19 | val item = Item() 20 | item.foo() 21 | } -------------------------------------------------------------------------------- /Chapter04/04_operator_overloading.kt: -------------------------------------------------------------------------------- 1 | data class Point(var x: Double, var y: Double) { 2 | operator fun plus(point: Point) = Point(x + point.x, y+ point.y) 3 | operator fun times(other: Int) = Point(x * other, y * other) 4 | } 5 | 6 | fun main(args: Array) { 7 | var p1 = Point(2.9, 5.0) 8 | var p2 = Point(2.0, 7.5) 9 | println(p1 + p2) 10 | println(p1 * 3) 11 | } -------------------------------------------------------------------------------- /Chapter04/04_property_default.kt: -------------------------------------------------------------------------------- 1 | class Fruit(var weight: Double) { 2 | val isHeavy = weight > 20 3 | } 4 | 5 | fun main(args: Array) { 6 | var fruit = Fruit(7.0) 7 | println(fruit.isHeavy) 8 | fruit.weight = 30.5 9 | println(fruit.isHeavy) 10 | } -------------------------------------------------------------------------------- /Chapter05/05_companion_object.kt: -------------------------------------------------------------------------------- 1 | class A { 2 | companion object {} 3 | } 4 | fun A.Companion.foo() { 5 | print(2) 6 | } 7 | 8 | fun main(args: Array) { 9 | A.foo() 10 | } 11 | -------------------------------------------------------------------------------- /Chapter05/05_extension_function.kt: -------------------------------------------------------------------------------- 1 | abstract class A 2 | class B: A() 3 | 4 | fun A.foo() { println("foo(A)") } 5 | fun B.foo() { println("foo(B)") } 6 | 7 | fun main(args: Array) { 8 | val b = B() 9 | b.foo() 10 | (b as A).foo() 11 | val a: A = b 12 | a.foo() 13 | } -------------------------------------------------------------------------------- /Chapter05/05_member_function.kt: -------------------------------------------------------------------------------- 1 | class A { 2 | fun foo() { 3 | println("foo from A") 4 | } 5 | } 6 | fun A.foo() { 7 | println("foo from Extension") 8 | } 9 | fun main(args: Array) { 10 | A().foo() 11 | } -------------------------------------------------------------------------------- /Chapter05/05_member_function_superclass.kt: -------------------------------------------------------------------------------- 1 | open class A { 2 | fun foo() { 3 | println("foo from A") 4 | } 5 | } 6 | 7 | class B: A() 8 | 9 | fun B.foo() { 10 | println("foo from Extension") 11 | } 12 | 13 | fun main(args: Array) { 14 | A().foo() 15 | } -------------------------------------------------------------------------------- /Chapter06/06_covariance.kt: -------------------------------------------------------------------------------- 1 | fun printElements(list: List) { 2 | for(e in list) print(e) 3 | } 4 | 5 | fun main(args: Array) { 6 | val intList = listOf(1, 2, 3, 4) 7 | val anyList = listOf(1, 'A') 8 | printElements(intList) 9 | printElements(anyList) 10 | } -------------------------------------------------------------------------------- /Chapter06/06_filter_function.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val fruits = listOf("Babana", "Orange", "Apple", "Blueberry") 3 | val bFruits = fruits.filter { it.startsWith("B") } 4 | println(bFruits) 5 | } -------------------------------------------------------------------------------- /Chapter06/06_non-nullable_type_arg.kt: -------------------------------------------------------------------------------- 1 | open class Action 2 | class ActionGroup(private val list: List) { 3 | fun lastOrNull(): T? = list.lastOrNull() 4 | } 5 | 6 | fun main(args: Array) { 7 | val actionGroup = ActionGroup(listOf(Action(), null)) 8 | println(actionGroup.lastOrNull()) 9 | } -------------------------------------------------------------------------------- /Chapter06/06_reified_type_param.kt: -------------------------------------------------------------------------------- 1 | interface View 2 | class ProfileView: View 3 | class HomeView: View 4 | inline fun typeCheck(s: Any) { 5 | if(s is T){ 6 | println("The same types") 7 | } else { 8 | println("Different types") 9 | } 10 | } 11 | 12 | fun main(args: Array) { 13 | typeCheck(ProfileView()) 14 | typeCheck(ProfileView()) 15 | typeCheck(ProfileView()) 16 | } -------------------------------------------------------------------------------- /Chapter06/06_star_projections.kt: -------------------------------------------------------------------------------- 1 | fun printSize(list: MutableList<*>) { 2 | println(list.size) 3 | } 4 | 5 | fun main(args: Array) { 6 | val stringList = mutableListOf("5", "a", "2", "d") 7 | val intList = mutableListOf(3, 7) 8 | printSize(stringList) 9 | printSize(intList) 10 | } -------------------------------------------------------------------------------- /Chapter06/06_type_erasure.kt: -------------------------------------------------------------------------------- 1 | package test 2 | class Box 3 | 4 | val intBox = Box() 5 | val stringBox = Box() 6 | 7 | fun main(args: Array) { 8 | println(intBox.javaClass) 9 | println(stringBox.javaClass) 10 | } -------------------------------------------------------------------------------- /Chapter06/06_type_mismatch_dec_example.kt: -------------------------------------------------------------------------------- 1 | fun addElement(mutableList: MutableList) { 2 | mutableList.add("Cat") 3 | } 4 | 5 | fun main(args: Array) { 6 | val mutableIntList = mutableListOf(1, 2, 3, 4) 7 | val mutableAnyList = mutableListOf(1, 'A') 8 | addElement(mutableIntList) 9 | addElement(mutableIntList) 10 | } -------------------------------------------------------------------------------- /Chapter06/06_variance.kt: -------------------------------------------------------------------------------- 1 | open class Animal(val name: String) 2 | class Dog(name: String): Animal(name) 3 | 4 | fun present(animal: Animal) { 5 | println( "This is ${ animal. name } " ) 6 | } 7 | 8 | fun main(args: Array) { 9 | present(Dog( "Pluto" )) 10 | } -------------------------------------------------------------------------------- /Chapter07/07_companion_object.kt: -------------------------------------------------------------------------------- 1 | class A { 2 | companion object {} 3 | } 4 | fun A.Companion.foo() { print(2) } 5 | fun main(args: Array) { 6 | A.foo() 7 | } -------------------------------------------------------------------------------- /Chapter07/07_data_class: -------------------------------------------------------------------------------- 1 | data class Student( 2 | val name: String, 3 | val grade: Double, 4 | val passing: Boolean 5 | ) 6 | 7 | val students = listOf( 8 | Student("John", 4.2, true), 9 | Student("Bill", 3.5, true), 10 | Student("John", 3.2, false), 11 | Student("Aron", 4.3, true), 12 | Student("Jimmy", 3.1, true) 13 | ) 14 | 15 | val bestStudents = students.filter { it.passing } 16 | .withIndex() 17 | .sortedBy { it.value.grade } 18 | .take(3) 19 | .sortedBy { it.index } 20 | .map { it.value } 21 | 22 | fun main(args: Array) { 23 | println(bestStudents.map { it.name }) 24 | } -------------------------------------------------------------------------------- /Chapter07/07_extensions.kt: -------------------------------------------------------------------------------- 1 | abstract class A 2 | class B: A() 3 | 4 | fun A.foo() { println("foo(A)") } 5 | fun B.foo() { println("foo(B)") } 6 | 7 | fun main(args: Array) { 8 | val b = B() 9 | b.foo() // prints: foo(B) 10 | (b as A).foo() // 1, prints: foo(A) 11 | val a: A = b 12 | a.foo() // 1, prints: foo(A) 13 | } -------------------------------------------------------------------------------- /Chapter07/07_functions.kt: -------------------------------------------------------------------------------- 1 | class Student(val name: String, val classCode: String, val meanGrade: Float) 2 | 3 | val students = listOf( 4 | Student("Homer", "1", 1.1F), 5 | Student("Carl", "2", 1.5F), 6 | Student("Donald", "2", 3.5F), 7 | Student("Alex", "3", 4.5F), 8 | Student("Marcin", "3", 5.0F), 9 | Student("Max", "1", 3.2F) 10 | ) 11 | 12 | val bestInClass = students 13 | .groupBy { it.classCode } 14 | .map { (_, students) -> students.maxBy { it.meanGrade }!! } 15 | .map { it.name } 16 | fun main(args: Array) { 17 | print(bestInClass) 18 | } -------------------------------------------------------------------------------- /Chapter07/07_let_function.kt: -------------------------------------------------------------------------------- 1 | val text = "hello {name}" 2 | 3 | fun correctStyle(text: String) = text 4 | .replace("hello", "hello,") 5 | 6 | fun greet(name: String) { 7 | text.replace("{name}", name) 8 | .let { correctStyle(it) } 9 | .capitalize() 10 | .let { print(it) } 11 | } 12 | 13 | 14 | fun main(args: Array) { 15 | greet("reader") 16 | } -------------------------------------------------------------------------------- /Chapter07/07_no_method_overriding.kt: -------------------------------------------------------------------------------- 1 | class A { 2 | fun foo() { 3 | println("foo from A") 4 | } 5 | } 6 | 7 | fun A.foo() { 8 | println("foo from Extension") 9 | } 10 | 11 | fun main(args: Array) { 12 | A().foo() 13 | } -------------------------------------------------------------------------------- /Chapter07/07_standard_library_functions.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val mutableList = mutableListOf(1) 3 | val mutableList = mutableListOf(1) 4 | val letResult = mutableList.let { 5 | it.add(2) 6 | listOf("A", "B", "C") 7 | } 8 | println(letResult) // Prints: [A, B, C] 9 | val applyResult = mutableList.apply { 10 | add(3) 11 | listOf("A", "B", "C") 12 | } 13 | println(applyResult) // Prints: [1, 2, 3] 14 | val alsoResult = mutableList.also { 15 | it.add(4) 16 | listOf("A", "B", "C") 17 | } 18 | println(alsoResult) // Prints: [1, 2, 3, 4] 19 | val runResult = mutableList.run { 20 | add(5) 21 | listOf("A", "B", "C") 22 | } 23 | println(runResult) // Prints: [A, B, C] 24 | val withResult = with(mutableList) { 25 | add(6) 26 | listOf("A", "B", "C") 27 | } 28 | println(withResult) // Prints: [A, B, C] 29 | println(mutableList) // Prints: [1, 2, 3, 4, 5, 6] 30 | } -------------------------------------------------------------------------------- /Chapter07/07_superclass.kt: -------------------------------------------------------------------------------- 1 | open class A { 2 | fun foo() { 3 | println("foo from A") 4 | } 5 | } 6 | 7 | class B: A() 8 | 9 | fun B.foo() { 10 | println("foo from Extension") 11 | } 12 | 13 | fun main(args: Array) { 14 | A().foo() 15 | } -------------------------------------------------------------------------------- /Chapter08/08_delegation_pattern.kt: -------------------------------------------------------------------------------- 1 | interface Player { 2 | fun playGame() 3 | } 4 | 5 | class RpgGamePlayer(val enemy: String) : Player { 6 | override fun playGame() { 7 | println("Killing $enemy") 8 | } 9 | } 10 | 11 | class WitcherPlayer(enemy: String) : Player { 12 | val player = RpgGamePlayer(enemy) 13 | 14 | override fun playGame() { 15 | player.playGame() 16 | } 17 | } 18 | 19 | fun main(args: Array) { 20 | RpgGamePlayer("monsters").playGame() 21 | WitcherPlayer("monsters").playGame() 22 | } -------------------------------------------------------------------------------- /Chapter08/08_delgation_pattern_02.kt: -------------------------------------------------------------------------------- 1 | interface Player { 2 | fun playGame() 3 | } 4 | 5 | interface GameMaker { 6 | fun developGame() 7 | } 8 | 9 | class WitcherPlayer(val enemy: String) : Player { 10 | override fun playGame() { 11 | print("Killin $enemy! ") 12 | } 13 | } 14 | 15 | class WitcherCreator(val gameName: String) : GameMaker{ 16 | override fun developGame() { 17 | println("Makin $gameName! ") 18 | } 19 | } 20 | 21 | class WitcherPassionate : 22 | Player by WitcherPlayer("monsters"), 23 | GameMaker by WitcherCreator("Witcher 3") { 24 | 25 | fun fulfillYourDestiny() { 26 | playGame() 27 | developGame() 28 | } 29 | } 30 | fun main(args: Array) { 31 | WitcherPassionate().fulfillYourDestiny() 32 | } 33 | -------------------------------------------------------------------------------- /Chapter08/08_lazy_function_Java.java: -------------------------------------------------------------------------------- 1 | public class MainActivity extends Activity { 2 | 3 | TextView questionLabelView 4 | EditText answerLabelView 5 | Button confirmButtonView 6 | 7 | @Override 8 | public void onCreate(Bundle savedInstanceState) { 9 | super.onCreate(savedInstanceState); 10 | setContentView(R.layout.activity_main); 11 | 12 | questionLabelView = findViewById 13 | (R.id.main_question_label); 14 | answerLabelView = findViewById 15 | (R.id.main_answer_label); 16 | confirmButtonView = findViewById