Table of Contents

Setup

Platform: macOS Mojave, Windows 10

This notebook uses JetBrain's Kotlin kernel for IPython/Jupyter, which requires Java 8 to be installed

Created a new conda environment and installed the following:

conda create -n kotlin-jupyter
conda activate kotlin-jupyter
conda install -c conda-forge python=3.7
conda install -c jetbrains kotlin-jupyter-kernel
conda install openjdk=8
conda install -c conda-forge jupyter

P.S. On macOS Mojave, also installed Apache Maven and Gradle for a different purpose (i.e., not needed to run this notebook)

Apache Maven and Gradle are both dependency management and Build Automation Tools, primarily used for Java applications. Maven build files are written in XML (called pom.xml) whereas Gradle doesn't use XML (Gradle's configuration file is called build.gradle in Groovy, or build.gradle.kts in Kotlin). Comparison between Maven and Gradle is discussed here

conda install -c conda-forge maven conda install -c conda-forge gradle

Hello World!

Concatenation

Nullable Type

Variables in Kotlin can't be assigned a null value by default. That means, no more NullPointerException errors as in Java. So, can't do this in Kotlin:

var regular: String = null

Conditionals

when
if ... else if ... else

Loops

for 
while
break ... continue

Function

Lambdas

Extension function

In Kotlin, we can use extension function to extend a class with new functionality (no need to create a derived class-- note that in other object-oriented languages, a new functionality is added by deriving a new class from an existing class and then adding in the functionality). So, an extension function is a member function of a class that is defined outside the class.

Collections (Array, List, ArrayList)

Array manipulation: map function

Object-oriented programming

Example of a Class

Class with named properties

Class with default values for parameters

Overloading the constructor:

Open Class

Abstract Class

Interfaces

Configuring Maven dependencies

Load additional dependencies to the notebook from Maven repos as follows:

@file:Repository("https://repo1.maven.org/maven2")
@file:DependsOn("org.nield:kotlin-statistics:1.2.1")

Then, import as:

import org.nield.kotlinstatistics.*

Load pre-configured libraries using magics

The Kotlin kernel pre-configures certain libraries, and allows the notebook user to load them via special commands, also known as magics. To pre-configure libraries for a notebook, one must comma-separate their names prepened with %use. Here's how it works:

lets-plot: Open-source plotting library for statistical data. It is inspired by Python's ggplot and The Grammar of Graphics; this library is integrated tightly with the Kotlin kernel; the library is multi-platform and can be used not just with JVM but also from JS and Python.

kmath: Could be pronounced as key-math. The Kotlin MATHematics library is intended as a Kotlin-based analog to Python's numpy library. In contrast to numpy and scipy it is modular and has a lightweight core. This library provides functionality for data manipulation using a functional-style API; it allows to filter, transform, aggregate and reshape tabular data.