kotlin

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}"
    }
    println(hello)

}

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.

Android/Kotlin: A FragmentStatePagerAdapter (in TabLayout) example alvin December 1, 2018 - 2:48pm

As a note to self, here’s an example of implementing a FragmentStatePagerAdapter Android class in Kotlin:

Android/Kotlin: A FloatingActionButton setOnClickListener Snackbar example alvin November 30, 2018 - 4:28pm

As a quick note to self, this is an example of how to implement a setOnClickListener on a FloatingActionButton with Android and Kotlin:

fab.setOnClickListener { view ->
    Snackbar.make(
        view,
        "An exciting message!",
        Snackbar.LENGTH_LONG
    ).setAction("Action", null).show()
}

Hopefully that’s enough to get “future me” pointed in the right direction when I need this again. :)

(There are more lambda details at this link.)

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:

Pulled a Steve Jobs and canceled my own project alvin November 14, 2018 - 10:36am

I’ve been working on a Kotlin book on and off for the past few months, and this morning I pulled a Steve Jobs on myself and canceled the project, even though it’s about 75% complete (by chapter count).

The problem with the book is that at this point it doesn’t contain anything unique, although arguably my way of explaining things might be better than other approaches. Unlike the Scala Cookbook, which provides solutions to common Scala problems, and Functional Programming, Simplified, which provides a unique approach to explaining functional programming in Scala, I don’t feel like there’s anything new here.

So, in short, without getting into the details of what’s next, the “vision guy” part of me decided that there are better things to do with my time. (And if you’ve ever been on a project that was canceled and you thought it was hard to take, imagine canceling your own project.)