the kotlin programming language

How to create ArrayList instances in Kotlin while explicitly assigning their types alvin February 5, 2019 - 7:50pm

As a little note to self, here are some examples of how to create ArrayList instances in Kotlin while explicitly assigning their types:

Examples of how to use 'let' in Kotlin alvin February 5, 2019 - 7:45pm

As a little note to self, here are a few let examples in Kotlin:

fun main(args: Array<String>) {

    fun toUpper(s: String?): String? = s?.toUpperCase()

    toUpper(null)?.let {
        // this will never be run
        println("toUpper(null) is: ${it}")

    toUpper("emily")?.let {
        println("toUpper(emily) is: ${it}")

    // `let` can return a value
    val hello = toUpper("Hannah")?.let {
        "Hello, ${it}"


The output looks like this:

A Kotlin Adler-32 checksum algorithm alvin February 5, 2019 - 6:18pm

As a short post today, here’s an example of a Kotlin implementation of the Adler-32 checksum algorithm:

“Kotlin Quick Reference” book

Somewhere around a year ago I started working on a Kotlin programming book, but then I had to get away from it to work on other things. When I got back to it recently I looked around and felt like the world didn’t need another “Introduction to Kotlin” book — there are a couple of good ones out there, including Kotlin in Action, and the kotlinlang.org documentation is excellent — so I decided to ditch the project completely.

Kotlin: A for loop that counts up to some maximum integer value alvin December 1, 2018 - 3:05pm

As a brief note to self, I was just converting some Java code to Kotlin, and the correct way to convert this Java for loop that uses i as a counter:

for (int i=0; i<tabLayout.tabCount; i++) { ...

is with this Kotlin for loop:

for (i in 0 until tabLayout.tabCount) { ...

The key there for me is the 0 until part of the syntax.

How to generate a small random number in Kotlin

If you need to generate a small random number in Kotlin, I find that this approach works:

val r = (1..10).shuffled().first()

That code generates a single random number in the range from 1 to 10 (including 1 on the low end and 10 on the high end).

You can use the technique to simulate the rolling of a dice (die):

val r = (1..6).shuffled().first()

Or simulate flipping a coin (0=heads, 1=tails, or vice versa):

val r = (1..2).shuffled().first()

This also works for the coin flip simulation:

Kotlin collections methods: examples and syntax alvin November 14, 2018 - 10:23am

As a quick note today, if you ever need some examples of how the Kotlin collections methods work, I hope these examples are helpful.

Sample data

First, here’s some sample data:

val a = listOf(10, 20, 30, 40, 10)
val names = listOf("joel", "ed", "chris", "maurice")

The Kotlin forEach println syntax

It’s a little hard to move back and forth between Scala and Kotlin because of some of the differences between the languages. Skipping the long story, here’s an example of how to print every line in a list of strings in Kotlin using forEach and println. First the setup:

import java.io.File
fun readFile(filename: String): List<String> = File(filename).readLines()
val lines = readFile("/etc/passwd")

Then here are two different ways to use forEach with println: