Kotlin Cookbook code examples ⬆
![]() |
Directory kotlin-cookbook\ contains Kotlin code examples from Ken Kousen's book "Koltin Cookbook" (O'Reilly, 2019).It also includes several build scripts (batch files, Gradle scripts) for experimenting with Kotlin on a Windows machine. |
Lazy loading can be implemented the hard way (with a private nullable property) or using a built-in lazy
delegate function.
This example has the following directory structure :
> tree /f /a . | findstr /v /b [A-Z] | build.bat | build.gradle | gradle.properties | Makefile | pom.xml \---src +---main | \---kotlin | Customer.kt \---test \---kotlin CustomerKtTest.kt
Command build.bat clean run
compiles the source files Customer.kt
and executes the generated Java class files :
> cd K:\kotlin-cookbook\Example_03-10 > build clean run Customer name: Fred
Command build -verbose test
compiles the source file CustomerKtTest.kt
and executes the implemented unit tests :
> build -verbose test No action required ('src\main\kotlin\*.kt') Compile 1 Kotlin test source files (JVM) to directory "target\test-classes" Execute test CustomerKtTestKt . +-- JUnit Jupiter [OK] | '-- CustomerKtTest [OK] | '-- load messages() [OK] '-- JUnit Vintage [OK] Test run finished after 85 ms [ 3 containers found ] [ 0 containers skipped ] [ 3 containers started ] [ 0 containers aborted ] [ 3 containers successful ] [ 0 containers failed ] [ 1 tests found ] [ 0 tests skipped ] [ 1 tests started ] [ 0 tests aborted ] [ 1 tests successful ] [ 0 tests failed ]
This example has the following directory structure :
> tree /f /a . | findstr /v /b [A-Z] | build.bat | build.gradle | gradle.properties | Makefile | pom.xml \---src +---main | \---kotlin | PointMain.kt \---test \---kotlin PointMainKtTest.kt
Command build.bat clean run
compiles the source files PointMain.kt
and executes the generated Java class files:
> cd K:\kotlin-cookbook\Example_03-13 > build clean run Point(x=-10, y=-20)
Command build -verbose test
compiles the source file PointMainKtTest.kt
and executes the implemented unit tests.
> build -verbose test No action required ('src\main\kotlin\*.kt') Compile 1 Kotlin test source files (JVM) to directory "target\test-classes" Execute test MainKtTest . +-- JUnit Jupiter [OK] | '-- MainKtTest [OK] | '-- point with negative axes$main() [OK] '-- JUnit Vintage [OK] Test run finished after 70 ms [ 3 containers found ] [ 0 containers skipped ] [ 3 containers started ] [ 0 containers aborted ] [ 3 containers successful ] [ 0 containers failed ] [ 1 tests found ] [ 0 tests skipped ] [ 1 tests started ] [ 0 tests aborted ] [ 1 tests successful ] [ 0 tests failed ]
Command build.bat clean run
compiles the source files ComplexOverloadOperators.kt
and executes the generated Java class files:
> cd K:\kotlin-cookbook\Example_03-14 > build clean run x = (1.0, 3.0) y = (2.0, 5.0) x+y = (3.0, 8.0) x*y = (-13.0, 11.0)
Command build.bat -verbose test
compiles the source file ComplexOverloadOperatorsKtTest.kt
and executes the implemented unit tests.
> build -verbose test No action required ('src\main\kotlin\*.kt') Compile 1 Kotlin test source files (JVM) to directory "target\test-classes" Execute test ComplexOverloadOperatorsKtTest . +-- JUnit Jupiter [OK] | '-- ComplexOverloadOperatorsKtTest [OK] | +-- Euler's formula$main() [OK] | +-- plus$main() [OK] | +-- negate$main() [OK] | '-- minus$main() [OK] '-- JUnit Vintage [OK] Test run finished after 99 ms [ 3 containers found ] [ 0 containers skipped ] [ 3 containers started ] [ 0 containers aborted ] [ 3 containers successful ] [ 0 containers failed ] [ 4 tests found ] [ 0 tests skipped ] [ 4 tests started ] [ 0 tests aborted ] [ 4 tests successful ] [ 0 tests failed ]
Command build.bat clean run
compiles the source files FoldMain.kt
and executes the generated Java class files:
> cd K:\kotlin-cookbook\Example_04-01 > build clean run nums = [1, 3, 5, 7] sum(*nums) = 16 acc = 0, n = 1 acc = 1, n = 3 acc = 4, n = 5 acc = 9, n = 7 sumWithTrace(*nums) = 16 factorialFold(20) = 2432902008176640000 fibonacciFold(20) = 6765
Command build.bat -verbose test
compiles the source file FoldMainKtTest.kt
and executes the implemented unit tests.
> build -verbose test No action required ('src\main\kotlin\*.kt') Compile 1 Kotlin test source files (JVM) to directory "target\test-classes" Execute test FoldMainKtTest . +-- JUnit Jupiter [OK] | '-- FoldMainKtTest [OK] | '-- sum using fold$main() [OK] '-- JUnit Vintage [OK] Test run finished after 100 ms [ 3 containers found ] [ 0 containers skipped ] [ 3 containers started ] [ 0 containers aborted ] [ 3 containers successful ] [ 0 containers failed ] [ 1 tests found ] [ 0 tests skipped ] [ 1 tests started ] [ 0 tests aborted ] [ 1 tests successful ] [ 0 tests failed ] build -verbose test