├── Chapter09 ├── .gradle │ ├── buildOutputCleanup │ │ ├── built.bin │ │ ├── cache.properties.lock │ │ └── cache.properties │ └── 4.0 │ │ ├── fileChanges │ │ └── last-build.bin │ │ └── fileHashes │ │ ├── fileHashes.bin │ │ └── fileHashes.lock ├── build │ └── kotlin-build │ │ └── version.txt ├── settings.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── out │ └── production │ │ └── classes │ │ ├── META-INF │ │ └── Chapter09_main.kotlin_module │ │ └── com │ │ └── rivuchk │ │ └── functionalkotlin │ │ └── chapter09 │ │ ├── Chapter9_2Kt.class │ │ ├── Chapter9_3Kt.class │ │ ├── Chapter9_4Kt.class │ │ ├── Chapter9_5Kt.class │ │ ├── Chapter9_6Kt.class │ │ ├── Chapter9_7Kt.class │ │ ├── Chapter9_8Kt.class │ │ ├── Chapter9_1_pullKt.class │ │ ├── Chapter9_2Kt$main$1.class │ │ ├── Chapter9_2Kt$main$2.class │ │ ├── Chapter9_2Kt$main$3.class │ │ ├── Chapter9_7Kt$main$1.class │ │ ├── Chapter9_7Kt$main$2.class │ │ ├── Chapter9_7Kt$main$3.class │ │ ├── Chapter9_8Kt$main$1.class │ │ ├── Chapter9_5Kt$main$future$1.class │ │ ├── Chapter9_3Kt$main$observer$1.class │ │ ├── Chapter9_4Kt$main$observer$1.class │ │ ├── Chapter9_5Kt$main$callable$1.class │ │ ├── Chapter9_5Kt$main$observer$1.class │ │ ├── Chapter9_6Kt$main$observer$1.class │ │ ├── Chapter9_7Kt$main$observer$1.class │ │ ├── Chapter9_8Kt$main$observer$1.class │ │ ├── Chapter9_4Kt$main$observable$1.class │ │ └── Chapter9_4Kt$main$observable2$1.class ├── src │ └── main │ │ └── kotlin │ │ └── com │ │ └── rivuchk │ │ └── functionalkotlin │ │ └── chapter09 │ │ ├── chapter9_1_pull.kt │ │ ├── chapter9_2.kt │ │ ├── chapter9_6.kt │ │ ├── chapter9_7.kt │ │ ├── chapter9_3.kt │ │ ├── chapter9_8.kt │ │ ├── chapter9_4.kt │ │ └── chapter9_5.kt ├── Chapter09.iml ├── build.gradle └── gradlew.bat ├── Chapter11 ├── settings.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── src │ └── main │ │ └── kotlin │ │ └── com │ │ └── rivuchk │ │ └── packtpub │ │ └── functionalkotlin │ │ └── chapter11 │ │ ├── chapter11_3_primitive.kt │ │ ├── chapter11_6_empty.kt │ │ ├── chapter11_7_of.kt │ │ ├── Chapter11_1_intro.kt │ │ ├── chapter11_12_groupby.kt │ │ ├── chapter11_2_working.kt │ │ ├── chapter11_9_tomap.kt │ │ ├── chapter11_4_doublestream.kt │ │ ├── chapter11_10_tocollection.kt │ │ ├── chapter11_8_generate.kt │ │ ├── chapter11_11_joining.kt │ │ └── chapter11_5_builder.kt ├── build.gradle └── gradlew.bat ├── Chapter06 ├── settings.gradle ├── src │ └── main │ │ └── kotlin │ │ └── com │ │ └── rivuchk │ │ └── packtpub │ │ └── functionalkotlin │ │ └── chapter06 │ │ ├── chapter6_3_lazy.kt │ │ ├── chapter6_2_lateinit.kt │ │ ├── chapter6_1_notnull.kt │ │ ├── chapter6_9_local.kt │ │ ├── chapter6_5_vetoable.kt │ │ ├── chapter6_4_observable.kt │ │ ├── chapter6_6_vetoable.kt │ │ ├── chapter6_10_class.kt │ │ ├── chapter6_7_map.kt │ │ └── chapter6_8_custom.kt ├── build.gradle └── gradlew.bat ├── Chapter08 ├── settings.gradle ├── src │ └── main │ │ └── kotlin │ │ └── com │ │ └── rivuchk │ │ └── packtpub │ │ └── functionalkotlin │ │ └── chapter08 │ │ ├── chapter8_14_group.kt │ │ ├── chapter8_1_listof.kt │ │ ├── chapter8_8_map.kt │ │ ├── chapter8_11_drop.kt │ │ ├── chapter8_4_set.kt │ │ ├── chapter8_10_flatmap.kt │ │ ├── chapter8_2_empty_list.kt │ │ ├── chapter8_13_zip.kt │ │ ├── chapter8_7_list_access.kt │ │ ├── chapter8_3_mutablelist.kt │ │ ├── chapter8_12_take.kt │ │ ├── chapter8_13_zip2.kt │ │ ├── chapter8_9_filter.kt │ │ ├── chapter8_6_maps.kt │ │ └── chapter8_5_set_customdata.kt ├── local.properties ├── build.gradle └── gradlew.bat ├── Chapter01 ├── settings.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── src │ └── main │ │ └── kotlin │ │ └── com │ │ └── packtpub │ │ └── functionalkotlin │ │ └── chapter01 │ │ ├── alias.kt │ │ ├── generics.kt │ │ ├── nullable.kt │ │ ├── inheritance.kt │ │ ├── classes.kt │ │ ├── types.kt │ │ ├── absclasses.kt │ │ └── objects.kt ├── build.gradle └── gradlew.bat ├── Chapter02 ├── settings.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── src │ └── main │ │ └── kotlin │ │ └── com │ │ └── packtpub │ │ └── functionalkotlin │ │ └── chapter02 │ │ ├── lazy.kt │ │ ├── basics.kt │ │ ├── collections.kt │ │ ├── recursion.kt │ │ ├── structures.kt │ │ ├── high.kt │ │ └── list.kt ├── build.gradle └── gradlew.bat ├── Chapter05 ├── settings.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── src │ └── main │ │ └── kotlin │ │ └── com │ │ └── packtpub │ │ └── functionalkotlin │ │ └── chapter05 │ │ ├── single.kt │ │ ├── recursion.kt │ │ ├── tornado.kt │ │ ├── infix.kt │ │ ├── corecursion.kt │ │ ├── operators.kt │ │ ├── inline.kt │ │ └── parameters.kt ├── build.gradle └── gradlew.bat ├── Chapter07 ├── settings.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── src │ └── main │ │ └── kotlin │ │ └── com │ │ └── packtpub │ │ └── functionalkotlin │ │ └── chapter07 │ │ ├── composing.kt │ │ ├── contexts.kt │ │ ├── cancel.kt │ │ ├── select.kt │ │ └── basics.kt ├── build.gradle └── gradlew.bat ├── Chapter10 ├── settings.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── build.gradle └── gradlew.bat ├── Chapter12 ├── settings.gradle ├── gradle │ ├── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties │ └── generated-kotlin-sources.gradle ├── src │ └── main │ │ └── kotlin │ │ └── com │ │ └── packtpub │ │ └── functionalkotlin │ │ └── chapter12 │ │ ├── complement.kt │ │ ├── reverse.kt │ │ ├── identity.kt │ │ ├── pipes.kt │ │ ├── currying.kt │ │ ├── partials.kt │ │ ├── partial.kt │ │ ├── composition.kt │ │ ├── optics.kt │ │ └── memoization.kt ├── build.gradle └── gradlew.bat ├── Chapter13 ├── settings.gradle ├── gradle │ ├── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties │ └── generated-kotlin-sources.gradle ├── build.gradle ├── src │ └── main │ │ └── kotlin │ │ └── com │ │ └── packtpub │ │ └── functionalkotlin │ │ └── chapter13 │ │ ├── types.kt │ │ ├── either.kt │ │ ├── state.kt │ │ └── tryy.kt └── gradlew.bat ├── Chapter03 ├── settings.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── src │ └── main │ │ └── kotlin │ │ └── com │ │ └── rivuchk │ │ └── functionalkotlin │ │ └── chapter3 │ │ ├── chapter3_02_PrimitiveExample.kt │ │ ├── chapter3_01_MutableObject.kt │ │ ├── chapter3_03_val.kt │ │ ├── chapter3_05_collections.kt │ │ ├── chapter3_04_mutable_val.kt │ │ ├── chapter3_06_threading.kt │ │ └── chapter3_07_threading_immutable.kt ├── .idea │ ├── misc.xml │ └── modules.xml ├── build.gradle └── gradlew.bat ├── Chapter14 ├── console │ ├── META-INF │ │ └── main.kotlin_module │ ├── hello.kt │ └── HelloKt.class └── gradle │ ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties │ ├── src │ └── main │ │ └── kotlin │ │ └── com │ │ └── packtpub │ │ └── appendix │ │ └── hello.kt │ ├── build.gradle │ ├── gradle.iml │ └── gradlew.bat ├── Appendix └── Code │ ├── console │ ├── META-INF │ │ └── main.kotlin_module │ ├── hello.kt │ └── HelloKt.class │ └── gradle │ ├── src │ └── main │ │ └── kotlin │ │ └── com │ │ └── packtpub │ │ └── appendix │ │ └── hello.kt │ ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties │ ├── build.gradle │ ├── gradle.iml │ └── gradlew.bat ├── Chapter04 ├── .idea │ ├── kotlinc.xml │ ├── misc.xml │ ├── modules.xml │ └── libraries │ │ └── KotlinJavaRuntime.xml ├── out │ └── production │ │ └── FunctionalKotlinProject │ │ ├── META-INF │ │ └── FunctionalKotlinProject.kotlin_module │ │ └── com │ │ └── rivuchk │ │ └── functionalkotlin │ │ └── chapter4 │ │ ├── Chapter4_8_ho1Kt.class │ │ ├── Chapter4_9_ho2Kt.class │ │ ├── LambdaIntroClass.class │ │ ├── Chapter4_1_pairKt.class │ │ ├── Chapter4_4_nestedKt.class │ │ ├── Chapter4_5_lambda1Kt.class │ │ ├── Chapter4_6_lambda2Kt.class │ │ ├── Chapter4_7_lambda3Kt.class │ │ ├── Chapter4_2_count_wordsKt.class │ │ ├── Chapter4_8_ho1Kt$main$1.class │ │ ├── Chapter4_8_ho1Kt$main$2.class │ │ ├── Chapter4_4_nestedKt$main$1.class │ │ ├── Chapter4_5_lambda1Kt$main$1.class │ │ ├── Chapter4_7_lambda3Kt$main$1.class │ │ ├── Chapter4_3_default_argumentsKt.class │ │ ├── Chapter4_6_lambda2Kt$main$sum$1.class │ │ ├── LambdaIntroClass$SomeInterface.class │ │ └── Chapter4_9_ho2Kt$getAnotherFunction$1.class ├── src │ └── com │ │ └── rivuchk │ │ └── functionalkotlin │ │ └── chapter4 │ │ ├── chapter4_11_withoutsideeffects.kt │ │ ├── chapter4_4_nested.kt │ │ ├── chapter4_5_lambda1.kt │ │ ├── chapter4_6_lambda2.kt │ │ ├── chapter4_1_pair.kt │ │ ├── chapter4_3_default_arguments.kt │ │ ├── chapter4_9_ho2.kt │ │ ├── chapter4_10_sideeffects.kt │ │ ├── chapter4_2_count_words.kt │ │ ├── chapter4_8_ho1.kt │ │ ├── LambdaIntroClass.java │ │ └── chapter4_7_lambda3.kt └── FunctionalKotlinProject.iml ├── LICENSE └── README.md /Chapter09/.gradle/buildOutputCleanup/built.bin: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Chapter09/build/kotlin-build/version.txt: -------------------------------------------------------------------------------- 1 | 11001 -------------------------------------------------------------------------------- /Chapter09/.gradle/4.0/fileChanges/last-build.bin: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Chapter09/.gradle/buildOutputCleanup/cache.properties.lock: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /Chapter11/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'Streams' 2 | 3 | -------------------------------------------------------------------------------- /Chapter06/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'Chapter06' 2 | 3 | -------------------------------------------------------------------------------- /Chapter08/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'Collections' 2 | 3 | -------------------------------------------------------------------------------- /Chapter09/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'Chapter09' 2 | 3 | -------------------------------------------------------------------------------- /Chapter01/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'functionalkotlin' 2 | 3 | -------------------------------------------------------------------------------- /Chapter02/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'functionalkotlin' 2 | 3 | -------------------------------------------------------------------------------- /Chapter05/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'functionalkotlin' 2 | 3 | -------------------------------------------------------------------------------- /Chapter07/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'functionalkotlin' 2 | 3 | -------------------------------------------------------------------------------- /Chapter10/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'functionalkotlin' 2 | 3 | -------------------------------------------------------------------------------- /Chapter12/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'functionalkotlin' 2 | 3 | -------------------------------------------------------------------------------- /Chapter13/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'functionalkotlin' 2 | 3 | -------------------------------------------------------------------------------- /Chapter03/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'Chapter03[FunctionalKotlin]' 2 | 3 | -------------------------------------------------------------------------------- /Chapter14/console/META-INF/main.kotlin_module: -------------------------------------------------------------------------------- 1 |  2 | 3 | HelloKt -------------------------------------------------------------------------------- /Appendix/Code/console/META-INF/main.kotlin_module: -------------------------------------------------------------------------------- 1 |  2 | 3 | HelloKt -------------------------------------------------------------------------------- /Chapter14/console/hello.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | println("Hello, World!") 3 | } -------------------------------------------------------------------------------- /Appendix/Code/console/hello.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | println("Hello, World!") 3 | } -------------------------------------------------------------------------------- /Chapter09/.gradle/buildOutputCleanup/cache.properties: -------------------------------------------------------------------------------- 1 | #Wed Feb 21 14:55:28 IST 2018 2 | gradle.version=4.0 3 | -------------------------------------------------------------------------------- /Chapter14/console/HelloKt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter14/console/HelloKt.class -------------------------------------------------------------------------------- /Appendix/Code/console/HelloKt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Appendix/Code/console/HelloKt.class -------------------------------------------------------------------------------- /Chapter01/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter01/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /Chapter02/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter02/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /Chapter03/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter03/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /Chapter05/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter05/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /Chapter07/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter07/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /Chapter09/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /Chapter10/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter10/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /Chapter11/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter11/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /Chapter12/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter12/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /Chapter13/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter13/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /Chapter09/.gradle/4.0/fileHashes/fileHashes.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/.gradle/4.0/fileHashes/fileHashes.bin -------------------------------------------------------------------------------- /Chapter09/.gradle/4.0/fileHashes/fileHashes.lock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/.gradle/4.0/fileHashes/fileHashes.lock -------------------------------------------------------------------------------- /Appendix/Code/gradle/src/main/kotlin/com/packtpub/appendix/hello.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.appendix 2 | 3 | fun main(args: Array) { 4 | println("Hello, World!") 5 | } -------------------------------------------------------------------------------- /Chapter14/gradle/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter14/gradle/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /Chapter14/gradle/src/main/kotlin/com/packtpub/appendix/hello.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.appendix 2 | 3 | fun main(args: Array) { 4 | println("Hello, World!") 5 | } -------------------------------------------------------------------------------- /Appendix/Code/gradle/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Appendix/Code/gradle/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /Chapter09/out/production/classes/META-INF/Chapter09_main.kotlin_module: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/out/production/classes/META-INF/Chapter09_main.kotlin_module -------------------------------------------------------------------------------- /Chapter04/.idea/kotlinc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | -------------------------------------------------------------------------------- /Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_2Kt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_2Kt.class -------------------------------------------------------------------------------- /Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_3Kt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_3Kt.class -------------------------------------------------------------------------------- /Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_4Kt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_4Kt.class -------------------------------------------------------------------------------- /Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_5Kt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_5Kt.class -------------------------------------------------------------------------------- /Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_6Kt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_6Kt.class -------------------------------------------------------------------------------- /Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_7Kt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_7Kt.class -------------------------------------------------------------------------------- /Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_8Kt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_8Kt.class -------------------------------------------------------------------------------- /Chapter04/out/production/FunctionalKotlinProject/META-INF/FunctionalKotlinProject.kotlin_module: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter04/out/production/FunctionalKotlinProject/META-INF/FunctionalKotlinProject.kotlin_module -------------------------------------------------------------------------------- /Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_1_pullKt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_1_pullKt.class -------------------------------------------------------------------------------- /Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_2Kt$main$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_2Kt$main$1.class -------------------------------------------------------------------------------- /Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_2Kt$main$2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_2Kt$main$2.class -------------------------------------------------------------------------------- /Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_2Kt$main$3.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_2Kt$main$3.class -------------------------------------------------------------------------------- /Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_7Kt$main$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_7Kt$main$1.class -------------------------------------------------------------------------------- /Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_7Kt$main$2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_7Kt$main$2.class -------------------------------------------------------------------------------- /Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_7Kt$main$3.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_7Kt$main$3.class -------------------------------------------------------------------------------- /Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_8Kt$main$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_8Kt$main$1.class -------------------------------------------------------------------------------- /Chapter04/src/com/rivuchk/functionalkotlin/chapter4/chapter4_11_withoutsideeffects.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter4 2 | 3 | fun addNumbers(a:Int = 0,b:Int = 0):Int { 4 | return a+b 5 | } 6 | 7 | fun main(args: Array) { 8 | println() 9 | } -------------------------------------------------------------------------------- /Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_5Kt$main$future$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_5Kt$main$future$1.class -------------------------------------------------------------------------------- /Appendix/Code/gradle/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | zipStoreBase=GRADLE_USER_HOME 4 | zipStorePath=wrapper/dists 5 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.5.1-bin.zip 6 | -------------------------------------------------------------------------------- /Chapter03/src/main/kotlin/com/rivuchk/functionalkotlin/chapter3/chapter3_02_PrimitiveExample.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter3 2 | 3 | fun main(args: Array) { 4 | var x:String = "abc" 5 | var y = x.capitalize() 6 | println("x = $x, y = $y") 7 | } -------------------------------------------------------------------------------- /Chapter08/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter08/chapter8_14_group.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter08 2 | 3 | fun main(args: Array) { 4 | val list = 1.rangeTo(50).toList() 5 | 6 | println(list.groupBy { it%5 }) 7 | } -------------------------------------------------------------------------------- /Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_3Kt$main$observer$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_3Kt$main$observer$1.class -------------------------------------------------------------------------------- /Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_4Kt$main$observer$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_4Kt$main$observer$1.class -------------------------------------------------------------------------------- /Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_5Kt$main$callable$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_5Kt$main$callable$1.class -------------------------------------------------------------------------------- /Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_5Kt$main$observer$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_5Kt$main$observer$1.class -------------------------------------------------------------------------------- /Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_6Kt$main$observer$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_6Kt$main$observer$1.class -------------------------------------------------------------------------------- /Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_7Kt$main$observer$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_7Kt$main$observer$1.class -------------------------------------------------------------------------------- /Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_8Kt$main$observer$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_8Kt$main$observer$1.class -------------------------------------------------------------------------------- /Chapter14/gradle/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | zipStoreBase=GRADLE_USER_HOME 4 | zipStorePath=wrapper/dists 5 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.5.1-bin.zip 6 | -------------------------------------------------------------------------------- /Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_8_ho1Kt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_8_ho1Kt.class -------------------------------------------------------------------------------- /Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_9_ho2Kt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_9_ho2Kt.class -------------------------------------------------------------------------------- /Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/LambdaIntroClass.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/LambdaIntroClass.class -------------------------------------------------------------------------------- /Chapter05/src/main/kotlin/com/packtpub/functionalkotlin/chapter05/single.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter05 2 | 3 | 4 | /* 5 | fun sum(a:Int, b:Int): Int { 6 | return a + b 7 | }*/ 8 | 9 | //fun sum(a:Int, b:Int): Int = a + b 10 | fun sum(a:Int, b:Int) = a + b -------------------------------------------------------------------------------- /Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_4Kt$main$observable$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_4Kt$main$observable$1.class -------------------------------------------------------------------------------- /Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_4Kt$main$observable2$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter09/out/production/classes/com/rivuchk/functionalkotlin/chapter09/Chapter9_4Kt$main$observable2$1.class -------------------------------------------------------------------------------- /Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_1_pairKt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_1_pairKt.class -------------------------------------------------------------------------------- /Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_4_nestedKt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_4_nestedKt.class -------------------------------------------------------------------------------- /Chapter01/src/main/kotlin/com/packtpub/functionalkotlin/chapter01/alias.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter01 2 | 3 | typealias Oven = Machine 4 | 5 | typealias Flavour = String 6 | 7 | typealias OvenTray = List 8 | 9 | typealias CupcakeFactory = Cupcake.Factory -------------------------------------------------------------------------------- /Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_5_lambda1Kt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_5_lambda1Kt.class -------------------------------------------------------------------------------- /Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_6_lambda2Kt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_6_lambda2Kt.class -------------------------------------------------------------------------------- /Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_7_lambda3Kt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_7_lambda3Kt.class -------------------------------------------------------------------------------- /Chapter03/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_2_count_wordsKt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_2_count_wordsKt.class -------------------------------------------------------------------------------- /Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_8_ho1Kt$main$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_8_ho1Kt$main$1.class -------------------------------------------------------------------------------- /Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_8_ho1Kt$main$2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_8_ho1Kt$main$2.class -------------------------------------------------------------------------------- /Chapter04/src/com/rivuchk/functionalkotlin/chapter4/chapter4_4_nested.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter4 2 | 3 | fun main(args: Array) { 4 | fun nested():String { 5 | return "String from nested function" 6 | } 7 | println("Nested Output: ${nested()}") 8 | } -------------------------------------------------------------------------------- /Chapter03/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Feb 20 08:34:59 IST 2018 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-bin.zip 7 | -------------------------------------------------------------------------------- /Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_4_nestedKt$main$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_4_nestedKt$main$1.class -------------------------------------------------------------------------------- /Chapter09/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Feb 19 07:29:58 IST 2018 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-all.zip 7 | -------------------------------------------------------------------------------- /Chapter11/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Jan 29 05:34:10 IST 2018 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-bin.zip 7 | -------------------------------------------------------------------------------- /Chapter01/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Oct 03 03:40:15 BST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-rc-2-all.zip 7 | -------------------------------------------------------------------------------- /Chapter02/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Oct 03 03:40:15 BST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-rc-2-all.zip 7 | -------------------------------------------------------------------------------- /Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_5_lambda1Kt$main$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_5_lambda1Kt$main$1.class -------------------------------------------------------------------------------- /Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_7_lambda3Kt$main$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_7_lambda3Kt$main$1.class -------------------------------------------------------------------------------- /Chapter05/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Oct 03 03:40:15 BST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-rc-2-all.zip 7 | -------------------------------------------------------------------------------- /Chapter07/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Oct 03 03:40:15 BST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-rc-2-all.zip 7 | -------------------------------------------------------------------------------- /Chapter10/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Oct 03 03:40:15 BST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-rc-2-all.zip 7 | -------------------------------------------------------------------------------- /Chapter12/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Oct 03 03:40:15 BST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-rc-2-all.zip 7 | -------------------------------------------------------------------------------- /Chapter13/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Oct 03 03:40:15 BST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-rc-2-all.zip 7 | -------------------------------------------------------------------------------- /Chapter04/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_3_default_argumentsKt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_3_default_argumentsKt.class -------------------------------------------------------------------------------- /Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_6_lambda2Kt$main$sum$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_6_lambda2Kt$main$sum$1.class -------------------------------------------------------------------------------- /Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/LambdaIntroClass$SomeInterface.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/LambdaIntroClass$SomeInterface.class -------------------------------------------------------------------------------- /Chapter03/src/main/kotlin/com/rivuchk/functionalkotlin/chapter3/chapter3_01_MutableObject.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter3 2 | 3 | class MutableObject { 4 | var mutableProperty:Int = 1 5 | } 6 | 7 | fun main(args: Array) { 8 | val mutableObj = MutableObject() 9 | 10 | 11 | } -------------------------------------------------------------------------------- /Chapter08/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter08/chapter8_1_listof.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter08 2 | 3 | fun main(args: Array) { 4 | val list = listOf(1,2,3,4,5,6,7,8,9,10) 5 | 6 | for (i in list) { 7 | println("Item $i") 8 | } 9 | } -------------------------------------------------------------------------------- /Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_9_ho2Kt$getAnotherFunction$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Functional-Kotlin/HEAD/Chapter04/out/production/FunctionalKotlinProject/com/rivuchk/functionalkotlin/chapter4/Chapter4_9_ho2Kt$getAnotherFunction$1.class -------------------------------------------------------------------------------- /Chapter04/src/com/rivuchk/functionalkotlin/chapter4/chapter4_5_lambda1.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter4 2 | 3 | fun invokeSomeStuff(lambda:()->Unit) { 4 | lambda() 5 | } 6 | 7 | fun main(args: Array) { 8 | invokeSomeStuff({ 9 | println("doSomeStuff called"); 10 | }) 11 | } 12 | -------------------------------------------------------------------------------- /Chapter04/src/com/rivuchk/functionalkotlin/chapter4/chapter4_6_lambda2.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter4 2 | 3 | import java.util.* 4 | 5 | fun main(args: Array) { 6 | val sum = { x: Int, y: Int -> x + y } // (1) 7 | println("Sum ${sum(10,13)}")// (2) 8 | println("Sum ${sum(52,68)}")// (3) 9 | } -------------------------------------------------------------------------------- /Chapter01/src/main/kotlin/com/packtpub/functionalkotlin/chapter01/generics.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter01 2 | 3 | /*interface Oven { 4 | fun process(product: Bakeable) 5 | }*/ 6 | 7 | interface Machine { 8 | fun process(product: T) 9 | } 10 | 11 | //interface Oven: Machine 12 | 13 | -------------------------------------------------------------------------------- /Chapter04/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter08/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter08/chapter8_8_map.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter08 2 | 3 | fun main(args: Array) { 4 | val list = listOf(1,2,3,4,5,6,7,8,9,10) 5 | val modifiedList = list.map { it*2 } 6 | 7 | println("modifiedList -> $modifiedList") 8 | } -------------------------------------------------------------------------------- /Chapter04/src/com/rivuchk/functionalkotlin/chapter4/chapter4_1_pair.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter4 2 | 3 | fun getUser():Pair { 4 | return Pair(1,"Rivu") 5 | } 6 | 7 | fun main(args: Array) { 8 | val (userID,userName) = getUser() 9 | 10 | println("User ID: $userID \t User Name: $userName") 11 | } -------------------------------------------------------------------------------- /Chapter08/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter08/chapter8_11_drop.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter08 2 | 3 | fun main(args: Array) { 4 | val list = 1.until(50).toList() 5 | 6 | println("list.drop(25) -> ${list.drop(25)}") 7 | println("list.dropLast(25) -> ${list.dropLast(25)}") 8 | } -------------------------------------------------------------------------------- /Chapter06/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter06/chapter6_3_lazy.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter06 2 | 3 | val myLazyVal:String by lazy { 4 | println("Just Initialised") 5 | "My Lazy Val" 6 | } 7 | 8 | fun main(args: Array) { 9 | println("Not yet initialised") 10 | println(myLazyVal) 11 | } -------------------------------------------------------------------------------- /Chapter11/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter11/chapter11_3_primitive.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter11 2 | 3 | import java.util.stream.IntStream 4 | 5 | fun main(args: Array) { 6 | val intStream = IntStream.range(1,10) 7 | val result = intStream.sum() 8 | println("The sum of elements is $result") 9 | } -------------------------------------------------------------------------------- /Chapter11/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter11/chapter11_6_empty.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter11 2 | 3 | import java.util.stream.Stream 4 | 5 | fun main(args: Array) { 6 | val emptyStream = Stream.empty() 7 | 8 | val item = emptyStream.findAny() 9 | println("Item is $item") 10 | } -------------------------------------------------------------------------------- /Chapter06/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter06/chapter6_2_lateinit.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter06 2 | 3 | 4 | lateinit var notNullStr1:String 5 | lateinit var notInit1:String 6 | 7 | fun main(args: Array) { 8 | notNullStr1 = "Initial value" 9 | println(notNullStr1) 10 | println(notInit1) 11 | } -------------------------------------------------------------------------------- /Chapter04/src/com/rivuchk/functionalkotlin/chapter4/chapter4_3_default_arguments.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter4 2 | 3 | fun Int.isGreaterThan(anotherNumber:Int=0):Boolean { 4 | return this>anotherNumber 5 | } 6 | 7 | 8 | fun main(args: Array) { 9 | println("5>0: ${5.isGreaterThan()}") 10 | println("5>6: ${5.isGreaterThan(6)}") 11 | } -------------------------------------------------------------------------------- /Chapter08/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter08/chapter8_4_set.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter08 2 | 3 | fun main(args: Array) { 4 | val set = mutableSetOf(1,2,3,3,2) 5 | 6 | println("set $set") 7 | 8 | set.add(4) 9 | set.add(5) 10 | set.add(5) 11 | set.add(6) 12 | 13 | println("set $set") 14 | } -------------------------------------------------------------------------------- /Chapter08/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter08/chapter8_10_flatmap.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter08 2 | 3 | fun main(args: Array) { 4 | val list = listOf(10,20,30) 5 | 6 | val flatMappedList = list.flatMap { 7 | it.rangeTo(it+2).toList() 8 | } 9 | 10 | println("flatMappedList -> $flatMappedList") 11 | } -------------------------------------------------------------------------------- /Chapter04/src/com/rivuchk/functionalkotlin/chapter4/chapter4_9_ho2.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter4 2 | 3 | fun getAnotherFunction(n:Int):(String)->Unit { 4 | return { 5 | println("n:$n it:$it") 6 | } 7 | } 8 | 9 | fun main(args: Array) { 10 | getAnotherFunction(0)("abc") 11 | getAnotherFunction(2)("def") 12 | getAnotherFunction(3)("ghi") 13 | } -------------------------------------------------------------------------------- /Chapter08/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter08/chapter8_2_empty_list.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter08 2 | 3 | fun main(args: Array) { 4 | val emptyList1 = listOf() 5 | 6 | val emptyList2 = emptyList() 7 | 8 | println("emptyList1.size = ${emptyList1.size}") 9 | println("emptyList2.size = ${emptyList2.size}") 10 | } -------------------------------------------------------------------------------- /Chapter09/src/main/kotlin/com/rivuchk/functionalkotlin/chapter09/chapter9_1_pull.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter09 2 | 3 | fun main(args: Array) { 4 | var list:List = listOf(1, "Two", 3, "Four", "Five", 5.5f) // 1 5 | var iterator = list.iterator() // 2 6 | while (iterator.hasNext()) { // 3 7 | println(iterator.next()) // Prints each element 4 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Chapter11/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter11/chapter11_7_of.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter11 2 | 3 | import java.util.stream.Collectors 4 | import java.util.stream.Stream 5 | 6 | fun main(args: Array) { 7 | val stream = Stream.of("Item 1",2,"Item 3",4,5.0,"Item 6") 8 | 9 | println("Items in Stream = ${stream.collect(Collectors.toList())}") 10 | } -------------------------------------------------------------------------------- /Chapter05/src/main/kotlin/com/packtpub/functionalkotlin/chapter05/recursion.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter05 2 | 3 | sealed class IntList 4 | class IntCons(val head: Int, val tail: F) : IntList() 5 | object IntNil : IntList() 6 | 7 | fun IntList.map(f: (T) -> R) : IntList = when(this){ 8 | is IntCons -> IntCons(head, f(tail)) 9 | is IntNil -> IntNil as IntList 10 | } 11 | -------------------------------------------------------------------------------- /Chapter11/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter11/Chapter11_1_intro.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter11 2 | 3 | import java.util.stream.Collectors 4 | import kotlin.streams.* 5 | 6 | fun main(args: Array) { 7 | val stream = 1.rangeTo(10).asSequence().asStream() 8 | 9 | val resultantList = stream.skip(5).collect(Collectors.toList()) 10 | 11 | println(resultantList) 12 | } -------------------------------------------------------------------------------- /Chapter06/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter06/chapter6_1_notnull.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter06 2 | 3 | import kotlin.properties.Delegates 4 | 5 | var notNullStr:String by Delegates.notNull() 6 | var notInit:String by Delegates.notNull() 7 | 8 | fun main(args: Array) { 9 | notNullStr = "Initial value" 10 | println(notNullStr) 11 | println(notInit) 12 | } -------------------------------------------------------------------------------- /Chapter11/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter11/chapter11_12_groupby.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter11 2 | 3 | import java.util.stream.Collectors 4 | import kotlin.streams.asStream 5 | 6 | fun main(args: Array) { 7 | val resultantSet = (1..20).asSequence().asStream() 8 | .collect(Collectors.groupingBy { it%5 }) 9 | 10 | println("resultantSet $resultantSet") 11 | } -------------------------------------------------------------------------------- /Chapter04/src/com/rivuchk/functionalkotlin/chapter4/chapter4_10_sideeffects.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter4 2 | 3 | class Calc { 4 | var a:Int=0 5 | var b:Int=0 6 | 7 | fun addNumbers(a:Int = this.a,b:Int = this.b):Int { 8 | this.a = a 9 | this.b = b 10 | return a+b 11 | } 12 | } 13 | 14 | fun main(args: Array) { 15 | val calc = Calc() 16 | println("Result is ${calc.addNumbers(10,15)}") 17 | } -------------------------------------------------------------------------------- /Chapter04/src/com/rivuchk/functionalkotlin/chapter4/chapter4_2_count_words.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter4 2 | 3 | import java.util.regex.Pattern 4 | 5 | fun String.countWords():Int { 6 | return trim() 7 | .split(Pattern.compile("\\s+")) 8 | .size 9 | } 10 | 11 | fun main(args: Array) { 12 | val counts = "This is an example String\nWith multiple words".countWords() 13 | println("Count Words: $counts") 14 | } -------------------------------------------------------------------------------- /Chapter06/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter06/chapter6_9_local.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter06 2 | 3 | fun useDelegate(shouldPrint:Boolean) { 4 | val localDelegate by lazy { 5 | "Delegate Used" 6 | } 7 | if(shouldPrint) { 8 | println(localDelegate) 9 | } 10 | 11 | println("bye bye") 12 | } 13 | 14 | fun main(args: Array) { 15 | useDelegate(true) 16 | useDelegate(false) 17 | } -------------------------------------------------------------------------------- /Chapter08/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter08/chapter8_13_zip.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter08 2 | 3 | fun main(args: Array) { 4 | val list1 = listOf(1,2,3,4,5) 5 | val list2 = listOf( 6 | "Item 1", 7 | "Item 2", 8 | "Item 3", 9 | "Item 4", 10 | "Item 5" 11 | ) 12 | 13 | val resultantList = list1.zip(list2) 14 | 15 | println(resultantList) 16 | } -------------------------------------------------------------------------------- /Chapter08/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter08/chapter8_7_list_access.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter08 2 | 3 | fun main(args: Array) { 4 | val list = listOf( 5 | "1st Item", 6 | "2nd Item", 7 | "3rd Item", 8 | "4th Item", 9 | "5th Item" 10 | ) 11 | 12 | println("3rd Item on the list - ${list.get(2)}") 13 | println("4rd Item on the list - ${list[3]}") 14 | } -------------------------------------------------------------------------------- /Chapter11/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter11/chapter11_2_working.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter11 2 | 3 | import java.util.stream.Collectors 4 | import kotlin.streams.asStream 5 | 6 | fun main(args: Array) { 7 | val stream = 1.rangeTo(10).asSequence().asStream() 8 | 9 | val resultantList = stream.parallel().filter{ 10 | it%2==0 11 | }.collect(Collectors.toList()) 12 | 13 | println(resultantList) 14 | } -------------------------------------------------------------------------------- /Chapter06/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter06/chapter6_5_vetoable.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter06 2 | 3 | import kotlin.properties.Delegates 4 | 5 | var myIntEven:Int by Delegates.vetoable(0) { 6 | property, oldValue, newValue -> 7 | println("${property.name} $oldValue -> $newValue") 8 | newValue%2==0 9 | } 10 | 11 | fun main(args: Array) { 12 | myIntEven = 6 13 | myIntEven = 3 14 | println("myIntEven:$myIntEven") 15 | } -------------------------------------------------------------------------------- /Chapter08/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter08/chapter8_3_mutablelist.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter08 2 | 3 | fun main(args: Array) { 4 | val list = mutableListOf(1,2,4) 5 | 6 | for (i in list) { 7 | println("for1 item $i") 8 | } 9 | 10 | println("-----Adding Items-----") 11 | 12 | list.add(5) 13 | list.add(2,3) 14 | list.add(6) 15 | 16 | for (i in list) { 17 | println("for2 item $i") 18 | } 19 | } -------------------------------------------------------------------------------- /Chapter08/local.properties: -------------------------------------------------------------------------------- 1 | ## This file is automatically generated by Android Studio. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must *NOT* be checked into Version Control Systems, 5 | # as it contains information specific to your local configuration. 6 | # 7 | # Location of the SDK. This is only used by Gradle. 8 | # For customization when using a Version Control System, please read the 9 | # header note. 10 | #Tue Jan 23 00:46:33 IST 2018 11 | sdk.dir=C\:\\Android\\sdk 12 | -------------------------------------------------------------------------------- /Chapter12/gradle/generated-kotlin-sources.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'idea' 2 | 3 | idea { 4 | module { 5 | sourceDirs += files( 6 | 'build/generated/source/kapt/main', 7 | 'build/generated/source/kaptKotlin/main', 8 | 'build/tmp/kapt/main/kotlinGenerated') 9 | generatedSourceDirs += files( 10 | 'build/generated/source/kapt/main', 11 | 'build/generated/source/kaptKotlin/main', 12 | 'build/tmp/kapt/main/kotlinGenerated') 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter13/gradle/generated-kotlin-sources.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'idea' 2 | 3 | idea { 4 | module { 5 | sourceDirs += files( 6 | 'build/generated/source/kapt/main', 7 | 'build/generated/source/kaptKotlin/main', 8 | 'build/tmp/kapt/main/kotlinGenerated') 9 | generatedSourceDirs += files( 10 | 'build/generated/source/kapt/main', 11 | 'build/generated/source/kaptKotlin/main', 12 | 'build/tmp/kapt/main/kotlinGenerated') 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter06/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter06/chapter6_4_observable.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter06 2 | 3 | import kotlin.properties.Delegates 4 | 5 | var myStr:String by Delegates.observable("") { 6 | property, oldValue, newValue -> 7 | println("Property `${property.name}` changed value from \"$oldValue\" to \"$newValue\"") 8 | } 9 | 10 | fun main(args: Array) { 11 | myStr = "Change Value" 12 | myStr = "Change Value again" 13 | } -------------------------------------------------------------------------------- /Chapter11/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter11/chapter11_9_tomap.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter11 2 | 3 | import java.util.stream.Collectors 4 | import kotlin.streams.asStream 5 | 6 | fun main(args: Array) { 7 | val resultantMap = (0..10).asSequence().asStream() 8 | .collect(Collectors.toMap({ 9 | it 10 | },{ 11 | it*it 12 | })) 13 | println("resultantMap = $resultantMap") 14 | } -------------------------------------------------------------------------------- /Chapter08/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter08/chapter8_12_take.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter08 2 | 3 | fun main(args: Array) { 4 | val list = 1.until(50).toList() 5 | 6 | println("list.take(25) -> ${list.take(25)}")//(1) 7 | println("list.takeLast(25) -> ${list.takeLast(25)}")//(2) 8 | println("list.takeWhile { it<=10 } -> ${list.takeWhile { it<=10 }}")//(3) 9 | println("list.takeLastWhile { it>=40 } -> ${list.takeLastWhile { it>=40 }}")//(4) 10 | } -------------------------------------------------------------------------------- /Chapter11/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter11/chapter11_4_doublestream.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter11 2 | 3 | import java.util.stream.DoubleStream 4 | 5 | fun main(args: Array) { 6 | val doubleStream = DoubleStream.iterate(1.5,{item->item*1.3})//(1) 7 | 8 | val avg = doubleStream 9 | .limit(10)//(2) 10 | .peek { 11 | println("Item $it") 12 | }.average()//(3) 13 | 14 | println("Average of 10 Items $avg") 15 | } -------------------------------------------------------------------------------- /Chapter03/src/main/kotlin/com/rivuchk/functionalkotlin/chapter3/chapter3_03_val.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter3 2 | 3 | object MutableVal { 4 | var count = 0 5 | val myString:String = "Mutable" 6 | get() {//(1) 7 | return "$field ${++count}"//(2) 8 | } 9 | } 10 | 11 | fun main(args: Array) { 12 | println("Calling 1st time ${MutableVal.myString}") 13 | println("Calling 2nd time ${MutableVal.myString}") 14 | println("Calling 3rd time ${MutableVal.myString}")//(3) 15 | } -------------------------------------------------------------------------------- /Chapter08/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter08/chapter8_13_zip2.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter08 2 | 3 | fun main(args: Array) { 4 | val list1 = listOf(1,2,3,4,5,6,7,8) 5 | val list2 = listOf( 6 | "Item 1", 7 | "Item 2", 8 | "Item 3", 9 | "Item 4", 10 | "Item 5" 11 | ) 12 | 13 | println("list1.zip(list2)-> ${list1.zip(list2)}") 14 | 15 | println("list1.zipWithNext() -> ${list1.zipWithNext()}") 16 | } -------------------------------------------------------------------------------- /Chapter03/src/main/kotlin/com/rivuchk/functionalkotlin/chapter3/chapter3_05_collections.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter3 2 | 3 | fun main(args: Array) { 4 | val immutableList = listOf(1,2,3,4,5,6,7)//(1) 5 | println("Immutable List $immutableList") 6 | val mutableList:MutableList = immutableList.toMutableList()//(2) 7 | println("Mutable List $mutableList") 8 | mutableList.add(8)//(3) 9 | println("Mutable List after add $mutableList") 10 | println("Mutable List after add $immutableList") 11 | } -------------------------------------------------------------------------------- /Chapter04/src/com/rivuchk/functionalkotlin/chapter4/chapter4_8_ho1.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter4 2 | 3 | 4 | fun performOperationOnEven(number:Int,operation:(Int)->Int):Int { 5 | if(number%2==0) { 6 | return operation(number) 7 | } else { 8 | return number 9 | } 10 | } 11 | 12 | fun main(args: Array) { 13 | println("Called with 4,(it*2): ${performOperationOnEven(4, 14 | {it*2})}") 15 | println("Called with 5,(it*2): ${performOperationOnEven(5, 16 | {it*2})}") 17 | } -------------------------------------------------------------------------------- /Chapter04/src/com/rivuchk/functionalkotlin/chapter4/LambdaIntroClass.java: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter4; 2 | 3 | public class LambdaIntroClass { 4 | 5 | interface SomeInterface { 6 | void doSomeStuff(); 7 | } 8 | 9 | private static void invokeSomeStuff(SomeInterface someInterface) { 10 | someInterface.doSomeStuff(); 11 | } 12 | 13 | public static void main(String[] args) { 14 | invokeSomeStuff(()->{ 15 | System.out.println("doSomeStuff called"); 16 | }); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Chapter02/src/main/kotlin/com/packtpub/functionalkotlin/chapter02/lazy.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter02 2 | 3 | 4 | /* 5 | fun main(args: Array) { 6 | val i by lazy { 7 | println("Lazy evaluation") 8 | 1 9 | } 10 | 11 | println("before using i") 12 | println(i) 13 | }*/ 14 | 15 | 16 | /* 17 | fun main(args: Array) { 18 | val size = listOf(2 + 1, 3 * 2, 1 / 0, 5 - 4).size 19 | }*/ 20 | 21 | fun main(args: Array) { 22 | val size = listOf({ 2 + 1 }, { 3 * 2 }, { 1 / 0 }, { 5 - 4 }).size 23 | } -------------------------------------------------------------------------------- /Chapter04/FunctionalKotlinProject.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Chapter11/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter11/chapter11_10_tocollection.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter11 2 | 3 | import com.sun.deploy.util.OrderedHashSet 4 | import java.util.* 5 | import java.util.stream.Collectors 6 | import kotlin.collections.LinkedHashSet 7 | import kotlin.streams.asStream 8 | 9 | fun main(args: Array) { 10 | val resultantSet = (0..10).asSequence().asStream() 11 | .collect(Collectors.toCollection{LinkedHashSet()}) 12 | 13 | println("resultantSet $resultantSet") 14 | } -------------------------------------------------------------------------------- /Chapter04/src/com/rivuchk/functionalkotlin/chapter4/chapter4_7_lambda3.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter4 2 | 3 | fun main(args: Array) { 4 | val reverse:(Int)->Int//(1) 5 | 6 | reverse = { 7 | var n = it 8 | var revNumber = 0 9 | while (n>0) { 10 | val digit = n%10 11 | revNumber=revNumber*10+digit 12 | n/=10 13 | } 14 | revNumber 15 | }// (2) 16 | println("reverse 123 ${reverse(123)}") 17 | println("reverse 456 ${reverse(456)}") 18 | println("reverse 789 ${reverse(789)}") 19 | } -------------------------------------------------------------------------------- /Chapter05/src/main/kotlin/com/packtpub/functionalkotlin/chapter05/tornado.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter05 2 | 3 | import javafx.application.Application 4 | import tornadofx.* 5 | 6 | /** 7 | * Created by IntelliJ IDEA. 8 | * @author Mario Arias 9 | * Date: 11/12/17 10 | * Time: 1:18 AM 11 | */ 12 | 13 | fun main(args: Array) { 14 | Application.launch(FxApp::class.java, *args) 15 | } 16 | 17 | class FxApp: App(FxView::class) 18 | 19 | class FxView: View() { 20 | override val root = vbox { 21 | label("Functional Kotlin") 22 | button("Press me") 23 | } 24 | } -------------------------------------------------------------------------------- /Chapter12/src/main/kotlin/com/packtpub/functionalkotlin/chapter12/complement.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter12 2 | 3 | import arrow.core.Predicate 4 | import arrow.syntax.function.complement 5 | 6 | 7 | fun main(args: Array) { 8 | val evenPredicate: Predicate = { i: Int -> i % 2 == 0 } 9 | val oddPredicate: (Int) -> Boolean = evenPredicate.complement() 10 | 11 | val numbers: IntRange = 1..10 12 | val evenNumbers: List = numbers.filter(evenPredicate) 13 | val oddNumbers: List = numbers.filter(oddPredicate) 14 | 15 | println(evenNumbers) 16 | println(oddNumbers) 17 | } -------------------------------------------------------------------------------- /Chapter12/src/main/kotlin/com/packtpub/functionalkotlin/chapter12/reverse.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter12 2 | 3 | import arrow.syntax.function.partially3 4 | import arrow.syntax.function.reverse 5 | 6 | fun main(args: Array) { 7 | val strong: (String, String, String) -> String = { body, id, style -> "$body" } 8 | 9 | val redStrong: (String, String) -> String = strong.partially3("font: red") //Explicit 10 | 11 | println(redStrong("Red Sonja", "movie1")) 12 | 13 | println(redStrong.reverse()("movie2", "The Hunt for Red October")) 14 | 15 | } -------------------------------------------------------------------------------- /Chapter02/src/main/kotlin/com/packtpub/functionalkotlin/chapter02/basics.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter02 2 | 3 | 4 | fun f(x: Long) : Long { 5 | return x * x // no access to external state 6 | } 7 | 8 | /* 9 | fun main(args: Array) { 10 | println(f(5)) 11 | println(f(5)) 12 | println(f(5)) 13 | }*/ 14 | 15 | 16 | fun main(args: Array) { 17 | var i = 0 18 | 19 | fun g(x: Long): Long { 20 | return x * i // accessing mutable state 21 | } 22 | 23 | println(g(1)) //0 24 | i++ 25 | println(g(1)) //1 26 | i++ 27 | println(g(1)) //2 28 | } -------------------------------------------------------------------------------- /Chapter08/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter08/chapter8_9_filter.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter08 2 | 3 | import kotlin.math.roundToInt 4 | import kotlin.math.sqrt 5 | 6 | fun main(args: Array) { 7 | val list = 1.until(50).toList() 8 | val filteredListEven = list.filter { it%2==0 } 9 | 10 | println("filteredListEven -> $filteredListEven") 11 | 12 | val filteredListPSquare = list.filter { 13 | val sqroot = sqrt(it.toDouble()).roundToInt() 14 | sqroot*sqroot==it 15 | } 16 | 17 | println("filteredListPSquare -> $filteredListPSquare") 18 | } -------------------------------------------------------------------------------- /Chapter06/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter06/chapter6_6_vetoable.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter06 2 | 3 | import kotlin.properties.Delegates 4 | 5 | var myCounter:Int by Delegates.vetoable(0) { 6 | property, oldValue, newValue -> 7 | println("${property.name} $oldValue -> $newValue") 8 | newValue>oldValue 9 | } 10 | 11 | fun main(args: Array) { 12 | myCounter = 2 13 | println("myCounter:$myCounter") 14 | myCounter = 5 15 | myCounter = 4 16 | println("myCounter:$myCounter") 17 | myCounter++ 18 | myCounter-- 19 | println("myCounter:$myCounter") 20 | } -------------------------------------------------------------------------------- /Chapter09/src/main/kotlin/com/rivuchk/functionalkotlin/chapter09/chapter9_2.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter09 2 | 3 | import io.reactivex.Observable 4 | import io.reactivex.rxkotlin.subscribeBy 5 | import io.reactivex.rxkotlin.toObservable 6 | 7 | fun main(args: Array) { 8 | var list = listOf(1, "Two", 3, "Four", "Five", 5.5f) // 1 9 | var observable = list.toObservable(); 10 | 11 | observable.subscribeBy( // named arguments for lambda Subscribers 12 | onNext = { println(it) }, 13 | onError = { it.printStackTrace() }, 14 | onComplete = { println("Done!") } 15 | ) 16 | } 17 | -------------------------------------------------------------------------------- /Chapter03/src/main/kotlin/com/rivuchk/functionalkotlin/chapter3/chapter3_04_mutable_val.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter3 2 | 3 | class MutableObj { 4 | var value = "" 5 | 6 | override fun toString(): String { 7 | return "MutableObj(value='$value')" 8 | } 9 | } 10 | 11 | fun main(args: Array) { 12 | val mutableObj:MutableObj = MutableObj()//(1) 13 | println("MutableObj $mutableObj") 14 | mutableObj.value = "Changed"//(2) 15 | println("MutableObj $mutableObj") 16 | 17 | val list = mutableListOf("a","b","c","d","e")//(3) 18 | println(list) 19 | list.add("f")//(4) 20 | println(list) 21 | } -------------------------------------------------------------------------------- /Chapter11/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter11/chapter11_8_generate.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter11 2 | 3 | import java.util.* 4 | import java.util.stream.Collectors 5 | import java.util.stream.Stream 6 | 7 | fun ClosedRange.random() = 8 | Random().nextInt(endInclusive - start) + start 9 | 10 | fun main(args: Array) { 11 | val stream = Stream.generate { 12 | //return a random number 13 | (1..20).random() 14 | } 15 | 16 | val resultantList = stream 17 | .limit(10) 18 | .collect(Collectors.toList()) 19 | 20 | println("resultantList = $resultantList") 21 | } -------------------------------------------------------------------------------- /Chapter08/build.gradle: -------------------------------------------------------------------------------- 1 | group 'com.rivuchk.packtpub.functionalkotlin.chapter08' 2 | version '1.0-SNAPSHOT' 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.2.20' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | dependencies { 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | } 13 | } 14 | 15 | apply plugin: 'kotlin' 16 | 17 | repositories { 18 | mavenCentral() 19 | } 20 | 21 | dependencies { 22 | compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 23 | } 24 | 25 | compileKotlin { 26 | kotlinOptions.jvmTarget = "1.8" 27 | } 28 | compileTestKotlin { 29 | kotlinOptions.jvmTarget = "1.8" 30 | } -------------------------------------------------------------------------------- /Chapter06/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter06/chapter6_10_class.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter06 2 | 3 | interface Person { 4 | fun printName() 5 | } 6 | 7 | class PersonImpl(val name:String):Person { 8 | override fun printName() { 9 | println(name) 10 | } 11 | } 12 | 13 | class User(val person:Person):Person by person { 14 | override fun printName() { 15 | println("Printing Name:") 16 | person.printName() 17 | } 18 | } 19 | 20 | fun main(args: Array) { 21 | val person = PersonImpl("Mario Arias") 22 | person.printName() 23 | println() 24 | val user = User(person) 25 | user.printName() 26 | } -------------------------------------------------------------------------------- /Chapter11/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter11/chapter11_11_joining.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter11 2 | 3 | import java.util.stream.Collectors 4 | import java.util.stream.Stream 5 | import kotlin.streams.asStream 6 | 7 | fun main(args: Array) { 8 | val resultantString = Stream.builder() 9 | .add("Item 1") 10 | .add("Item 2") 11 | .add("Item 3") 12 | .add("Item 4") 13 | .add("Item 5") 14 | .add("Item 6") 15 | .build() 16 | .collect(Collectors.joining(" - ","Starts Here=>","<=Ends Here")) 17 | 18 | println("resultantString $resultantString") 19 | } -------------------------------------------------------------------------------- /Chapter11/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter11/chapter11_5_builder.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter11 2 | 3 | import java.util.stream.Collectors 4 | import java.util.stream.Stream 5 | 6 | fun main(args: Array) { 7 | val stream = Stream.builder() 8 | .add("Item 1") 9 | .add("Item 2") 10 | .add("Item 3") 11 | .add("Item 4") 12 | .add("Item 5") 13 | .add("Item 6") 14 | .add("Item 7") 15 | .add("Item 8") 16 | .add("Item 9") 17 | .add("Item 10") 18 | .build() 19 | 20 | println("The Stream is ${stream.collect(Collectors.toList())}") 21 | } -------------------------------------------------------------------------------- /Chapter05/src/main/kotlin/com/packtpub/functionalkotlin/chapter05/infix.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter05 2 | 3 | /** 4 | * Created by IntelliJ IDEA. 5 | * @author Mario Arias 6 | * Date: 1/12/17 7 | * Time: 3:19 AM 8 | */ 9 | 10 | infix fun Int.superOperation(i: Int) = this + i 11 | 12 | 13 | object All { 14 | infix fun your(base: Pair) {} 15 | } 16 | 17 | object Base { 18 | infix fun are(belong: Belong) = this 19 | } 20 | 21 | object Belong 22 | 23 | object Us 24 | 25 | infix fun String.`(╯°□°)╯︵ ┻━┻`(s: String) = "*$this flips table at $s*" 26 | 27 | fun main(args: Array) { 28 | 29 | /*1 superOperation 2 30 | 1.superOperation(2) 31 | 32 | "Adam" `(╯°□°)╯︵ ┻━┻` "Ben"*/ 33 | All your (Base are Belong to Us) 34 | } 35 | -------------------------------------------------------------------------------- /Chapter14/gradle/build.gradle: -------------------------------------------------------------------------------- 1 | group 'com.packtpub' 2 | version '1.0' 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.2.21' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | dependencies { 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | } 13 | } 14 | 15 | apply plugin: 'kotlin' 16 | apply plugin: 'application' 17 | 18 | mainClassName = 'com.packtpub.appendix.HelloKt' 19 | 20 | defaultTasks 'run' 21 | 22 | repositories { 23 | mavenCentral() 24 | } 25 | 26 | dependencies { 27 | compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 28 | } 29 | 30 | compileKotlin { 31 | kotlinOptions.jvmTarget = "1.8" 32 | } 33 | 34 | compileTestKotlin { 35 | kotlinOptions.jvmTarget = "1.8" 36 | } -------------------------------------------------------------------------------- /Appendix/Code/gradle/build.gradle: -------------------------------------------------------------------------------- 1 | group 'com.packtpub' 2 | version '1.0' 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.2.21' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | dependencies { 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | } 13 | } 14 | 15 | apply plugin: 'kotlin' 16 | apply plugin: 'application' 17 | 18 | mainClassName = 'com.packtpub.appendix.HelloKt' 19 | 20 | defaultTasks 'run' 21 | 22 | repositories { 23 | mavenCentral() 24 | } 25 | 26 | dependencies { 27 | compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 28 | } 29 | 30 | compileKotlin { 31 | kotlinOptions.jvmTarget = "1.8" 32 | } 33 | 34 | compileTestKotlin { 35 | kotlinOptions.jvmTarget = "1.8" 36 | } -------------------------------------------------------------------------------- /Chapter03/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Chapter06/build.gradle: -------------------------------------------------------------------------------- 1 | group 'com.rivuchk.packtpub.functionalkotlin.chapter06' 2 | version '1.0' 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.2.0' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | dependencies { 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | } 13 | } 14 | 15 | apply plugin: 'java' 16 | apply plugin: 'kotlin' 17 | 18 | sourceCompatibility = 1.8 19 | 20 | repositories { 21 | mavenCentral() 22 | } 23 | 24 | dependencies { 25 | compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 26 | testCompile group: 'junit', name: 'junit', version: '4.12' 27 | } 28 | 29 | compileKotlin { 30 | kotlinOptions.jvmTarget = "1.8" 31 | } 32 | compileTestKotlin { 33 | kotlinOptions.jvmTarget = "1.8" 34 | } -------------------------------------------------------------------------------- /Chapter11/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext.kotlin_version = '1.2.21' 3 | 4 | repositories { 5 | mavenCentral() 6 | } 7 | dependencies { 8 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 9 | } 10 | } 11 | 12 | group 'com.rivuchk.packtpub.functionalkotlin' 13 | version '1.0-SNAPSHOT' 14 | 15 | apply plugin: 'java' 16 | apply plugin: 'kotlin' 17 | 18 | sourceCompatibility = 1.8 19 | 20 | repositories { 21 | mavenCentral() 22 | } 23 | 24 | dependencies { 25 | compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 26 | testCompile group: 'junit', name: 'junit', version: '4.12' 27 | } 28 | 29 | compileKotlin { 30 | kotlinOptions.jvmTarget = "1.8" 31 | } 32 | compileTestKotlin { 33 | kotlinOptions.jvmTarget = "1.8" 34 | } -------------------------------------------------------------------------------- /Chapter14/gradle/gradle.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Appendix/Code/gradle/gradle.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Chapter03/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext.kotlin_version = '1.2.21' 3 | 4 | repositories { 5 | mavenCentral() 6 | } 7 | dependencies { 8 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 9 | } 10 | } 11 | 12 | group 'com.rivuchk.functionalkotlin.chapter03' 13 | version '1.0-SNAPSHOT' 14 | 15 | apply plugin: 'kotlin' 16 | 17 | kotlin { 18 | experimental { 19 | coroutines 'enable' 20 | } 21 | } 22 | 23 | repositories { 24 | mavenCentral() 25 | } 26 | 27 | dependencies { 28 | compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 29 | compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.21" 30 | } 31 | 32 | compileKotlin { 33 | kotlinOptions.jvmTarget = "1.8" 34 | } 35 | compileTestKotlin { 36 | kotlinOptions.jvmTarget = "1.8" 37 | } -------------------------------------------------------------------------------- /Chapter04/.idea/libraries/KotlinJavaRuntime.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Chapter01/build.gradle: -------------------------------------------------------------------------------- 1 | group 'com.packtpub' 2 | version '1.0-SNAPSHOT' 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.2.20' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | dependencies { 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | } 13 | } 14 | 15 | apply plugin: 'java' 16 | apply plugin: 'kotlin' 17 | 18 | 19 | sourceCompatibility = 1.8 20 | 21 | repositories { 22 | jcenter() 23 | mavenCentral() 24 | mavenLocal() 25 | } 26 | 27 | dependencies { 28 | compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 29 | compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" 30 | testCompile group: 'junit', name: 'junit', version: '4.12' 31 | } 32 | 33 | compileKotlin { 34 | kotlinOptions.jvmTarget = "1.8" 35 | } 36 | compileTestKotlin { 37 | kotlinOptions.jvmTarget = "1.8" 38 | } 39 | -------------------------------------------------------------------------------- /Chapter02/build.gradle: -------------------------------------------------------------------------------- 1 | group 'com.packtpub' 2 | version '1.0-SNAPSHOT' 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.2.20' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | dependencies { 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | } 13 | } 14 | 15 | apply plugin: 'java' 16 | apply plugin: 'kotlin' 17 | 18 | sourceCompatibility = 1.8 19 | 20 | repositories { 21 | jcenter() 22 | mavenCentral() 23 | mavenLocal() 24 | } 25 | 26 | dependencies { 27 | compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 28 | compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" 29 | testCompile group: 'junit', name: 'junit', version: '4.12' 30 | } 31 | 32 | compileKotlin { 33 | kotlinOptions.jvmTarget = "1.8" 34 | } 35 | compileTestKotlin { 36 | kotlinOptions.jvmTarget = "1.8" 37 | } 38 | kotlin { 39 | experimental { 40 | coroutines "enable" 41 | } 42 | } -------------------------------------------------------------------------------- /Chapter03/src/main/kotlin/com/rivuchk/functionalkotlin/chapter3/chapter3_06_threading.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter3 2 | 3 | 4 | import kotlinx.coroutines.experimental.CommonPool 5 | import kotlinx.coroutines.experimental.async 6 | import kotlinx.coroutines.experimental.delay 7 | import kotlinx.coroutines.experimental.runBlocking 8 | 9 | class MyData { 10 | var someData:Int = 0 11 | } 12 | 13 | fun main(args: Array) { 14 | val myData:MyData = MyData() 15 | 16 | async(CommonPool) { 17 | for(i in 11..20) { 18 | myData.someData+=i 19 | println("someData from 1st async ${myData.someData}") 20 | delay(500) 21 | } 22 | } 23 | 24 | async(CommonPool) { 25 | for(i in 1..10) { 26 | myData.someData++ 27 | println("someData from 2nd async ${myData.someData}") 28 | delay(300) 29 | } 30 | } 31 | 32 | runBlocking { delay(10000) } 33 | } -------------------------------------------------------------------------------- /Chapter10/build.gradle: -------------------------------------------------------------------------------- 1 | group 'com.packtpub' 2 | version '1.0-SNAPSHOT' 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.2.20' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | dependencies { 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | } 13 | } 14 | 15 | apply plugin: 'java' 16 | apply plugin: 'kotlin' 17 | 18 | 19 | sourceCompatibility = 1.8 20 | 21 | repositories { 22 | jcenter() 23 | mavenCentral() 24 | mavenLocal() 25 | } 26 | 27 | dependencies { 28 | compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 29 | compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" 30 | testCompile group: 'junit', name: 'junit', version: '4.12' 31 | } 32 | 33 | compileKotlin { 34 | kotlinOptions.jvmTarget = "1.8" 35 | } 36 | compileTestKotlin { 37 | kotlinOptions.jvmTarget = "1.8" 38 | } 39 | kotlin { 40 | experimental { 41 | coroutines "enable" 42 | } 43 | } -------------------------------------------------------------------------------- /Chapter12/src/main/kotlin/com/packtpub/functionalkotlin/chapter12/identity.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter12 2 | 3 | import arrow.core.PartialFunction 4 | import arrow.core.constant 5 | import arrow.core.orElse 6 | 7 | /* 8 | fun main(args: Array) { 9 | 10 | val oneToFour = 1..4 11 | 12 | println("With identity: ${oneToFour.map(::identity).joinToString()}") //1, 2, 3, 4 13 | 14 | println("With constant: ${oneToFour.map(constant(1)).joinToString()}") //1, 1, 1, 1 15 | 16 | }*/ 17 | 18 | fun main(args: Array) { 19 | val fizz = PartialFunction({ n: Int -> n % 3 == 0 }, constant("FIZZ")) 20 | val buzz = PartialFunction({ n: Int -> n % 5 == 0 }, constant("BUZZ")) 21 | val fizzBuzz = PartialFunction({ n: Int -> fizz.isDefinedAt(n) && buzz.isDefinedAt(n) }, constant("FIZZBUZZ")) 22 | val pass = PartialFunction(constant(true)) { n -> n.toString() } 23 | 24 | (1..50).map(fizzBuzz orElse buzz orElse fizz orElse pass).forEach(::println) 25 | } -------------------------------------------------------------------------------- /Chapter09/Chapter09.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Chapter12/src/main/kotlin/com/packtpub/functionalkotlin/chapter12/pipes.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter12 2 | 3 | import arrow.syntax.function.pipe 4 | import arrow.syntax.function.pipe3 5 | import arrow.syntax.function.reverse 6 | 7 | /*fun main(args: Array) { 8 | val strong: (String) -> String = { body -> "$body" } 9 | 10 | "From a pipe".pipe(strong).pipe(::println) 11 | }*/ 12 | 13 | 14 | 15 | /*fun main(args: Array) { 16 | splitter(filterBills(calculatePrice(Quote(20.0, "Foo", "Shoes", 20)))) 17 | 18 | Quote(20.0, "Foo", "Shoes", 20) pipe ::calculatePrice pipe ::filterBills pipe ::splitter 19 | }*/ 20 | 21 | fun main(args: Array) { 22 | val strong: (String, String, String) -> String = { body, id, style -> "$body" } 23 | 24 | val redStrong: (String, String) -> String = "color: red" pipe3 strong.reverse() 25 | 26 | redStrong("movie3", "Three colors: Red") pipe ::println 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Chapter05/build.gradle: -------------------------------------------------------------------------------- 1 | group 'com.packtpub' 2 | version '1.0-SNAPSHOT' 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.2.20' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | dependencies { 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | } 13 | } 14 | 15 | apply plugin: 'java' 16 | apply plugin: 'kotlin' 17 | 18 | sourceCompatibility = 1.8 19 | 20 | repositories { 21 | jcenter() 22 | mavenCentral() 23 | mavenLocal() 24 | } 25 | 26 | dependencies { 27 | compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 28 | compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" 29 | compile 'no.tornado:tornadofx:1.7.13' 30 | testCompile group: 'junit', name: 'junit', version: '4.12' 31 | } 32 | 33 | compileKotlin { 34 | kotlinOptions.jvmTarget = "1.8" 35 | } 36 | compileTestKotlin { 37 | kotlinOptions.jvmTarget = "1.8" 38 | } 39 | kotlin { 40 | experimental { 41 | coroutines "enable" 42 | } 43 | } -------------------------------------------------------------------------------- /Chapter09/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext.kotlin_version = '1.2.21' 3 | 4 | repositories { 5 | mavenCentral() 6 | } 7 | dependencies { 8 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 9 | } 10 | } 11 | 12 | group 'com.rivuchk.functionalkotlin.chapter09' 13 | version '1.0-SNAPSHOT' 14 | 15 | apply plugin: 'java' 16 | apply plugin: 'kotlin' 17 | 18 | kotlin { 19 | experimental { 20 | coroutines 'enable' 21 | } 22 | } 23 | 24 | sourceCompatibility = 1.8 25 | 26 | repositories { 27 | mavenCentral() 28 | jcenter() 29 | } 30 | 31 | dependencies { 32 | compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 33 | compile 'io.reactivex.rxjava2:rxkotlin:2.2.0' 34 | compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.21" 35 | testCompile group: 'junit', name: 'junit', version: '4.12' 36 | } 37 | 38 | compileKotlin { 39 | kotlinOptions.jvmTarget = "1.8" 40 | } 41 | compileTestKotlin { 42 | kotlinOptions.jvmTarget = "1.8" 43 | } -------------------------------------------------------------------------------- /Chapter09/src/main/kotlin/com/rivuchk/functionalkotlin/chapter09/chapter9_6.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter09 2 | 3 | import io.reactivex.Observable 4 | import io.reactivex.Observer 5 | import io.reactivex.disposables.Disposable 6 | import io.reactivex.rxkotlin.toObservable 7 | 8 | fun main(args: Array) { 9 | val observer: Observer = object : Observer { 10 | override fun onComplete() { 11 | println("Completed") 12 | } 13 | 14 | override fun onNext(item: String) { 15 | println("Received-> $item") 16 | } 17 | 18 | override fun onError(e: Throwable) { 19 | println("Error Occured => ${e.message}") 20 | } 21 | 22 | override fun onSubscribe(d: Disposable) { 23 | println("Subscription") 24 | } 25 | }//Create Observer 26 | val list:List = listOf("Str 1","Str 2","Str 3","Str 4") 27 | 28 | val observable: Observable = list.toObservable() 29 | 30 | observable.subscribe(observer) 31 | 32 | } -------------------------------------------------------------------------------- /Chapter03/src/main/kotlin/com/rivuchk/functionalkotlin/chapter3/chapter3_07_threading_immutable.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter3 2 | 3 | import kotlinx.coroutines.experimental.CommonPool 4 | import kotlinx.coroutines.experimental.async 5 | import kotlinx.coroutines.experimental.delay 6 | import kotlinx.coroutines.experimental.runBlocking 7 | 8 | class MyDataImmutable { 9 | val someData:Int = 0 10 | } 11 | 12 | fun main(args: Array) { 13 | val myData: MyDataImmutable = MyDataImmutable() 14 | 15 | async(CommonPool) { 16 | var someDataCopy = myData.someData 17 | for (i in 11..20) { 18 | someDataCopy += i 19 | println("someData from 1st async $someDataCopy") 20 | delay(500) 21 | } 22 | } 23 | 24 | async(CommonPool) { 25 | var someDataCopy = myData.someData 26 | for (i in 1..10) { 27 | someDataCopy++ 28 | println("someData from 2nd async $someDataCopy") 29 | delay(300) 30 | } 31 | } 32 | 33 | runBlocking { delay(10000) } 34 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Chapter09/src/main/kotlin/com/rivuchk/functionalkotlin/chapter09/chapter9_7.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter09 2 | 3 | import io.reactivex.Observable 4 | import io.reactivex.Observer 5 | import io.reactivex.disposables.Disposable 6 | 7 | fun main(args: Array) { 8 | val observable = Observable.range(1,5)//1 9 | 10 | observable.subscribe({//2 11 | //onNext method 12 | println("Next-> $it") 13 | },{ 14 | //onError Method 15 | println("Error=> ${it.message}") 16 | },{ 17 | //onComplete Method 18 | println("Done") 19 | }) 20 | 21 | val observer: Observer = object : Observer {//3 22 | override fun onComplete() { 23 | println("All Completed") 24 | } 25 | 26 | override fun onNext(item: Int) { 27 | println("Next-> $item") 28 | } 29 | 30 | override fun onError(e: Throwable) { 31 | println("Error Occurred=> ${e.message}") 32 | } 33 | 34 | override fun onSubscribe(d: Disposable) { 35 | println("New Subscription ") 36 | } 37 | } 38 | 39 | observable.subscribe(observer) 40 | } 41 | -------------------------------------------------------------------------------- /Chapter08/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter08/chapter8_6_maps.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter08 2 | 3 | fun main(args: Array) { 4 | val map = mapOf( 5 | "One".to(1), 6 | "Two".to(2), 7 | "Three".to(3), 8 | "Four".to(4), 9 | "Five".to(0),//(1) We placed 0 instead of 5 here, will be replaced later 10 | "Six".to(6), 11 | "Five".to(5)//(2) This will replace earlier map of "Five".to(0) 12 | ) 13 | 14 | println("The value at Key `Four` is ${map["Four"]}") 15 | 16 | println("Contents in map") 17 | for(entry in map) { 18 | println("Key ${entry.key}, Value ${entry.value}") 19 | } 20 | 21 | val mutableMap = mutableMapOf() 22 | 23 | mutableMap.put(1,"Item 1") 24 | mutableMap.put(2,"Item 2") 25 | mutableMap.put(3,"Item 3") 26 | mutableMap.put(4,"Item 4") 27 | 28 | println("Replacing value at key 1 - ${mutableMap.put(1,"Item 5")}")//(3) 29 | 30 | println("Contents in mutableMap") 31 | for(entry in mutableMap) { 32 | println("Key ${entry.key}, Value ${entry.value}") 33 | } 34 | } -------------------------------------------------------------------------------- /Chapter09/src/main/kotlin/com/rivuchk/functionalkotlin/chapter09/chapter9_3.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter09 2 | 3 | import io.reactivex.Observable 4 | import io.reactivex.Observer 5 | import io.reactivex.disposables.Disposable 6 | import io.reactivex.rxkotlin.toObservable 7 | 8 | fun main(args: Array) { 9 | 10 | val observer = object :Observer{//1 11 | override fun onComplete() {//2 12 | println("All Completed") 13 | } 14 | 15 | override fun onNext(item: Any) {//3 16 | println("Next $item") 17 | } 18 | 19 | override fun onError(e: Throwable) {//4 20 | println("Error Occured $e") 21 | } 22 | 23 | override fun onSubscribe(d: Disposable) {//5 24 | println("Subscribed to $d") 25 | } 26 | } 27 | 28 | val observable = listOf(1, "Two", 3, "Four", "Five", 5.5f).toObservable() //6 29 | 30 | observable.subscribe(observer)//7 31 | 32 | val observableOnList = Observable.just(listOf("One", 2, "Three", "Four", 4.5, "Five", 6.0f), 33 | listOf("List with 1 Item"), 34 | listOf(1,2,3))//8 35 | 36 | 37 | 38 | observableOnList.subscribe(observer)//9 39 | 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Chapter07/src/main/kotlin/com/packtpub/functionalkotlin/chapter07/composing.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter07 2 | 3 | import kotlinx.coroutines.experimental.async 4 | import kotlinx.coroutines.experimental.delay 5 | 6 | suspend fun doSomethingUsefulOne(): Int { 7 | delay(1000L) 8 | return 13 9 | } 10 | 11 | suspend fun doSomethingUsefulTwo(): Int { 12 | delay(1000L) 13 | return 29 14 | } 15 | 16 | fun asyncSomethingUsefulOne() = async { 17 | doSomethingUsefulOne() 18 | } 19 | 20 | fun asyncSomethingUsefulTwo() = async { 21 | doSomethingUsefulTwo() 22 | } 23 | 24 | /*fun main(args: Array) = runBlocking { 25 | val time = measureTimeMillis { 26 | val one = async(start = CoroutineStart.LAZY) { doSomethingUsefulOne() } 27 | val two = async(start = CoroutineStart.LAZY) { doSomethingUsefulTwo() } 28 | println("The answer is ${one.await() + two.await()}") 29 | } 30 | println("Completed in = $time ms") 31 | }*/ 32 | 33 | /* 34 | fun main(args: Array) { 35 | val time = measureTimeMillis { 36 | val one = asyncSomethingUsefulOne() 37 | val two = asyncSomethingUsefulTwo() 38 | runBlocking { 39 | println("The answer is ${one.await() + two.await()}") 40 | } 41 | } 42 | println("Completed in = $time ms") 43 | }*/ 44 | -------------------------------------------------------------------------------- /Chapter06/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter06/chapter6_7_map.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter06 2 | 3 | import java.text.SimpleDateFormat 4 | import java.util.* 5 | 6 | data class Book (val delegate:Map) { 7 | val name:String by delegate 8 | val authors:String by delegate 9 | val pageCount:Int by delegate 10 | val publicationDate:Date by delegate 11 | val publisher:String by delegate 12 | } 13 | 14 | fun main(args: Array) { 15 | val map1 = mapOf( 16 | Pair("name","Reactive Programming in Kotlin"), 17 | Pair("authors","Rivu Chakraborty"), 18 | Pair("pageCount",400), 19 | Pair("publicationDate",SimpleDateFormat("yyyy/MM/dd").parse("2017/12/05")), 20 | Pair("publisher","Packt") 21 | ) 22 | val map2 = mapOf( 23 | "name" to "Kotlin Blueprints", 24 | "authors" to "Ashish Belagali, Hardik Trivedi, Akshay Chordiya", 25 | 26 | "publicationDate" to SimpleDateFormat("yyyy/MM/dd").parse("2017/12/10"), 27 | "publisher" to "Packt" 28 | ) 29 | 30 | val book1 = Book(map1) 31 | val book2 = Book(map2) 32 | book2.pageCount 33 | 34 | println("Book1 $book1 \nBook2 $book2") 35 | } -------------------------------------------------------------------------------- /Chapter13/build.gradle: -------------------------------------------------------------------------------- 1 | group 'com.packtpub' 2 | version '1.0-SNAPSHOT' 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.2.20' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | dependencies { 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | } 13 | } 14 | 15 | apply plugin: 'java' 16 | apply plugin: 'kotlin' 17 | apply plugin: 'kotlin-kapt' 18 | apply from: rootProject.file('gradle/generated-kotlin-sources.gradle') 19 | 20 | sourceCompatibility = 1.8 21 | 22 | repositories { 23 | jcenter() 24 | mavenCentral() 25 | mavenLocal() 26 | } 27 | 28 | dependencies { 29 | compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 30 | compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" 31 | compile 'io.arrow-kt:arrow-syntax:0.6.1' 32 | compile 'io.arrow-kt:arrow-optics:0.6.1' 33 | compile 'io.arrow-kt:arrow-effects:0.6.1' 34 | kapt 'io.arrow-kt:arrow-annotations-processor:0.6.1' 35 | testCompile group: 'junit', name: 'junit', version: '4.12' 36 | } 37 | 38 | compileKotlin { 39 | kotlinOptions.jvmTarget = "1.8" 40 | } 41 | compileTestKotlin { 42 | kotlinOptions.jvmTarget = "1.8" 43 | } 44 | kotlin { 45 | experimental { 46 | coroutines "enable" 47 | } 48 | } -------------------------------------------------------------------------------- /Chapter12/build.gradle: -------------------------------------------------------------------------------- 1 | group 'com.packtpub' 2 | version '1.0-SNAPSHOT' 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.2.20' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | dependencies { 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | } 13 | } 14 | 15 | apply plugin: 'java' 16 | apply plugin: 'kotlin' 17 | apply plugin: 'kotlin-kapt' 18 | apply from: rootProject.file('gradle/generated-kotlin-sources.gradle') 19 | 20 | sourceCompatibility = 1.8 21 | 22 | repositories { 23 | jcenter() 24 | mavenCentral() 25 | mavenLocal() 26 | } 27 | 28 | dependencies { 29 | compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 30 | compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" 31 | compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.21.2' 32 | compile 'io.arrow-kt:arrow-syntax:0.6.1' 33 | compile 'io.arrow-kt:arrow-optics:0.6.1' 34 | kapt 'io.arrow-kt:arrow-annotations-processor:0.6.1' 35 | testCompile group: 'junit', name: 'junit', version: '4.12' 36 | } 37 | 38 | compileKotlin { 39 | kotlinOptions.jvmTarget = "1.8" 40 | } 41 | compileTestKotlin { 42 | kotlinOptions.jvmTarget = "1.8" 43 | } 44 | kotlin { 45 | experimental { 46 | coroutines "enable" 47 | } 48 | } -------------------------------------------------------------------------------- /Chapter09/src/main/kotlin/com/rivuchk/functionalkotlin/chapter09/chapter9_8.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter09 2 | 3 | import io.reactivex.Observable 4 | import io.reactivex.Observer 5 | import io.reactivex.disposables.Disposable 6 | import kotlinx.coroutines.experimental.delay 7 | import kotlinx.coroutines.experimental.runBlocking 8 | import java.util.concurrent.TimeUnit 9 | 10 | fun main(args: Array) { 11 | 12 | val observale = Observable.interval(100, TimeUnit.MILLISECONDS)//1 13 | val observer = object : Observer { 14 | 15 | lateinit var disposable: Disposable//2 16 | 17 | override fun onSubscribe(d: Disposable) { 18 | disposable = d//3 19 | } 20 | 21 | override fun onNext(item: Long) { 22 | println("Received $item") 23 | if (item >= 10 && !disposable.isDisposed) {//4 24 | disposable.dispose()//5 25 | println("Disposed") 26 | } 27 | } 28 | 29 | override fun onError(e: Throwable) { 30 | println("Error ${e.message}") 31 | } 32 | 33 | override fun onComplete() { 34 | println("Complete") 35 | } 36 | 37 | } 38 | runBlocking { 39 | observale.subscribe(observer) 40 | delay(1500)//6 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Chapter09/src/main/kotlin/com/rivuchk/functionalkotlin/chapter09/chapter9_4.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter09 2 | 3 | import io.reactivex.Observable 4 | import io.reactivex.Observer 5 | import io.reactivex.disposables.Disposable 6 | 7 | fun main(args: Array) { 8 | 9 | val observer: Observer = object : Observer { 10 | override fun onComplete() { 11 | println("All Completed") 12 | } 13 | 14 | override fun onNext(item: String) { 15 | println("Next $item") 16 | } 17 | 18 | override fun onError(e: Throwable) { 19 | println("Error Occured => ${e.message}") 20 | } 21 | 22 | override fun onSubscribe(d: Disposable) { 23 | println("New Subscription ") 24 | } 25 | }//Create Observer 26 | 27 | val observable:Observable = Observable.create {//1 28 | it.onNext("Emitted 1") 29 | it.onNext("Emitted 2") 30 | it.onNext("Emitted 3") 31 | it.onNext("Emitted 4") 32 | it.onComplete() 33 | } 34 | 35 | observable.subscribe(observer) 36 | 37 | val observable2:Observable = Observable.create {//2 38 | it.onNext("Emitted 1") 39 | it.onNext("Emitted 2") 40 | it.onError(Exception("My Exception")) 41 | } 42 | 43 | observable2.subscribe(observer) 44 | } 45 | -------------------------------------------------------------------------------- /Chapter07/build.gradle: -------------------------------------------------------------------------------- 1 | group 'com.packtpub' 2 | version '1.0-SNAPSHOT' 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.2.20' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | dependencies { 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | } 13 | } 14 | 15 | apply plugin: 'java' 16 | apply plugin: 'kotlin' 17 | 18 | sourceCompatibility = 1.8 19 | 20 | repositories { 21 | jcenter() 22 | mavenCentral() 23 | mavenLocal() 24 | } 25 | 26 | dependencies { 27 | compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 28 | compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" 29 | compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.21.2' 30 | compile 'com.h2database:h2:1.4.196' 31 | compile 'org.springframework:spring-jdbc:5.0.2.RELEASE' 32 | compile 'org.http4k:http4k-client-apache:3.13.0' 33 | compile 'com.github.salomonbrys.kotson:kotson:2.5.0' 34 | compile 'nl.komponents.kovenant:kovenant:3.3.0' 35 | testCompile group: 'junit', name: 'junit', version: '4.12' 36 | } 37 | 38 | compileKotlin { 39 | kotlinOptions{ 40 | jvmTarget = "1.8" 41 | freeCompilerArgs = ["-Xskip-runtime-version-check"] 42 | } 43 | } 44 | compileTestKotlin { 45 | kotlinOptions.jvmTarget = "1.8" 46 | } 47 | kotlin { 48 | experimental { 49 | coroutines "enable" 50 | } 51 | } -------------------------------------------------------------------------------- /Chapter12/src/main/kotlin/com/packtpub/functionalkotlin/chapter12/currying.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter12 2 | 3 | import arrow.syntax.function.curried 4 | import arrow.syntax.function.invoke 5 | 6 | /* 7 | fun main(args: Array) { 8 | 9 | val strong: (String, String, String) -> String = { body, id, style -> "$body" } 10 | 11 | val curriedStrong: (style: String) -> (id: String) -> (body: String) -> String = strong.reverse().curried() 12 | 13 | val greenStrong: (id: String) -> (body: String) -> String = curriedStrong("color:green") 14 | 15 | val uncurriedGreenStrong: (id: String, body: String) -> String = greenStrong.uncurried() 16 | 17 | println(greenStrong("movie5")("Green Inferno")) 18 | 19 | println(uncurriedGreenStrong("movie6", "Green Hornet")) 20 | 21 | "Fried Green Tomatoes" pipe ("movie7" pipe greenStrong) pipe ::println 22 | }*/ 23 | 24 | fun main(args: Array) { 25 | val strong: (String, String, String) -> String = { body, id, style -> "$body" } 26 | 27 | println(strong.curried()("Batman Begins")("trilogy1")("color:black")) // Curried 28 | 29 | println(strong("The Dark Knight")("trilogy2")("color:black")) // Fake curried, just partial application 30 | 31 | println(strong(p2 = "trilogy3")(p2 = "color:black")("The Dark Knight rises")) // partial application 32 | 33 | } -------------------------------------------------------------------------------- /Chapter05/src/main/kotlin/com/packtpub/functionalkotlin/chapter05/corecursion.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter05 2 | 3 | /** 4 | * Created by IntelliJ IDEA. 5 | * @author Mario Arias 6 | * Date: 12/12/17 7 | * Time: 3:27 AM 8 | */ 9 | 10 | fun unfold(s: S, f: (S) -> Pair?): Sequence { 11 | val result = f(s) 12 | return if (result != null) { 13 | sequenceOf(result.first) + unfold(result.second, f) 14 | } else { 15 | sequenceOf() 16 | } 17 | } 18 | 19 | fun elements(element: T, numOfValues: Int): Sequence { 20 | return unfold(1) { i -> 21 | if (numOfValues > i) 22 | element to i + 1 23 | else 24 | null 25 | } 26 | } 27 | 28 | fun factorial(size: Int): Sequence { 29 | return sequenceOf(1L) + unfold(1L to 1) { (acc, n) -> 30 | if (size > n) { 31 | val x = n * acc 32 | (x) to (x to n + 1) 33 | } else 34 | null 35 | } 36 | } 37 | 38 | fun fib(size: Int): Sequence { 39 | return sequenceOf(1L) + unfold(Triple(0L, 1L, 1)) { (cur, next, n) -> 40 | if (size > n) { 41 | val x = cur + next 42 | (x) to Triple(next, x, n + 1) 43 | } 44 | else 45 | null 46 | } 47 | } 48 | 49 | fun main(args: Array) { 50 | val strings = elements("Kotlin", 5) 51 | strings.forEach(::println) 52 | 53 | val factorials = factorial(10) 54 | factorials.forEach(::println) 55 | 56 | val fibonacci = fib(5) 57 | fibonacci.forEach(::println) 58 | } -------------------------------------------------------------------------------- /Chapter01/src/main/kotlin/com/packtpub/functionalkotlin/chapter01/nullable.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter01 2 | 3 | /* 4 | fun main(args: Array) { 5 | val myBlueberryCupcake: Cupcake = null //Compilation error: Null can not be a value of a non-null type Cupcake 6 | }*/ 7 | 8 | /* 9 | fun main(args: Array) { 10 | val myBlueberryCupcake: Cupcake? = null 11 | }*/ 12 | 13 | /* 14 | fun eat(cupcake: Cupcake?){ 15 | // something happens here 16 | } 17 | 18 | fun main(args: Array) { 19 | val myAlmondCupcake = Cupcake.almond() 20 | 21 | eat(myAlmondCupcake) 22 | 23 | eat(null) 24 | }*/ 25 | 26 | /* 27 | fun main(args: Array) { 28 | val cupcake: Cupcake = Cupcake.almond() 29 | val nullableCupcake: Cupcake? = Cupcake.almond() 30 | 31 | cupcake.eat() // Happy days 32 | nullableCupcake.eat() //Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Cupcake? 33 | }*/ 34 | 35 | 36 | fun main(args: Array) { 37 | val nullableCupcake: Cupcake? = Cupcake.almond() 38 | 39 | if (nullableCupcake != null) { 40 | nullableCupcake.eat() 41 | } 42 | 43 | if (nullableCupcake is Cupcake) { 44 | nullableCupcake.eat() 45 | } 46 | 47 | when (nullableCupcake) { 48 | is Cupcake -> nullableCupcake.eat() 49 | } 50 | 51 | nullableCupcake?.eat() 52 | 53 | val result: String? = nullableCupcake?.eat() 54 | 55 | val length: Int? = nullableCupcake?.eat()?.length 56 | 57 | 58 | 59 | } -------------------------------------------------------------------------------- /Chapter05/src/main/kotlin/com/packtpub/functionalkotlin/chapter05/operators.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter05 2 | 3 | /** 4 | * Created by IntelliJ IDEA. 5 | * @author Mario Arias 6 | * Date: 4/12/17 7 | * Time: 12:03 AM 8 | */ 9 | 10 | 11 | enum class WolfActions { 12 | SLEEP, WALK, BITE 13 | } 14 | 15 | enum class WolfRelationships { 16 | FRIEND, SIBLING, ENEMY, PARTNER 17 | } 18 | 19 | operator fun Wolf.set(relationship: WolfRelationships, wolf: Wolf): Int { 20 | println("${wolf.name} is my new $relationship") 21 | return 1 22 | } 23 | 24 | operator fun Wolf.not() = "$name is angry!!!" 25 | 26 | 27 | class Wolf(val name: String) { 28 | operator fun plus(wolf: Wolf) = Pack(mapOf(name to this, wolf.name to wolf)) 29 | 30 | operator fun invoke(action: WolfActions) = when (action) { 31 | WolfActions.SLEEP -> "$name is sleeping" 32 | WolfActions.WALK -> "$name is walking" 33 | WolfActions.BITE -> "$name is biting" 34 | } 35 | 36 | 37 | } 38 | 39 | class Pack(val members: Map) 40 | 41 | operator fun Pack.get(name:String) = members[name]!! 42 | 43 | 44 | operator fun Pack.plus(wolf: Wolf) = Pack(this.members.toMutableMap() + (wolf.name to wolf)) 45 | 46 | fun main(args: Array) { 47 | val talbot = Wolf("Talbot") 48 | 49 | talbot(WolfActions.SLEEP) 50 | 51 | val northPack: Pack = talbot + Wolf("Big Bertha") // talbot.plus(Wolf("...")) 52 | 53 | val biggerPack = northPack + Wolf("Bad Wolf") 54 | 55 | val badWolf = biggerPack["Bad Wolf"] 56 | 57 | talbot[WolfRelationships.ENEMY] = badWolf 58 | 59 | !talbot 60 | 61 | 62 | } -------------------------------------------------------------------------------- /Chapter12/src/main/kotlin/com/packtpub/functionalkotlin/chapter12/partials.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter12 2 | 3 | import arrow.syntax.function.bind 4 | 5 | fun partialSplitter(billAndOrder: Pair?, warehouse: (PickingOrder) -> Unit, accounting: (Bill) -> Unit) { 6 | if (billAndOrder != null) { 7 | warehouse(billAndOrder.second) 8 | accounting(billAndOrder.first) 9 | } 10 | } 11 | 12 | /*fun main(args: Array) { 13 | val splitter: (billAndOrder: Pair?) -> Unit = ::partialSplitter.partially2 { order -> println("TESTING $order") }(p2 = ::accounting) 14 | 15 | val salesSystem: (quote: Quote) -> Unit = ::calculatePrice andThen ::filterBills forwardCompose splitter 16 | salesSystem(Quote(20.0, "Foo", "Shoes", 1)) 17 | salesSystem(Quote(20.0, "Bar", "Shoes", 200)) 18 | salesSystem(Quote(2000.0, "Foo", "Motorbike", 1)) 19 | } 20 | 21 | fun main(args: Array) { 22 | val strong: (String, String, String) -> String = { body, id, style -> "$body" } 23 | 24 | val redStrong: (String, String) -> String = strong.partially3("font: red") //Explicit 25 | 26 | val blueStrong: (String, String) -> String = strong(p3 = "font: blue") //Implicit 27 | 28 | println(redStrong("Red Sonja", "movie1")) 29 | println(blueStrong("Deep Blue Sea", "movie2")) 30 | }*/ 31 | 32 | fun main(args: Array) { 33 | 34 | val footer:(String) -> String = {content -> "
$content
"} 35 | 36 | val fixFooter: () -> String = footer.bind("Functional Kotlin - 2018") //alias for partially1 37 | 38 | println(fixFooter()) 39 | } 40 | -------------------------------------------------------------------------------- /Chapter07/src/main/kotlin/com/packtpub/functionalkotlin/chapter07/contexts.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter07 2 | 3 | import kotlinx.coroutines.experimental.* 4 | 5 | fun main(args: Array) = runBlocking { 6 | println("run blocking coroutineContext = $coroutineContext") 7 | println("coroutineContext[Job] = ${coroutineContext[Job]}") 8 | println(Thread.currentThread().name) 9 | println("-----") 10 | 11 | val jobs = listOf( 12 | launch { 13 | println("launch coroutineContext = $coroutineContext") 14 | println("coroutineContext[Job] = ${coroutineContext[Job]}") 15 | println(Thread.currentThread().name) 16 | println("-----") 17 | }, 18 | async { 19 | println("async coroutineContext = $coroutineContext") 20 | println("coroutineContext[Job] = ${coroutineContext[Job]}") 21 | println(Thread.currentThread().name) 22 | println("-----") 23 | }, 24 | launch(CommonPool) { 25 | println("common launch coroutineContext = $coroutineContext") 26 | println("coroutineContext[Job] = ${coroutineContext[Job]}") 27 | println(Thread.currentThread().name) 28 | println("-----") 29 | }, 30 | launch(coroutineContext) { 31 | println("inherit launch coroutineContext = $coroutineContext") 32 | println("coroutineContext[Job] = ${coroutineContext[Job]}") 33 | println(Thread.currentThread().name) 34 | println("-----") 35 | } 36 | ) 37 | 38 | jobs.forEach { job -> 39 | println("job = $job") 40 | job.join() 41 | } 42 | } 43 | 44 | 45 | /*fun main(args: Array) = runBlocking { 46 | 47 | val job = launch { 48 | repeat(1_000_000) { 49 | launch(coroutineContext) { 50 | delay(1000) 51 | print('.') 52 | } 53 | } 54 | } 55 | 56 | job.join() 57 | }*/ 58 | -------------------------------------------------------------------------------- /Chapter02/src/main/kotlin/com/packtpub/functionalkotlin/chapter02/collections.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter02 2 | 3 | val numbers: List = listOf(1, 2, 3, 4) 4 | 5 | fun main(args: Array) { 6 | val sum = numbers.reduce { acc, i -> 7 | println("acc, i = $acc, $i") 8 | acc + i 9 | } 10 | 11 | println(sum) 12 | } 13 | 14 | /*fun main(args: Array) { 15 | val sum = numbers.fold(0) { acc, i -> 16 | println("acc, i = $acc, $i") 17 | acc + i 18 | } 19 | 20 | println(sum) 21 | }*/ 22 | 23 | /*fun main(args: Array) { 24 | val sum = numbers.fold(0) { acc, i -> acc + i } 25 | 26 | println(sum) 27 | }*/ 28 | 29 | /*fun main(args: Array) { 30 | val sum = numbers.sum() 31 | 32 | println(sum) 33 | }*/ 34 | 35 | /*fun main(args: Array) { 36 | var sum = 0 37 | 38 | for (i in numbers) { 39 | sum += i 40 | } 41 | 42 | println(sum) 43 | }*/ 44 | 45 | /*fun main(args: Array) { 46 | val numbersTwice: List = numbers.map { i -> i * 2 } 47 | }*/ 48 | 49 | /*fun main(args: Array) { 50 | val numbersTwice: MutableList = mutableListOf() 51 | 52 | for (i in numbers) { 53 | numbersTwice.add(i * 2) //Nice! 54 | } 55 | }*/ 56 | 57 | /*fun main(args: Array) { 58 | val numbersTwice: List = listOf() 59 | 60 | for (i in numbers) { 61 | numbersTwice.add(i * 2) //Compilation error: Unresolved reference: add 62 | } 63 | }*/ 64 | 65 | /* 66 | fun main(args: Array) { 67 | for(i in numbers) { 68 | println("i = $i") 69 | } 70 | }*/ 71 | 72 | /* 73 | fun main(args: Array) { 74 | numbers.forEach { i -> println("i = $i") } 75 | }*/ 76 | -------------------------------------------------------------------------------- /Chapter01/src/main/kotlin/com/packtpub/functionalkotlin/chapter01/inheritance.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter01 2 | 3 | 4 | /* 5 | class Biscuit(val flavour: String) { 6 | fun eat(): String { 7 | return "nom, nom, nom... delicious $flavour biscuit" 8 | } 9 | }*/ 10 | 11 | /*open class BakeryGood(val flavour: String) { 12 | fun eat(): String { 13 | return "nom, nom, nom... delicious $flavour bakery good" 14 | } 15 | } 16 | 17 | class Cupcake(flavour: String): BakeryGood(flavour) 18 | class Biscuit(flavour: String): BakeryGood(flavour)*/ 19 | 20 | /*open class BakeryGood(val flavour: String) { 21 | fun eat(): String { 22 | return "nom, nom, nom... delicious $flavour ${name()}" 23 | } 24 | 25 | open fun name(): String { 26 | return "bakery good" 27 | } 28 | }*/ 29 | 30 | /*class Cupcake(flavour: String) : BakeryGood(flavour) { 31 | override fun name(): String { 32 | return "cupcake" 33 | } 34 | }*/ 35 | 36 | class Biscuit(flavour: String) : BakeryGood(flavour) { 37 | override fun name(): String { 38 | return "biscuit" 39 | } 40 | } 41 | 42 | 43 | /* 44 | fun main(args: Array) { 45 | val myBlueberryCupcake: BakeryGood = Cupcake("Blueberry") 46 | println(myBlueberryCupcake.eat()) 47 | }*/ 48 | 49 | open class Roll(flavour: String) : BakeryGood(flavour) { 50 | override fun name(): String { 51 | return "roll" 52 | } 53 | } 54 | 55 | 56 | 57 | /*open class Donut(flavour: String, val topping: String) : BakeryGood(flavour) { 58 | override fun name(): String { 59 | return "donut with $topping topping" 60 | } 61 | }*/ 62 | 63 | fun main(args: Array) { 64 | val myDonut = Donut("Custard", "Powdered sugar") 65 | println(myDonut.eat()) 66 | } -------------------------------------------------------------------------------- /Chapter06/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter06/chapter6_8_custom.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter06 2 | 3 | import kotlin.properties.ReadWriteProperty 4 | import kotlin.reflect.KProperty 5 | 6 | abstract class MakeEven(initialValue: Int):ReadWriteProperty { 7 | private var value:Int = initialValue 8 | 9 | override fun getValue(thisRef: Any?, property: KProperty<*>) = value 10 | 11 | override fun setValue(thisRef: Any?, property: KProperty<*>, value: Int) { 12 | val oldValue = value 13 | val wasEven = value%2==0 14 | if(wasEven) { 15 | this.value = value 16 | } else { 17 | this.value = value+1 18 | } 19 | afterAssignmentCall(property,oldValue,value,wasEven) 20 | } 21 | 22 | abstract fun afterAssignmentCall (property: KProperty<*>, oldValue: Int, newValue: Int, wasEven:Boolean):Unit 23 | } 24 | 25 | inline fun makeEven(initialValue: Int, crossinline onAssignment:(property: KProperty<*>, oldValue: Int, newValue: Int, wasEven:Boolean)->Unit):ReadWriteProperty 26 | =object : MakeEven(initialValue){ 27 | override fun afterAssignmentCall(property: KProperty<*>, oldValue: Int, newValue: Int, wasEven: Boolean) 28 | = onAssignment(property,oldValue,newValue,wasEven) 29 | } 30 | 31 | var myEven:Int by makeEven(0) { 32 | property, oldValue, newValue, wasEven -> 33 | println("${property.name} $oldValue -> $newValue, Even:$wasEven") 34 | } 35 | 36 | fun main(args: Array) { 37 | myEven = 6 38 | println("myEven:$myEven") 39 | myEven = 3 40 | println("myEven:$myEven") 41 | myEven = 5 42 | println("myEven:$myEven") 43 | myEven = 8 44 | println("myEven:$myEven") 45 | } 46 | -------------------------------------------------------------------------------- /Chapter01/src/main/kotlin/com/packtpub/functionalkotlin/chapter01/classes.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter01 2 | 3 | //class VeryBasic 4 | 5 | /*fun main(args: Array) { 6 | val basic: VeryBasic = VeryBasic() 7 | }*/ 8 | 9 | 10 | /* 11 | fun main(args: Array) { 12 | val basic = VeryBasic() 13 | }*/ 14 | 15 | 16 | /* 17 | class BlueberryCupcake { 18 | var flavour = "Blueberry" 19 | } 20 | 21 | fun main(args: Array) { 22 | val myCupcake = BlueberryCupcake() 23 | myCupcake.flavour = "Almond" 24 | println("My cupcake has ${myCupcake.flavour}") 25 | }*/ 26 | 27 | /* 28 | class BlueberryCupcake { 29 | val flavour = "Blueberry" 30 | } 31 | 32 | fun main(args: Array) { 33 | val myCupcake = BlueberryCupcake() 34 | myCupcake.flavour = "Almond" //Compilation error: Val cannot be reassigned 35 | println("My cupcake has ${myCupcake.flavour}") 36 | }*/ 37 | 38 | /* 39 | class AlmondCupcake { 40 | val flavour = "Almond" 41 | } 42 | 43 | fun main(args: Array) { 44 | val mySecondCupcake = AlmondCupcake() 45 | println("My second cupcake has ${mySecondCupcake.flavour} flavour") 46 | }*/ 47 | 48 | /*class Cupcake(flavour: String) { 49 | val flavour = flavour 50 | }*/ 51 | 52 | /* 53 | class Cupcake(val flavour: String) 54 | 55 | fun main(args: Array) { 56 | val myBlueberryCupcake = Cupcake("Blueberry") 57 | val myAlmondCupcake = Cupcake("Almond") 58 | val myCheeseCupcake = Cupcake("Cheese") 59 | val myCaramelCupcake = Cupcake("Caramel") 60 | }*/ 61 | 62 | /* 63 | class Cupcake(val flavour: String) { 64 | fun eat(): String { 65 | return "nom, nom, nom... delicious $flavour cupcake" 66 | } 67 | } 68 | 69 | fun main(args: Array) { 70 | val myBlueberryCupcake = Cupcake("Blueberry") 71 | println(myBlueberryCupcake.eat()) 72 | }*/ 73 | -------------------------------------------------------------------------------- /Chapter01/src/main/kotlin/com/packtpub/functionalkotlin/chapter01/types.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter01 2 | 3 | /* 4 | fun main(args: Array) { 5 | 6 | val myAlmondCupcake = Cupcake.almond() 7 | 8 | val anyMachine = object : Machine { 9 | override fun process(product: Any) { 10 | println(product.toString()) 11 | } 12 | } 13 | 14 | anyMachine.process(3) 15 | 16 | anyMachine.process("") 17 | 18 | anyMachine.process(myAlmondCupcake) 19 | }*/ 20 | 21 | /* 22 | fun main(args: Array) { 23 | 24 | val anyMachine = object : Machine { 25 | override fun process(product: Any) { 26 | println(product.toString()) 27 | } 28 | } 29 | 30 | val nullableCupcake: Cupcake? = Cupcake.almond() 31 | 32 | anyMachine.process(nullableCupcake) //Error:(32, 24) Kotlin: Type mismatch: inferred type is Cupcake? but Any was expected 33 | }*/ 34 | 35 | /* 36 | fun main(args: Array) { 37 | val nullableCupcake: Cupcake? = Cupcake.almond() 38 | 39 | val length = nullableCupcake?.eat()?.length ?: "" 40 | }*/ 41 | 42 | @Target(AnnotationTarget.CLASS) 43 | @Retention(AnnotationRetention.RUNTIME) 44 | annotation class Tasty(val tasty: Boolean = true) 45 | 46 | @Tasty(false) 47 | object ElectricOven : Oven { 48 | override fun process(product: Bakeable) { 49 | println(product.bake()) 50 | } 51 | } 52 | 53 | @Tasty 54 | class CinnamonRoll : Roll("Cinnamon") 55 | 56 | @Tasty 57 | interface Fried { 58 | fun fry(): String 59 | } 60 | 61 | fun main(args: Array) { 62 | val annotations: List = ElectricOven::class.annotations 63 | 64 | for (annotation in annotations) { 65 | when (annotation) { 66 | is Tasty -> println("Is it tasty? ${annotation.tasty}") 67 | else -> println(annotation) 68 | } 69 | } 70 | } -------------------------------------------------------------------------------- /Chapter08/src/main/kotlin/com/rivuchk/packtpub/functionalkotlin/chapter08/chapter8_5_set_customdata.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.packtpub.functionalkotlin.chapter08 2 | 3 | data class MyDataClass (val someNumericValue:Int, val someStringValue:String) 4 | class MyCustomClass (val someNumericValue:Int, val someStringValue:String) { 5 | override fun toString(): String { 6 | return "MyCustomClass(someNumericValue=$someNumericValue, someStringValue=$someStringValue)" 7 | } 8 | 9 | override fun hashCode() = someStringValue.hashCode()+someNumericValue.hashCode() 10 | 11 | override fun equals(other: Any?): Boolean { 12 | return other is MyCustomClass && other.someNumericValue == someNumericValue && other.someStringValue==someStringValue 13 | } 14 | } 15 | 16 | fun main(args: Array) { 17 | val dataClassSet = setOf( 18 | MyDataClass(1,"1st obj"), 19 | MyDataClass(2,"2nd obj"), 20 | MyDataClass(3,"3rd obj"), 21 | MyDataClass(2,"2nd obj"), 22 | MyDataClass(4,"4th obj"), 23 | MyDataClass(5,"5th obj"), 24 | MyDataClass(2,"will be added"), 25 | MyDataClass(3,"3rd obj") 26 | ) 27 | 28 | println("Printing items of dataClassSet one by one") 29 | for(item in dataClassSet) { 30 | println(item) 31 | } 32 | 33 | val customClassSet = setOf( 34 | MyCustomClass(1,"1st obj"), 35 | MyCustomClass(2,"2nd obj"), 36 | MyCustomClass(3,"3rd obj"), 37 | MyCustomClass(2,"2nd obj"), 38 | MyCustomClass(4,"4th obj"), 39 | MyCustomClass(5,"5th obj"), 40 | MyCustomClass(5,"5th obj"), 41 | MyCustomClass(3,"3rd obj") 42 | ) 43 | 44 | println("Printing items of customClassSet one by one ${MyCustomClass(2,"2nd obj").equals(MyCustomClass(2,"2nd obj"))}") 45 | for(item in customClassSet) { 46 | println(item) 47 | } 48 | } -------------------------------------------------------------------------------- /Chapter09/src/main/kotlin/com/rivuchk/functionalkotlin/chapter09/chapter9_5.kt: -------------------------------------------------------------------------------- 1 | package com.rivuchk.functionalkotlin.chapter09 2 | 3 | import io.reactivex.Observable 4 | import io.reactivex.Observer 5 | import io.reactivex.disposables.Disposable 6 | import java.util.concurrent.Callable 7 | import java.util.concurrent.Future 8 | import java.util.concurrent.TimeUnit 9 | 10 | fun main(args: Array) { 11 | 12 | val observer: Observer = object : Observer { 13 | override fun onComplete() { 14 | println("Completed") 15 | } 16 | 17 | override fun onNext(item: String) { 18 | println("Received-> $item") 19 | } 20 | 21 | override fun onError(e: Throwable) { 22 | println("Error Occured => ${e.message}") 23 | } 24 | 25 | override fun onSubscribe(d: Disposable) { 26 | println("Subscription") 27 | } 28 | }//Create Observer 29 | 30 | val list = listOf("Str 1","Str 2","Str 3","Str 4") 31 | val observableFromIterable: Observable = Observable.fromIterable(list)//1 32 | observableFromIterable.subscribe(observer) 33 | 34 | 35 | val callable = object : Callable { 36 | override fun call(): String { 37 | return "I'm From Callable" 38 | } 39 | 40 | } 41 | val observableFromCallable:Observable = Observable.fromCallable(callable)//2 42 | observableFromCallable.subscribe(observer) 43 | 44 | val future:Future = object : Future { 45 | val retStr = "I'm from Future" 46 | 47 | override fun get() = retStr 48 | 49 | override fun get(timeout: Long, unit: TimeUnit?) = retStr 50 | 51 | override fun isDone(): Boolean = true 52 | 53 | override fun isCancelled(): Boolean = false 54 | 55 | override fun cancel(mayInterruptIfRunning: Boolean): Boolean = false 56 | 57 | } 58 | val observableFromFuture:Observable = Observable.fromFuture(future)//3 59 | observableFromFuture.subscribe(observer) 60 | } 61 | -------------------------------------------------------------------------------- /Chapter13/src/main/kotlin/com/packtpub/functionalkotlin/chapter13/types.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter13 2 | 3 | import arrow.HK 4 | import arrow.core.None 5 | import arrow.core.Option 6 | import arrow.core.Some 7 | import arrow.core.ev 8 | import arrow.deriving 9 | import arrow.higherkind 10 | import arrow.instance 11 | import arrow.typeclasses.Functor 12 | import arrow.typeclasses.functor 13 | 14 | @higherkind 15 | class Mappable(val t: T) : MappableKind { 16 | fun map(f: (T) -> R): Mappable = Mappable(f(t)) 17 | 18 | override fun toString(): String = "Mappable(t=$t)" 19 | 20 | companion object 21 | } 22 | 23 | @instance(Mappable::class) 24 | interface MappableFunctorInstance : Functor { 25 | override fun map(fa: MappableKind, f: (A) -> B): Mappable { 26 | return fa.ev().map(f) 27 | } 28 | } 29 | 30 | 31 | @higherkind 32 | @deriving(Functor::class) 33 | class DerivedMappable(val t: T) : DerivedMappableKind { 34 | fun map(f: (T) -> R): DerivedMappable = DerivedMappable(f(t)) 35 | 36 | override fun toString(): String = "DerivedMappable(t=$t)" 37 | 38 | companion object 39 | } 40 | 41 | 42 | @higherkind 43 | class NotAFunctor(val t: T) : NotAFunctorKind { 44 | fun map(f: (T) -> R): NotAFunctor = NotAFunctor(f(t)) 45 | 46 | override fun toString(): String = "NotAFunctor(t=$t)" 47 | } 48 | 49 | 50 | inline fun buildBicycle(mapper: HK, 51 | noinline f: (Int) -> Bicycle, 52 | FR: Functor = functor()): HK = FR.map(mapper, f) 53 | 54 | fun main(args: Array) { 55 | 56 | val mappable: Mappable = buildBicycle(Mappable(3), ::Bicycle).ev() 57 | println("mappable = $mappable") 58 | 59 | val derived: DerivedMappable = buildBicycle(DerivedMappable(3), ::Bicycle).ev() 60 | println("derived = $derived") 61 | 62 | val option: Option = buildBicycle(Some(2), ::Bicycle).ev() 63 | println("option = $option") 64 | 65 | val none: Option = buildBicycle(None, ::Bicycle).ev() 66 | println("none = $none") 67 | 68 | val not: NotAFunctor = buildBicycle(NotAFunctor(4), ::Bicycle).ev() 69 | println("not = $not") 70 | 71 | } 72 | -------------------------------------------------------------------------------- /Chapter05/src/main/kotlin/com/packtpub/functionalkotlin/chapter05/inline.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter05 2 | 3 | /** 4 | * Created by IntelliJ IDEA. 5 | * @author Mario Arias 6 | * Date: 1/12/17 7 | * Time: 3:11 AM 8 | */ 9 | 10 | 11 | fun time(body: () -> T): Pair { 12 | val startTime = System.nanoTime() 13 | val v = body() 14 | val endTime = System.nanoTime() 15 | return v to endTime - startTime 16 | } 17 | 18 | inline fun inTime(body: () -> T): Pair { 19 | val startTime = System.currentTimeMillis() 20 | val v = body() 21 | val endTime = System.currentTimeMillis() 22 | return v to endTime - startTime 23 | } 24 | 25 | typealias UserListener = (User) -> Unit 26 | 27 | data class User(val name: String) 28 | 29 | class UserService { 30 | val listeners = mutableListOf() 31 | val users = mutableListOf() 32 | 33 | inline fun transformName(crossinline transform: (name: String) -> String): List { 34 | 35 | val buildUser = { name: String -> 36 | User(transform(name)) //compilation error: Can't inline transform here 37 | } 38 | 39 | return users.map { user -> buildUser(user.name) } 40 | } 41 | 42 | inline fun runListeners(builder: () -> User) { 43 | listeners.forEach { listener -> listener(builder()) } 44 | } 45 | 46 | // fun addListener(listener: (User) -> Unit) { 47 | // listeners += listener 48 | // } 49 | 50 | // inline fun addListener(listener: (User) -> Unit) { 51 | // listeners += listener 52 | // } 53 | // 54 | inline fun addListener(noinline listener: (User) -> Unit) { 55 | listeners += listener 56 | } 57 | 58 | 59 | } 60 | 61 | fun main(args: Array) { 62 | // val (_, time) = time { Thread.sleep(1000) } 63 | /*val (_, time) = time(object : Function0 { 64 | override fun invoke() { 65 | Thread.sleep(1000) 66 | } 67 | })*/ 68 | // val (_, inTime) = inTime { Thread.sleep(1000) } 69 | 70 | /*val startTime = System.nanoTime() 71 | val v = Thread.sleep(1000) 72 | val endTime = System.nanoTime() 73 | val (_, inTime) = (v to endTime - startTime)*/ 74 | 75 | // println("time = $time") 76 | // println("inTime = $inTime") 77 | 78 | UserService().transformName(String::toLowerCase) 79 | 80 | } -------------------------------------------------------------------------------- /Chapter12/src/main/kotlin/com/packtpub/functionalkotlin/chapter12/partial.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter12 2 | 3 | import arrow.core.PartialFunction 4 | import arrow.core.orElse 5 | 6 | 7 | /* 8 | fun main(args: Array) { 9 | val upper: (String?) -> String = { s: String? -> s!!.toUpperCase() } //Partial function, it can't transform null 10 | 11 | listOf("one", "two", null, "four").map(upper).forEach(::println) 12 | }*/ 13 | 14 | 15 | /* 16 | fun main(args: Array) { 17 | 18 | val upper: (String?) -> String = { s: String? -> s!!.toUpperCase() } //Partial function, it can't transform null 19 | 20 | val partialUpper: PartialFunction = PartialFunction(definetAt = { s -> s != null }, f = upper) 21 | 22 | listOf("one", "two", null, "four").map(partialUpper).forEach(::println) 23 | }*/ 24 | 25 | /*fun main(args: Array) { 26 | 27 | val upper: (String?) -> String = { s: String? -> s!!.toUpperCase() } //Partial function, it can't transform null 28 | 29 | val partialUpper: PartialFunction = PartialFunction(definetAt = { s -> s != null }, f = upper) 30 | 31 | listOf("one", "two", null, "four").map{ s -> partialUpper.invokeOrElse(s, "NULL")}.forEach(::println) 32 | }*/ 33 | 34 | /*fun main(args: Array) { 35 | 36 | val upper: (String?) -> String = { s: String? -> s!!.toUpperCase() } //Partial function, it can't transform null 37 | 38 | val partialUpper: PartialFunction = PartialFunction(definetAt = { s -> s != null }, f = upper) 39 | 40 | val upperForNull: PartialFunction = PartialFunction({ s -> s == null }) { "NULL" } 41 | 42 | val totalUpper: PartialFunction = partialUpper orElse upperForNull 43 | 44 | listOf("one", "two", null, "four").map(totalUpper).forEach(::println) 45 | }*/ 46 | 47 | 48 | fun main(args: Array) { 49 | val fizz = PartialFunction({ n: Int -> n % 3 == 0 }) { "FIZZ" } 50 | val buzz = PartialFunction({ n: Int -> n % 5 == 0 }) { "BUZZ" } 51 | val fizzBuzz = PartialFunction({ n: Int -> fizz.isDefinedAt(n) && buzz.isDefinedAt(n) }) { "FUZZBIZZ" } 52 | val pass = PartialFunction({ true }) { n: Int -> n.toString() } 53 | 54 | (1..50).map(fizzBuzz orElse buzz orElse fizz orElse pass).forEach(::println) 55 | } -------------------------------------------------------------------------------- /Chapter07/src/main/kotlin/com/packtpub/functionalkotlin/chapter07/cancel.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter07 2 | 3 | /*fun main(args: Array) = runBlocking { 4 | val job = launch { 5 | repeat(1000) { i -> 6 | println("I'm sleeping = $i...") 7 | delay(500) 8 | } 9 | 10 | } 11 | delay(1300) 12 | println("main: I'm tired of waiting!") 13 | job.cancelAndJoin() 14 | println("main: Now I can quit") 15 | }*/ 16 | 17 | /* 18 | fun main(args: Array) = runBlocking { 19 | val startTime = System.currentTimeMillis() 20 | val job = launch { 21 | var nextPrimeTime = startTime 22 | var i = 0 23 | while (i < 5) { 24 | if(System.currentTimeMillis() >= nextPrimeTime) { 25 | println("I'm sleeping = ${i++}...") 26 | nextPrimeTime += 500L 27 | } 28 | } 29 | } 30 | delay(1300) 31 | println("main: I'm tired of waiting!") 32 | job.cancelAndJoin() 33 | println("main: Now I can quit.") 34 | }*/ 35 | 36 | /* 37 | fun main(args: Array) = runBlocking { 38 | val startTime = System.currentTimeMillis() 39 | val job = launch { 40 | var nextPrimeTime = startTime 41 | var i = 0 42 | while (isActive) { 43 | if(System.currentTimeMillis() >= nextPrimeTime) { 44 | println("I'm sleeping = ${i++}...") 45 | nextPrimeTime += 500L 46 | } 47 | } 48 | } 49 | delay(1300) 50 | println("main: I'm tired of waiting!") 51 | job.cancelAndJoin() 52 | println("main: Now I can quit.") 53 | }*/ 54 | 55 | /*fun main(args: Array) = runBlocking { 56 | val job = launch { 57 | try { 58 | repeat(1000) { i -> 59 | println("I'm sleeping = $i...") 60 | delay(500) 61 | } 62 | } finally { 63 | println("I'm running finally") 64 | } 65 | 66 | } 67 | delay(1300) 68 | println("main: I'm tired of waiting!") 69 | job.cancelAndJoin() 70 | println("main: Now I can quit") 71 | }*/ 72 | 73 | 74 | /* 75 | fun main(args: Array) = runBlocking { 76 | withTimeout(1300L) { 77 | repeat(1000) { i -> 78 | println("I'm sleeping = $i...") 79 | delay(500) 80 | } 81 | } 82 | }*/ 83 | 84 | /* 85 | fun main(args: Array) = runBlocking { 86 | val result = withTimeoutOrNull(1300L) { 87 | repeat(1000) { i -> 88 | println("I'm sleeping = $i...") 89 | delay(500) 90 | } 91 | "Done" 92 | } 93 | println("Result is $result") 94 | }*/ 95 | -------------------------------------------------------------------------------- /Chapter12/src/main/kotlin/com/packtpub/functionalkotlin/chapter12/composition.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter12 2 | 3 | import arrow.syntax.function.andThen 4 | import arrow.syntax.function.forwardCompose 5 | import java.util.* 6 | 7 | 8 | data class Quote(val value: Double, val client: String, val item: String, val quantity: Int) 9 | 10 | data class Bill(val value: Double, val client: String) 11 | 12 | data class PickingOrder(val item: String, val quantity: Int) 13 | 14 | fun calculatePrice(quote: Quote) = Bill(quote.value * quote.quantity, quote.client) to PickingOrder(quote.item, quote.quantity) 15 | 16 | fun filterBills(billAndOrder: Pair): Pair? { 17 | val (bill, _) = billAndOrder 18 | return if (bill.value >= 100) { 19 | billAndOrder 20 | } else { 21 | null 22 | } 23 | } 24 | 25 | fun warehouse(order: PickingOrder) { 26 | println("Processing order = $order") 27 | } 28 | 29 | fun accounting(bill: Bill) { 30 | println("processing = $bill") 31 | } 32 | 33 | fun splitter(billAndOrder: Pair?) { 34 | if (billAndOrder != null) { 35 | warehouse(billAndOrder.second) 36 | accounting(billAndOrder.first) 37 | } 38 | } 39 | 40 | fun main(args: Array) { 41 | val salesSystem: (quote: Quote) -> Unit = ::calculatePrice andThen ::filterBills forwardCompose ::splitter 42 | salesSystem(Quote(20.0, "Foo", "Shoes", 1)) 43 | salesSystem(Quote(20.0, "Bar", "Shoes", 200)) 44 | salesSystem(Quote(2000.0, "Foo", "Motorbike", 1)) 45 | } 46 | 47 | val strong: (String) -> String = { body -> "$body" } 48 | 49 | val p: (String) -> String = { body -> "

$body

" } 50 | 51 | val span: (String) -> String = { body -> "$body" } 52 | 53 | val div: (String) -> String = { body -> "
$body
" } 54 | 55 | val randomNames: () -> String = { 56 | if (Random().nextInt() % 2 == 0) { 57 | "foo" 58 | } else { 59 | "bar" 60 | } 61 | } 62 | 63 | /* 64 | fun main(args: Array) { 65 | val divStrong: (String) -> String = div compose strong 66 | 67 | val spanP: (String) -> String = p forwardCompose span 68 | 69 | val randomStrong: () -> String = randomNames andThen strong 70 | 71 | println(divStrong("Hello composition world!")) 72 | println(spanP("Hello composition world!")) 73 | println(randomStrong()) 74 | }*/ 75 | -------------------------------------------------------------------------------- /Chapter12/src/main/kotlin/com/packtpub/functionalkotlin/chapter12/optics.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter12 2 | 3 | import arrow.lenses 4 | import arrow.optics.Lens 5 | import arrow.optics.modify 6 | 7 | typealias GB = Int 8 | 9 | @lenses data class Memory(val size: GB) 10 | @lenses data class MotherBoard(val brand: String, val memory: Memory) 11 | @lenses data class Laptop(val price: Double, val motherBoard: MotherBoard) 12 | 13 | 14 | /* 15 | fun main(args: Array) { 16 | val laptopX8 = Laptop(500.0, MotherBoard("X", Memory(8))) 17 | 18 | val laptopX16 = laptopX8.copy( 19 | price = 780.0, 20 | motherBoard = laptopX8.motherBoard.copy( 21 | memory = laptopX8.motherBoard.memory.copy( 22 | size = laptopX8.motherBoard.memory.size * 2 23 | ) 24 | ) 25 | ) 26 | 27 | println("laptopX16 = $laptopX16") 28 | }*/ 29 | 30 | 31 | val laptopPrice: Lens = Lens( 32 | get = { laptop -> laptop.price }, 33 | set = { price -> { laptop -> laptop.copy(price = price) } } 34 | ) 35 | 36 | /*val laptopPrice: Lens = Lens( 37 | get = { laptop -> laptop.price }, 38 | set = { price: Double, laptop: Laptop -> laptop.copy(price = price) }.curried() 39 | ) 40 | 41 | val laptopMotherBoard: Lens = Lens( 42 | get = { laptop -> laptop.motherBoard }, 43 | set = { mb -> { laptop -> laptop.copy(motherBoard = mb) } } 44 | ) 45 | 46 | val motherBoardMemory: Lens = Lens( 47 | get = { mb -> mb.memory }, 48 | set = { memory -> { mb -> mb.copy(memory = memory) } } 49 | ) 50 | 51 | val memorySize: Lens = Lens( 52 | get = { memory -> memory.size }, 53 | set = { size -> { memory -> memory.copy(size = size) } } 54 | )*/ 55 | 56 | /* 57 | fun main(args: Array) { 58 | val laptopX8 = Laptop(500.0, MotherBoard("X", Memory(8))) 59 | 60 | val laptopMemorySize = laptopMotherBoard compose motherBoardMemory compose memorySize 61 | 62 | val laptopX16 = laptopMemorySize.modify(laptopPrice.set(laptopX8, 780.0)) { size -> 63 | size * 2 64 | } 65 | 66 | println("laptopX16 = $laptopX16") 67 | }*/ 68 | 69 | fun main(args: Array) { 70 | val laptopX8 = Laptop(500.0, MotherBoard("X", Memory(8))) 71 | 72 | val laptopMemorySize: Lens = laptopMotherBoard() compose motherBoardMemory() compose memorySize() 73 | 74 | val laptopX16 = laptopMemorySize.modify(laptopPrice().set(laptopX8, 780.0)) { size -> 75 | size * 2 76 | } 77 | 78 | println("laptopX16 = $laptopX16") 79 | } -------------------------------------------------------------------------------- /Chapter05/src/main/kotlin/com/packtpub/functionalkotlin/chapter05/parameters.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter05 2 | 3 | /** 4 | * Created by IntelliJ IDEA. 5 | * @author Mario Arias 6 | * Date: 10/11/17 7 | * Time: 2:29 AM 8 | */ 9 | 10 | fun basicFunction(name: String, size: Int) { 11 | 12 | } 13 | 14 | data class Person(val name: String) 15 | 16 | fun aVarargFun(vararg names: String) { 17 | names.forEach(::println) 18 | } 19 | 20 | fun high(f: (age:Int, name:String) -> Unit) { 21 | f(1, "Romeo") 22 | } 23 | 24 | /*fun multipleVarargs(vararg names: String, vararg sizes: Person) { 25 | // Error:(18, 43) Kotlin: Multiple vararg-parameters are prohibited 26 | }*/ 27 | 28 | fun paramAfterVararg(courseId: Int, vararg students: String, roomTemperature: Double) { 29 | 30 | } 31 | 32 | fun transform(vararg ts: T, f: (T) -> R): List = ts.map(f) 33 | 34 | fun emit(t: T, vararg listeners: (T) -> Unit) = listeners.forEach { listener -> 35 | listener(t) 36 | } 37 | 38 | fun item(price: Double, vat: Double, total: Double = price + vat) { 39 | 40 | } 41 | 42 | fun main(args: Array) { 43 | 44 | high{ q, w -> 45 | //do something 46 | } 47 | 48 | aVarargFun() 49 | aVarargFun("Angela", "Brenda", "Caroline") 50 | 51 | 52 | transform(1, 2, 3, 4) { i -> i.toString() } 53 | 54 | //emit(1){i -> println(i)} // Compilation error 55 | 56 | emit(1, ::println, { i -> println(i * 2) }) 57 | 58 | paramAfterVararg(68, "Abel", "Barbara", "Carl", "Diane", roomTemperature = 18.0) 59 | 60 | val customer1 = Customer("John", "Carl", "Doe", "XX234", 82.3, 180) 61 | 62 | val customer2 = Customer( 63 | lastName = "Doe", 64 | firstName = "John", 65 | middleName = "Carl", 66 | height = 180, 67 | weight = 82.3, 68 | passportNumber = "XX234") 69 | 70 | val programmer1 = Programmer("John", "Doe") 71 | 72 | // val programmer2 = Programmer("John", "Doe", 12) //Error 73 | 74 | val programmer2 = Programmer("John", "Doe", yearsOfExperience = 12) //OK 75 | 76 | val programmer3 = Programmer("John", "Doe", "TypeScript", 1) 77 | } 78 | 79 | data class Programmer(val firstName: String, 80 | val lastName: String, 81 | val favouriteLanguage: String = "Kotlin", 82 | val yearsOfExperience: Int = 0) 83 | 84 | typealias Kg = Double 85 | typealias cm = Int 86 | 87 | data class Customer(val firstName: String, 88 | val middleName: String, 89 | val lastName: String, 90 | val passportNumber: String, 91 | val weight: Kg, 92 | val height: cm) -------------------------------------------------------------------------------- /Chapter01/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /Chapter02/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /Chapter03/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /Chapter05/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /Chapter06/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /Chapter07/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /Chapter08/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /Chapter09/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /Chapter10/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /Chapter11/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /Chapter12/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /Chapter13/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /Chapter14/gradle/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /Appendix/Code/gradle/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /Chapter07/src/main/kotlin/com/packtpub/functionalkotlin/chapter07/select.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter07 2 | 3 | import kotlinx.coroutines.experimental.channels.ReceiveChannel 4 | import kotlinx.coroutines.experimental.channels.SendChannel 5 | import kotlinx.coroutines.experimental.channels.produce 6 | import kotlinx.coroutines.experimental.delay 7 | import kotlinx.coroutines.experimental.selects.select 8 | import kotlin.coroutines.experimental.CoroutineContext 9 | 10 | fun fizz(context: CoroutineContext) = produce(context) { 11 | while (true) { 12 | delay(300) 13 | send("Fizz") 14 | } 15 | } 16 | 17 | fun buzz(context: CoroutineContext) = produce(context) { 18 | while (true) { 19 | delay(500) 20 | send("Buzz") 21 | } 22 | } 23 | 24 | suspend fun selectFizzBuzz(fizz: ReceiveChannel, buzz: ReceiveChannel) { 25 | select { 26 | fizz.onReceive { 27 | log("fizz -> $it") 28 | } 29 | 30 | buzz.onReceive { 31 | log("buzz -> $it") 32 | } 33 | } 34 | } 35 | 36 | /*fun main(args: Array) = runBlocking { 37 | val fizz = fizz(coroutineContext) 38 | val buzz = buzz(coroutineContext) 39 | repeat(7) { 40 | selectFizzBuzz(fizz, buzz) 41 | } 42 | coroutineContext.cancelChildren() 43 | }*/ 44 | 45 | suspend fun selectAorB(a: ReceiveChannel, b: ReceiveChannel): String = select { 46 | fun receive(channel: ReceiveChannel, name: String) { 47 | channel.onReceiveOrNull { 48 | if (it == null) { 49 | "Channel '$name' is closed" 50 | } else { 51 | "$name -> $it" 52 | } 53 | } 54 | } 55 | 56 | receive(a, "a") 57 | receive(b, "b") 58 | } 59 | 60 | /*fun main(args: Array) = runBlocking { 61 | val a = produce(coroutineContext) { 62 | repeat(4) { send("Hello $it") } 63 | } 64 | 65 | val b = produce(coroutineContext) { 66 | repeat(4) { send("World $it") } 67 | } 68 | 69 | repeat(8){ 70 | log(selectAorB(a, b)) 71 | } 72 | coroutineContext.cancelChildren() 73 | }*/ 74 | 75 | fun produceNumbers(context: CoroutineContext, side:SendChannel) = produce(context) { 76 | for (i in 1..10) { 77 | delay(100) 78 | select { 79 | onSend(i){} 80 | side.onSend(i){} 81 | } 82 | } 83 | } 84 | 85 | /*fun main(args: Array) = runBlocking { 86 | val side = Channel() 87 | launch(coroutineContext) { 88 | side.consumeEach { log("Side channel has $it") } 89 | } 90 | produceNumbers(coroutineContext, side).consumeEach { 91 | log("Consuming $it") 92 | delay(250) 93 | } 94 | log("Done consuming") 95 | coroutineContext.cancelChildren() 96 | }*/ 97 | -------------------------------------------------------------------------------- /Chapter12/src/main/kotlin/com/packtpub/functionalkotlin/chapter12/memoization.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter12 2 | 3 | import arrow.syntax.function.memoize 4 | import kotlinx.coroutines.experimental.launch 5 | import kotlinx.coroutines.experimental.runBlocking 6 | import kotlin.system.measureNanoTime 7 | 8 | fun recursiveFib(n: Long): Long = if (n < 2) { 9 | n 10 | } else { 11 | recursiveFib(n - 1) + recursiveFib(n - 2) 12 | } 13 | 14 | fun imperativeFib(n: Long): Long { 15 | return when (n) { 16 | 0L -> 0 17 | 1L -> 1 18 | else -> { 19 | var a = 0L 20 | var b = 1L 21 | var c = 0L 22 | for (i in 2..n) { 23 | c = a + b 24 | a = b 25 | b = c 26 | } 27 | c 28 | } 29 | } 30 | } 31 | 32 | /*fun main(args: Array) { 33 | 34 | var lambdaFib: (Long) -> Long = { it } //Declared ahead to be used inside recursively 35 | 36 | lambdaFib = { n: Long -> 37 | if (n < 2) n else lambdaFib(n - 1) + lambdaFib(n - 2) 38 | } 39 | 40 | var memoizedFib: (Long) -> Long = { it } 41 | 42 | memoizedFib = { n: Long -> 43 | if (n < 2) n else memoizedFib(n - 1) + memoizedFib(n - 2) 44 | }.memoize() 45 | 46 | println(milliseconds("imperative fib") { imperativeFib(40) }) //0.006 47 | 48 | println(milliseconds("recursive fib") { recursiveFib(40) }) //1143.167 49 | println(milliseconds("lambda fib") { lambdaFib(40) }) //4324.890 50 | println(milliseconds("memoized fib") { memoizedFib(40) }) //1.588 51 | }*/ 52 | 53 | inline fun milliseconds(description: String, body: () -> Unit): String { 54 | return "$description:${measureNanoTime(body) / 1_000_000.00} ms" 55 | } 56 | 57 | fun main(args: Array) = runBlocking { 58 | 59 | var lambdaFib: (Long) -> Long = { it } //Declared ahead to be used inside recursively 60 | 61 | lambdaFib = { n: Long -> 62 | if (n < 2) n else lambdaFib(n - 1) + lambdaFib(n - 2) 63 | } 64 | 65 | var memoizedFib: (Long) -> Long = { it } 66 | 67 | memoizedFib = { n: Long -> 68 | println("from memoized fib n = $n") 69 | if (n < 2) n else memoizedFib(n - 1) + memoizedFib(n - 2) 70 | }.memoize() 71 | 72 | val job = launch { 73 | repeat(10) { i -> 74 | launch(coroutineContext) { println(milliseconds("On coroutine $i - imperative fib") { imperativeFib(40) }) } 75 | launch(coroutineContext) { println(milliseconds("On coroutine $i - recursive fib") { recursiveFib(40) }) } 76 | launch(coroutineContext) { println(milliseconds("On coroutine $i - lambda fib") { lambdaFib(40) }) } 77 | launch(coroutineContext) { println(milliseconds("On coroutine $i - memoized fib") { memoizedFib(40) }) } 78 | } 79 | } 80 | 81 | job.join() 82 | 83 | } 84 | 85 | -------------------------------------------------------------------------------- /Chapter02/src/main/kotlin/com/packtpub/functionalkotlin/chapter02/recursion.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter02 2 | 3 | /** 4 | * Created by IntelliJ IDEA. 5 | * @author Mario Arias 6 | * Date: 9/02/18 7 | * Time: 5:40 AM 8 | */ 9 | fun factorial(n: Long): Long { 10 | var result = 1L 11 | for (i in 1..n) { 12 | result *= i 13 | } 14 | return result 15 | } 16 | 17 | fun functionalFactorial(n: Long): Long { 18 | fun go(n: Long, acc: Long): Long { 19 | return if (n <= 0) { 20 | acc 21 | } else { 22 | go(n - 1, n * acc) 23 | } 24 | } 25 | 26 | return go(n, 1) 27 | } 28 | 29 | fun tailrecFactorial(n: Long): Long { 30 | tailrec fun go(n: Long, acc: Long): Long { 31 | return if (n <= 0) { 32 | acc 33 | } else { 34 | go(n - 1, n * acc) 35 | } 36 | } 37 | 38 | return go(n, 1) 39 | } 40 | 41 | fun fib(n: Long): Long { 42 | return when (n) { 43 | 0L -> 0 44 | 1L -> 1 45 | else -> { 46 | var a = 0L 47 | var b = 1L 48 | var c = 0L 49 | for (i in 2..n) { 50 | c = a + b 51 | a = b 52 | b = c 53 | } 54 | c 55 | } 56 | } 57 | } 58 | 59 | fun functionalFib(n: Long): Long { 60 | fun go(n: Long, prev: Long, cur: Long): Long { 61 | return if (n == 0L) { 62 | prev 63 | } else { 64 | go(n - 1, cur, prev + cur) 65 | } 66 | } 67 | 68 | return go(n, 0, 1) 69 | } 70 | 71 | fun tailrecFib(n: Long): Long { 72 | tailrec fun go(n: Long, prev: Long, cur: Long): Long { 73 | return if (n == 0L) { 74 | prev 75 | } else { 76 | go(n - 1, cur, prev + cur) 77 | } 78 | } 79 | 80 | return go(n, 0, 1) 81 | } 82 | 83 | fun executionTime(body: () -> Unit): Long { 84 | val startTime = System.nanoTime() 85 | body() 86 | val endTime = System.nanoTime() 87 | return endTime - startTime 88 | } 89 | 90 | /* 91 | fun main(args: Array) { 92 | println("factorial :" + executionTime { factorial(20) }) 93 | println("functionalFactorial :" + executionTime { functionalFactorial(20) }) 94 | println("tailrecFactorial :" + executionTime { tailrecFactorial(20) }) 95 | }*/ 96 | 97 | fun main(args: Array) { 98 | println("fib :" + executionTime { fib(93) }) 99 | println("functionalFib :" + executionTime { functionalFib(93) }) 100 | println("tailrecFib :" + executionTime { tailrecFib(93) }) 101 | } -------------------------------------------------------------------------------- /Chapter13/src/main/kotlin/com/packtpub/functionalkotlin/chapter13/either.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter13 2 | 3 | import arrow.core.* 4 | import arrow.core.Either.Left 5 | import arrow.core.Either.Right 6 | import arrow.syntax.option.toEither 7 | import arrow.typeclasses.binding 8 | 9 | 10 | fun randomlyEither(): Either = randomlyOption().toEither { "Nothing here" } 11 | 12 | /* 13 | fun main(args: Array) { 14 | val either = randomlyEither() 15 | when(either){ 16 | is Either.Right -> println("Int: ${either.b}") 17 | is Either.Left -> println(either.a) 18 | } 19 | }*/ 20 | 21 | /* 22 | fun main(args: Array) { 23 | randomlyEither().fold(::println,{i -> println("Int: $i")}) 24 | }*/ 25 | 26 | /* 27 | fun main(args: Array) { 28 | randomlyEither().map { i -> "Int: $i" }.fold(::println, ::println) 29 | }*/ 30 | 31 | /* 32 | fun main(args: Array) { 33 | randomlyEither().flatMap { i -> 34 | if (i > 0) { 35 | Right(i) 36 | } else { 37 | Left("Not a positive") 38 | } 39 | }.getOrElse { 0 } pipe ::println 40 | }*/ 41 | 42 | 43 | fun main(args: Array) { 44 | Either.monad().binding { 45 | val num1 = randomlyEither().bind() 46 | val num2 = randomlyEither().mapLeft { s -> s.toUpperCase() }.bind() 47 | num1 + num2 48 | }.ev().fold(::println, ::println) 49 | } 50 | 51 | fun eitherDivide(num: Int, den: Int): Either { 52 | val option = optionDivide(num, den) 53 | return when (option) { 54 | is Some -> Right(option.t) 55 | None -> Left("$num isn't divisible by $den") 56 | } 57 | } 58 | 59 | fun eitherDivision(a: Int, b: Int, den: Int): Either> { 60 | val aDiv = eitherDivide(a, den) 61 | return when (aDiv) { 62 | is Right -> { 63 | val bDiv = eitherDivide(b, den) 64 | when (bDiv) { 65 | is Right -> Right(aDiv.getOrElse { 0 } toT bDiv.getOrElse { 0 }) 66 | is Left -> bDiv as Either 67 | } 68 | } 69 | is Left -> aDiv as Either 70 | } 71 | } 72 | 73 | fun flatMapEitherDivision(a: Int, b: Int, den: Int): Either> { 74 | return eitherDivide(a, den).flatMap { aDiv -> 75 | eitherDivide(b, den).flatMap { bDiv -> 76 | Right(aDiv toT bDiv) 77 | } 78 | } 79 | } 80 | 81 | // 82 | fun comprehensionEitherDivision(a: Int, b: Int, den: Int): Either> { 83 | return Either.monad().binding { 84 | val aDiv = eitherDivide(a, den).bind() 85 | val bDiv = eitherDivide(b, den).bind() 86 | aDiv toT bDiv 87 | }.ev() 88 | } 89 | 90 | /*fun main(args: Array) { 91 | eitherDivision(3, 2, 4).fold(::println, ::println) 92 | }*/ 93 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /Chapter01/src/main/kotlin/com/packtpub/functionalkotlin/chapter01/absclasses.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter01 2 | 3 | /* 4 | fun main(args: Array) { 5 | val anyGood = BakeryGood("Generic flavour") 6 | }*/ 7 | 8 | /* 9 | abstract class BakeryGood(val flavour: String) { 10 | fun eat(): String { 11 | return "nom, nom, nom... delicious $flavour ${name()}" 12 | } 13 | 14 | open fun name(): String { 15 | return "bakery good" 16 | } 17 | }*/ 18 | 19 | /*abstract class BakeryGood(val flavour: String) { 20 | fun eat(): String { 21 | return "nom, nom, nom... delicious $flavour ${name()}" 22 | } 23 | 24 | abstract fun name(): String 25 | }*/ 26 | 27 | 28 | abstract class BakeryGood(val flavour: String) { 29 | 30 | init { 31 | println("Preparing a new bakery good") 32 | } 33 | 34 | fun eat(): String { 35 | return "nom, nom, nom... delicious $flavour ${name()}" 36 | } 37 | 38 | abstract fun name(): String 39 | } 40 | 41 | /*abstract class BakeryGood(val flavour: String) { 42 | fun eat(): String { 43 | return "nom, nom, nom... delicious $flavour ${name()}" 44 | } 45 | 46 | fun bake(): String { 47 | return "is hot here, isn't??" 48 | } 49 | 50 | abstract fun name(): String 51 | }*/ 52 | 53 | /*abstract class Bakeable { 54 | fun bake(): String { 55 | return "is hot here, isn't" 56 | } 57 | }*/ 58 | 59 | interface Bakeable { 60 | fun bake(): String { 61 | return "is hot here, isn't" 62 | } 63 | } 64 | 65 | class Customer(val name: String) { 66 | fun eats(food: BakeryGood) { 67 | println("$name is eating... ${food.eat()}") 68 | } 69 | } 70 | 71 | /*class Cupcake(flavour: String) : BakeryGood(flavour), Bakeable() { 72 | override fun name(): String { 73 | return "cupcake" 74 | } 75 | }*/ 76 | 77 | /*class Cupcake(flavour: String) : BakeryGood(flavour), Bakeable { 78 | override fun name(): String { 79 | return "cupcake" 80 | } 81 | }*/ 82 | 83 | /* 84 | fun main(args: Array) { 85 | val myDonut = Donut("Custard", "Powdered sugar") 86 | val mario = Customer("Mario") 87 | mario.eats(myDonut) 88 | }*/ 89 | 90 | fun main(args: Array) { 91 | val mario = Customer("Mario") 92 | 93 | val food: BakeryGood = object : BakeryGood("TEST_1") { 94 | override fun name(): String { 95 | return "TEST_2" 96 | } 97 | } 98 | 99 | mario.eats(food) 100 | } 101 | 102 | open class Donut(flavour: String, val topping: String) : BakeryGood(flavour), Fried { 103 | 104 | override fun fry(): String { 105 | return "*swimming on oil*" 106 | } 107 | 108 | override fun name(): String { 109 | return "donut with $topping topping" 110 | } 111 | } -------------------------------------------------------------------------------- /Chapter02/src/main/kotlin/com/packtpub/functionalkotlin/chapter02/structures.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter02 2 | 3 | 4 | /** 5 | * Created by IntelliJ IDEA. 6 | * @author Mario Arias 7 | * Date: 7/10/17 8 | * Time: 1:53 AM 9 | */ 10 | 11 | 12 | 13 | 14 | 15 | 16 | fun main(args: Array) { 17 | // println(capitalize("hello world!")) 18 | 19 | /*println(transform("kotlin", capitalize)) 20 | 21 | println(transform("kotlin", ::reverse)) 22 | 23 | val transformer = Transformer() 24 | 25 | println(transform("kotlin", transformer::upperCased)) 26 | 27 | println(transform("kotlin", Transformer.Companion::lowerCased)) 28 | 29 | println(transform("kotlin", MyUtils::doNothing)) 30 | 31 | println(transform("kotlin", { str -> str.substring(0..1) })) 32 | println(transform("kotlin", { it.substring(0..1) })) 33 | println(transform("kotlin") { str -> str.substring(0..1) }) 34 | 35 | 36 | val securityCheck = false 37 | 38 | unless(securityCheck) { 39 | println("You can't access this website") 40 | } 41 | 42 | 43 | useMachine(5, PrintMachine()) 44 | 45 | useMachine(5, ::println) 46 | 47 | useMachine(5) { i -> 48 | println(i) 49 | }*/ 50 | 51 | /*println("factorial :" + executionTime { factorial(20) }) 52 | println("functionalFactorial :" + executionTime { functionalFactorial(20) }) 53 | println("tailrecFactorial :" + executionTime { tailrecFactorial(20) })*/ 54 | 55 | /*println("fib :" + executionTime { fib(93) }) 56 | println("functionalFib :" + executionTime { functionalFib(93) }) 57 | println("tailrecFib :" + executionTime { tailrecFib(93) })*/ 58 | 59 | // val size = listOf(2 + 1, 3 * 2, 1 / 0, 5 - 4).size 60 | // val size = listOf({ 2 + 1 }, { 3 * 2 }, { 1 / 0 }, { 5 - 4 }).size 61 | // println(size) 62 | 63 | /*val a by lazy({ 2 + 1 }) 64 | val b by lazy({ 3 * 2 }) 65 | val c by lazy({ 1 / 0 }) 66 | val d by lazy({ 5 - 4 }) 67 | 68 | val size = listOf(a, b, c, d).size*/ 69 | 70 | /*val i by lazy { 71 | println("Lazy evaluation") 72 | 1 73 | } 74 | 75 | println("before using i") 76 | println(i)*/ 77 | 78 | /*fun f(x: Long) : Long { 79 | return x * x 80 | } 81 | 82 | var i = 0 83 | 84 | fun g(x: Long): Long { 85 | i++ 86 | return x * i // accessing mutable state 87 | } 88 | 89 | println(g(1)) //0 90 | i++ 91 | println(g(1)) //1 92 | i++ 93 | println(g(1)) //2*/ 94 | 95 | val numbers: List = listOf(1, 2, 3, 4) 96 | 97 | val sum = numbers.foldRight(0) { acc, i -> 98 | println("acc, i = $acc, $i") 99 | acc + i 100 | } 101 | 102 | println(sum) 103 | 104 | 105 | 106 | 107 | // val numbersTwice: List = numbers.map { i -> i * 2 } 108 | 109 | 110 | /*val numbersTwice: MutableList = mutableListOf() 111 | 112 | for (i in numbers) { 113 | numbersTwice.add(i * 2) 114 | }*/ 115 | 116 | 117 | /*for(i in numbers) { 118 | println("i = $i") 119 | } 120 | 121 | numbers.forEach { i -> println("i = $i") }*/ 122 | 123 | 124 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # Functional Kotlin 5 | This is the code repository for [Functional Kotlin](https://www.packtpub.com/application-development/functional-kotlin?utm_source=github&utm_medium=repository&utm_campaign=9781788476485), published by [Packt](https://www.packtpub.com/?utm_source=github). It contains all the supporting project files necessary to work through the book from start to finish. 6 | ## About the Book 7 | Functional programming makes your application faster, improves performance, and increases your productivity. Kotlin supports many of the popular and advanced functional features of functional languages. This book will cover the A-Z of functional programming in Kotlin. This book bridges the language gap for Kotlin developers by showing you how to create and consume functional constructs in Kotlin. We also bridge the domain gap by showing how functional constructs can be applied in business scenarios. We’ll take you through lambdas, pattern matching, immutability, and help you develop a deep understanding of the concepts and practices of functional programming. If you want learn to address problems using Recursion, Koltin has support for it as well. You’ll also learn how to use the funKtionale library to perform currying and lazy programming and more. Finally, you’ll learn functional design patterns and techniques that will make you a better programmer.By the end of the book, you will be more confident in your functional programming skills and will be able to apply them while programming in Kotlin. 8 | ## Instructions and Navigation 9 | All of the code is organized into folders. Each folder starts with a number followed by the application name. For example, Chapter02. 10 | 11 | All chapters have codes. 12 | 13 | The code will look like the following: 14 | ``` 15 | open class BakeryGood(val flavour: String) { 16 | fun eat(): String { 17 | return "nom, nom, nom... delicious $flavour bakery good" 18 | } 19 | } 20 | ``` 21 | 22 | The only recommended software to run and write Kotlin programs is IntelliJ IDEA. You can install IntelliJ IDEA on Windows, Mac, and Linux. 23 | 24 | ## Related Products 25 | * [Hands-on Design Patterns with Kotlin](https://www.packtpub.com/application-development/hands-design-patterns-kotlin?utm_source=github&utm_medium=repository&utm_campaign=9781788998017) 26 | 27 | * [Learning Concurrency in Kotlin](https://www.packtpub.com/application-development/learning-concurrency-kotlin?utm_source=github&utm_medium=repository&utm_campaign=9781788627160) 28 | 29 | * [Mastering High Performance with Kotlin](https://www.packtpub.com/application-development/mastering-high-performance-kotlin?utm_source=github&utm_medium=repository&utm_campaign=9781788996648) 30 | 31 | ### Download a free PDF 32 | 33 | If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to claim your free PDF.
34 |
-------------------------------------------------------------------------------- /Chapter01/src/main/kotlin/com/packtpub/functionalkotlin/chapter01/objects.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter01 2 | 3 | /* 4 | fun main(args: Array) { 5 | val expression = object { 6 | val property = "" 7 | 8 | fun method(): Int { 9 | println("from an object expressions") 10 | return 42 11 | } 12 | } 13 | 14 | val i = "${expression.method()} ${expression.property}" 15 | println(i) 16 | }*/ 17 | 18 | /* 19 | class Outer { 20 | val internal = object { 21 | val property = "" 22 | } 23 | } 24 | 25 | fun main(args: Array) { 26 | val outer = Outer() 27 | 28 | println(outer.internal.property) // Compilation error: Unresolved reference: property 29 | }*/ 30 | 31 | /* 32 | object Oven { 33 | fun process(product: Bakeable) { 34 | println(product.bake()) 35 | } 36 | } 37 | 38 | fun main(args: Array) { 39 | val myAlmondCupcake = Cupcake("Almond") 40 | Oven.process(myAlmondCupcake) 41 | }*/ 42 | 43 | /*interface Oven { 44 | fun process(product: Bakeable) 45 | } 46 | 47 | object ElectricOven: Oven { 48 | override fun process(product: Bakeable) { 49 | println(product.bake()) 50 | } 51 | } 52 | 53 | fun main(args: Array) { 54 | val myAlmondCupcake = Cupcake("Almond") 55 | ElectricOven.process(myAlmondCupcake) 56 | }*/ 57 | 58 | /*class Cupcake(flavour: String) : BakeryGood(flavour), Bakeable { 59 | override fun name(): String { 60 | return "cupcake" 61 | } 62 | 63 | companion object { 64 | fun almond(): Cupcake { 65 | return Cupcake("almond") 66 | } 67 | 68 | fun cheese(): Cupcake { 69 | return Cupcake("cheese") 70 | } 71 | } 72 | }*/ 73 | 74 | class Cupcake(flavour: String) : BakeryGood(flavour), Bakeable { 75 | override fun name(): String { 76 | return "cupcake" 77 | } 78 | 79 | companion object Factory { 80 | fun almond(): Cupcake { 81 | return Cupcake("almond") 82 | } 83 | 84 | fun cheese(): Cupcake { 85 | return Cupcake("cheese") 86 | } 87 | } 88 | } 89 | 90 | /* 91 | fun main(args: Array) { 92 | val myBlueberryCupcake: BakeryGood = Cupcake("Blueberry") 93 | val myAlmondCupcake = Cupcake.almond() 94 | val myCheeseCupcake = Cupcake.cheese() 95 | val myCaramelCupcake = Cupcake("Caramel") 96 | }*/ 97 | 98 | /* 99 | fun main(args: Array) { 100 | val myAlmondCupcake = Cupcake.almond() 101 | val myCheeseCupcake = myAlmondCupcake.cheese() //Compilation error: Unresolved reference: cheese 102 | }*/ 103 | 104 | /* 105 | fun main(args: Array) { 106 | val factory: Cupcake.Companion = Cupcake.Companion 107 | }*/ 108 | 109 | /* 110 | fun main(args: Array) { 111 | val factory: Cupcake.Factory = Cupcake.Factory 112 | }*/ 113 | 114 | fun main(args: Array) { 115 | val factory: Cupcake.Factory = Cupcake 116 | } -------------------------------------------------------------------------------- /Chapter07/src/main/kotlin/com/packtpub/functionalkotlin/chapter07/basics.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter07 2 | 3 | import kotlinx.coroutines.experimental.delay 4 | import kotlinx.coroutines.experimental.launch 5 | import kotlinx.coroutines.experimental.runBlocking 6 | 7 | /* 8 | fun main(args: Array) { 9 | launch { 10 | delay(1000L) 11 | println("World!") 12 | } 13 | println("Hello, ") 14 | Thread.sleep(2000L) 15 | } 16 | */ 17 | 18 | /*fun main(args: Array) { 19 | launch{ 20 | delay(1000) 21 | println("World!") 22 | } 23 | print("Hello, ") 24 | runBlocking { 25 | delay(2000) 26 | } 27 | }*/ 28 | 29 | /*fun main(args: Array) = runBlocking { 30 | launch { 31 | delay(1000) 32 | println("World!") 33 | } 34 | print("Hello, ") 35 | 36 | delay(2000) 37 | }*/ 38 | 39 | /* 40 | fun main(args: Array) = runBlocking { 41 | val job = launch { 42 | doWorld() 43 | } 44 | print("Hello, ") 45 | job.join() 46 | } 47 | 48 | private suspend fun doWorld() { 49 | delay(1000) 50 | println("World!") 51 | }*/ 52 | 53 | fun main(args: Array) = runBlocking { 54 | val jobs = List(100_000) { 55 | launch { 56 | delay(1000) 57 | print(".") 58 | } 59 | } 60 | 61 | jobs.forEach { it.join() } 62 | } 63 | 64 | /* 65 | fun main(args: Array) = runBlocking{ 66 | launch { 67 | repeat(1000){ i -> 68 | println("I'm sleeping $i...") 69 | delay(500) 70 | } 71 | } 72 | delay(1300) 73 | }*/ 74 | 75 | /*fun main(args: Array) { 76 | thread { 77 | Thread.sleep(1000) 78 | println("World!") 79 | } 80 | print("Hello ") 81 | Thread.sleep(2000) 82 | }*/ 83 | 84 | /*fun main(args: Array) { 85 | val computation = thread { 86 | Thread.sleep(1000) 87 | println("World!") 88 | } 89 | print("Hello ") 90 | computation.join() 91 | }*/ 92 | 93 | /*fun main(args: Array) = awaitEnter{ 94 | pauseUntilEnter() 95 | val threads = List(1900){ 96 | thread { 97 | Thread.sleep(1000) 98 | print('.') 99 | } 100 | } 101 | threads.forEach(Thread::join) 102 | }*/ 103 | 104 | /*fun main(args: Array) = awaitEnter{ 105 | pauseUntilEnter() 106 | val pool = Executors.newFixedThreadPool(1024) 107 | repeat(10000){ 108 | pool.submit { 109 | Thread.sleep(1000) 110 | print('.') 111 | } 112 | } 113 | pool.shutdown() 114 | pool.awaitTermination(2, TimeUnit.SECONDS) 115 | }*/ 116 | 117 | /*fun main(args: Array) = runBlocking { 118 | launch { 119 | delay(1000) 120 | println("World") 121 | } 122 | print("Hello ") 123 | delay(2000) 124 | }*/ 125 | 126 | /*fun main(args: Array) = runBlocking { 127 | val job = launch { 128 | delay(1000) 129 | println("World") 130 | } 131 | print("Hello ") 132 | job.join() 133 | }*/ 134 | 135 | /*fun main(args: Array) = awaitEnter { 136 | pauseUntilEnter() 137 | runBlocking { 138 | val jobs = List(1_000_000) { 139 | launch { 140 | delay(1000) 141 | print('.') 142 | } 143 | } 144 | jobs.forEach { job -> job.join() } 145 | } 146 | }*/ 147 | 148 | 149 | 150 | 151 | -------------------------------------------------------------------------------- /Chapter02/src/main/kotlin/com/packtpub/functionalkotlin/chapter02/high.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter02 2 | 3 | /** 4 | * Created by IntelliJ IDEA. 5 | * @author Mario Arias 6 | * Date: 9/02/18 7 | * Time: 5:03 AM 8 | */ 9 | val capitalize = { str: String -> str.capitalize() } 10 | /*val capitalize = object : Function1 { 11 | override fun invoke(p1: String): String { 12 | return p1.capitalize() 13 | } 14 | }*/ 15 | 16 | fun transform(t: T, fn: (T) -> T): T { 17 | return fn(t) 18 | } 19 | 20 | fun reverse(str: String): String { 21 | return str.reversed() 22 | } 23 | 24 | class Transformer { 25 | fun upperCased(str: String): String { 26 | return str.toUpperCase() 27 | } 28 | 29 | companion object { 30 | fun lowerCased(str: String): String { 31 | return str.toLowerCase() 32 | } 33 | } 34 | } 35 | 36 | object MyUtils { 37 | fun doNothing(str: String): String { 38 | return str 39 | } 40 | } 41 | 42 | fun unless(condition: Boolean, block: () -> Unit) { 43 | if (!condition) block() 44 | } 45 | 46 | /*interface Machine { 47 | fun process(t: T) 48 | } 49 | 50 | fun useMachine(t: T, machine: Machine) { 51 | machine.process(t) 52 | } 53 | 54 | class PrintMachine : Machine { 55 | override fun process(t: T) { 56 | println(t) 57 | } 58 | }*/ 59 | 60 | 61 | typealias Machine = (T) -> Unit 62 | 63 | fun useMachine(t: T, machine: Machine) { 64 | machine(t) 65 | } 66 | 67 | class PrintMachine : Machine { 68 | override fun invoke(p1: T) { 69 | println(p1) 70 | } 71 | } 72 | 73 | /* 74 | fun main(args: Array) { 75 | println(transform("kotlin", capitalize)) 76 | }*/ 77 | 78 | /* 79 | fun main(args: Array) { 80 | println(transform("kotlin", ::reverse)) 81 | }*/ 82 | 83 | 84 | /* 85 | fun main(args: Array) { 86 | println(transform("kotlin", MyUtils::doNothing)) 87 | }*/ 88 | 89 | /* 90 | fun main(args: Array) { 91 | val transformer = Transformer() 92 | 93 | println(transform("kotlin", transformer::upperCased)) 94 | 95 | println(transform("kotlin", Transformer.Companion::lowerCased)) 96 | }*/ 97 | 98 | /*fun main(args: Array) { 99 | println(transform("kotlin", { str -> str.substring(0..1) })) 100 | }*/ 101 | 102 | /*fun main(args: Array) { 103 | println(transform("kotlin", { it.substring(0..1) })) 104 | }*/ 105 | 106 | /*fun main(args: Array) { 107 | println(transform("kotlin") { str -> str.substring(0..1) }) 108 | }*/ 109 | 110 | /*fun main(args: Array) { 111 | val securityCheck = false // some interesting code here 112 | 113 | unless(securityCheck) { 114 | println("You can't access this website") 115 | } 116 | }*/ 117 | 118 | 119 | /* 120 | fun main(args: Array) { 121 | useMachine(5, PrintMachine()) 122 | 123 | useMachine(5, object : Machine { 124 | override fun process(t: Int) { 125 | println(t) 126 | } 127 | }) 128 | }*/ 129 | 130 | fun main(args: Array) { 131 | useMachine(5, PrintMachine()) 132 | 133 | useMachine(5, ::println) 134 | 135 | useMachine(5) { i -> 136 | println(i) 137 | } 138 | } -------------------------------------------------------------------------------- /Chapter02/src/main/kotlin/com/packtpub/functionalkotlin/chapter02/list.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter02 2 | 3 | import com.packtpub.functionalkotlin.chapter02.FunList.Cons 4 | import com.packtpub.functionalkotlin.chapter02.FunList.Nil 5 | 6 | /** 7 | * Created by IntelliJ IDEA. 8 | * @author Mario Arias 9 | * Date: 5/11/17 10 | * Time: 2:21 AM 11 | */ 12 | 13 | sealed class FunList { 14 | object Nil : FunList() 15 | 16 | data class Cons(val head: T, val tail: FunList) : FunList() 17 | 18 | fun forEach(f: (T) -> Unit) { 19 | tailrec fun go(list: FunList, f: (T) -> Unit) { 20 | when (list) { 21 | is Cons -> { 22 | f(list.head) 23 | go(list.tail, f) 24 | } 25 | is Nil -> Unit//Do nothing 26 | } 27 | } 28 | 29 | go(this, f) 30 | } 31 | 32 | fun fold(init: R, f: (R, T) -> R): R { 33 | tailrec fun go(list: FunList, init: R, f: (R, T) -> R): R = when (list) { 34 | is Cons -> go(list.tail, f(init, list.head), f) 35 | is Nil -> init 36 | } 37 | 38 | return go(this, init, f) 39 | } 40 | 41 | fun reverse(): FunList = fold(Nil as FunList) { acc, i -> Cons(i, acc) } 42 | 43 | fun foldRight(init: R, f: (R, T) -> R): R = this.reverse().fold(init, f) 44 | 45 | fun map(f: (T) -> R): FunList = foldRight(Nil as FunList) { tail, head -> Cons(f(head), tail) } 46 | 47 | } 48 | 49 | fun intListOf(vararg numbers: Int): FunList { 50 | return if (numbers.isEmpty()) { 51 | Nil 52 | } else { 53 | Cons(numbers.first(), intListOf(*numbers.drop(1).toTypedArray().toIntArray())) 54 | } 55 | } 56 | 57 | /* 58 | fun main(args: Array) { 59 | 60 | // val numbers = Cons(1, Cons(2, Cons(3, Cons(4, Nil)))) 61 | 62 | val numbers = intListOf(1, 2, 3, 4) 63 | 64 | //println(numbers.reverse()) 65 | 66 | */ 67 | /*numbers.forEach { i -> println("i = $i") }*//* 68 | 69 | 70 | 71 | 72 | numbers.map { i -> i * 2 }.forEach(::println) 73 | 74 | val i = listOf(1, 1.0) 75 | 76 | */ 77 | /*val funList = intListOf(1, 2, 3, 4) 78 | val list = listOf(1, 2, 3, 4) 79 | 80 | println("fold on funList : ${executionTime { funList.fold(0) { acc, i -> acc + i } }}") 81 | println("fold on list : ${executionTime { list.fold(0) { acc, i -> acc + i } }}")*//* 82 | 83 | }*/ 84 | 85 | /* 86 | fun main(args: Array) { 87 | val numbers = Cons(1, Cons(2, Cons(3, Cons(4, Nil)))) 88 | }*/ 89 | 90 | /* 91 | fun main(args: Array) { 92 | val numbers = intListOf(1, 2, 3, 4) 93 | }*/ 94 | 95 | /* 96 | fun main(args: Array) { 97 | val numbers = intListOf(1, 2, 3, 4) 98 | 99 | numbers.forEach { i -> println("i = $i") } 100 | }*/ 101 | 102 | /* 103 | fun main(args: Array) { 104 | val numbers = intListOf(1, 2, 3, 4) 105 | 106 | val sum = numbers.fold(0) { acc, i -> acc + i} 107 | }*/ 108 | 109 | /* 110 | fun main(args: Array) { 111 | val funList = intListOf(1, 2, 3, 4) 112 | val list = listOf(1, 2, 3, 4) 113 | 114 | println("fold on funList : ${executionTime { funList.fold(0) { acc, i -> acc + i } }}") 115 | println("fold on list : ${executionTime { list.fold(0) { acc, i -> acc + i } }}") 116 | }*/ 117 | -------------------------------------------------------------------------------- /Chapter13/src/main/kotlin/com/packtpub/functionalkotlin/chapter13/state.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter13 2 | 3 | import arrow.core.None 4 | import arrow.core.Option 5 | import arrow.core.Tuple2 6 | import arrow.core.toT 7 | import arrow.data.State 8 | import arrow.data.ev 9 | import arrow.data.run 10 | import arrow.data.runA 11 | import arrow.instances.monad 12 | import arrow.syntax.option.some 13 | import arrow.syntax.tuples.plus 14 | import arrow.typeclasses.binding 15 | 16 | typealias PriceLog = MutableList> 17 | 18 | fun addVat(): State = State { log: PriceLog -> 19 | val (_, price) = log.last() 20 | val vat = price * 0.2 21 | log.add("Add VAT: $vat" toT price + vat) 22 | log toT Unit 23 | } 24 | 25 | fun applyDiscount(threshold: Double, discount: Double): State = State { log -> 26 | val (_, price) = log.last() 27 | if (price > threshold) { 28 | log.add("Applying -$discount" toT price - discount) 29 | } else { 30 | log.add("No discount applied" toT price) 31 | } 32 | log toT Unit 33 | } 34 | 35 | fun finalPrice(): State = State { log -> 36 | val (_, price) = log.last() 37 | log.add("Final Price" toT price) 38 | log toT price 39 | } 40 | 41 | 42 | fun calculatePrice(threshold: Double, discount: Double) = State().monad().binding { 43 | addVat().bind() //Unit 44 | applyDiscount(threshold, discount).bind() //Unit 45 | val price: Double = finalPrice().bind() 46 | price 47 | }.ev() 48 | 49 | 50 | fun main(args: Array) { 51 | val (history: PriceLog, price: Double) = calculatePrice(100.0, 2.0).run(mutableListOf("Init" toT 15.0)) 52 | println("Price: $price") 53 | println("::History::") 54 | history 55 | .map { (text, value) -> "$text\t|\t$value" } 56 | .forEach(::println) 57 | 58 | val bigPrice: Double = calculatePrice(100.0, 2.0).runA(mutableListOf("Init" toT 1000.0)) 59 | println("bigPrice = $bigPrice") 60 | } 61 | 62 | 63 | fun unfold(s: S, state: State>): Sequence { 64 | val (actualState: S, value: Option) = state.run(s) 65 | return value.fold( 66 | { sequenceOf() }, 67 | { t -> 68 | sequenceOf(t) + unfold(actualState, state) 69 | }) 70 | } 71 | 72 | fun elements(element: T, size: Int): Sequence { 73 | return unfold(1, State { i -> 74 | if (size > i) { 75 | (i + 1) toT element.some() 76 | } else { 77 | 0 toT None 78 | } 79 | }) 80 | } 81 | 82 | fun factorial(size: Int): Sequence { 83 | return sequenceOf(1L) + unfold(1L toT 1, State { (acc, n) -> 84 | if (size > n) { 85 | val x = n * acc 86 | (x toT n + 1) toT x.some() 87 | } else { 88 | (0L toT 0) toT None 89 | } 90 | }) 91 | } 92 | 93 | fun fib(size: Int): Sequence { 94 | return sequenceOf(1L) + unfold((0L toT 1L) + 1, State { (cur, next, n) -> 95 | if (size > n) { 96 | val x = cur + next 97 | ((next toT x) + (n + 1)) toT x.some() 98 | } else { 99 | ((0L toT 0L) + 0) toT None 100 | } 101 | }) 102 | } 103 | 104 | 105 | /* 106 | fun main(args: Array) { 107 | factorial(10).forEach(::println) 108 | fib(10).forEach(::println) 109 | }*/ 110 | -------------------------------------------------------------------------------- /Chapter13/src/main/kotlin/com/packtpub/functionalkotlin/chapter13/tryy.kt: -------------------------------------------------------------------------------- 1 | package com.packtpub.functionalkotlin.chapter13 2 | 3 | import arrow.core.Tuple2 4 | import arrow.core.toT 5 | import arrow.data.* 6 | import arrow.data.Try.Success 7 | import arrow.typeclasses.binding 8 | import arrow.typeclasses.bindingCatch 9 | import java.lang.IllegalArgumentException 10 | import java.util.* 11 | 12 | /*fun main(args: Array) { 13 | try { 14 | val order = OrderService.getOrder("foo") 15 | println("order = $order") 16 | } catch (e: IllegalArgumentException) { 17 | println("Exception reading order :${e.message}") 18 | } 19 | }*/ 20 | 21 | /*fun main(args: Array) { 22 | Try { OrderService.getOrder("foo") } 23 | .getOrDefault { Order("", 0.0) } pipe ::println 24 | }*/ 25 | 26 | /*fun main(args: Array) { 27 | Try { OrderService.getOrder("foo") } 28 | .getOrElse { e: Throwable -> Order(e.message!!, 0.0) } pipe ::println 29 | }*/ 30 | 31 | 32 | /*fun main(args: Array) { 33 | Try { OrderService.getOrder("foo") }.filter { order -> order.price > 10 } pipe ::println 34 | }*/ 35 | 36 | /*fun main(args: Array) { 37 | Try { OrderService.getOrder("foo")}.recover { e -> e.message } pipe ::println 38 | }*/ 39 | 40 | /*fun main(args: Array) { 41 | Try { OrderService.getOrder("foo")}.map { order -> order.price } pipe ::println 42 | }*/ 43 | 44 | 45 | fun main(args: Array) { 46 | val totalPrice = Try.monad().binding { 47 | val fooPrice = Try { OrderService.getOrder("foo")}.map { order -> order.price }.bind() 48 | val bar = Try { OrderService.getOrder("bar") }.bind() 49 | fooPrice + bar.price 50 | 51 | }.ev() 52 | 53 | println(totalPrice.getOrDefault { -1 }) 54 | } 55 | 56 | /*fun main(args: Array) { 57 | val totalPrice = Try.monadError().bindingCatch{ 58 | val fooPrice = OrderService.getOrder("foo").price 59 | val bar = OrderService.getOrder("bar") 60 | fooPrice + bar.price 61 | }.ev() 62 | // 63 | println(totalPrice.getOrDefault { -1 }) 64 | }*/ 65 | 66 | data class Order(val serial: String, val price: Double) 67 | 68 | object OrderService { 69 | private val random = Random() 70 | 71 | @Throws(IllegalArgumentException::class) 72 | fun getOrder(serial: String): Order { 73 | if (random.change(50)) { 74 | return Order(serial, 9.0) 75 | } else { 76 | throw IllegalArgumentException("Invalid serial:$serial") 77 | } 78 | } 79 | } 80 | 81 | 82 | fun tryDivide(num: Int, den: Int): Try = Try { divide(num, den)!! } 83 | 84 | fun tryDivision(a: Int, b: Int, den: Int): Try> { 85 | val aDiv = tryDivide(a, den) 86 | return when (aDiv) { 87 | is Success -> { 88 | val bDiv = tryDivide(b, den) 89 | when (bDiv) { 90 | is Success -> { 91 | Try { aDiv.value toT bDiv.value } 92 | } 93 | 94 | is Failure -> Failure(bDiv.exception) 95 | } 96 | } 97 | is Failure -> Failure(aDiv.exception) 98 | } 99 | } 100 | 101 | fun flatMapTryDivision(a: Int, b: Int, den: Int): Try> { 102 | return tryDivide(a, den).flatMap { aDiv -> 103 | tryDivide(b, den).flatMap { bDiv -> 104 | Try { aDiv toT bDiv } 105 | } 106 | } 107 | } 108 | 109 | fun comprehensionTryDivision(a: Int, b: Int, den: Int): Try> { 110 | return Try.monad().binding { 111 | val aDiv = tryDivide(a, den).bind() 112 | val bDiv = tryDivide(b, den).bind() 113 | aDiv toT bDiv 114 | }.ev() 115 | } 116 | 117 | 118 | fun monadErrorTryDivision(a: Int, b: Int, den: Int): Try> { 119 | return Try.monadError().bindingCatch { 120 | val aDiv = divide(a, den)!! 121 | val bDiv = divide(b, den)!! 122 | aDiv toT bDiv 123 | }.ev() 124 | } 125 | 126 | --------------------------------------------------------------------------------

https://packt.link/free-ebook/9781788476485