tag:dreamwidth.org,2017-01-08:2740657From the Mind of Trevor Stone… blogging like it's the mid-naughtiesflwyd2020-12-27T06:24:34Ztag:dreamwidth.org,2017-01-08:2740657:396527Advent of Kotlin2020-12-27T06:24:34Z2020-12-27T06:24:34ZKGNU - Under the Floorboardsquixoticpublic4Each December for the past several years, <a href="https://adventofcode.com/">Advent of Code</a> has presented a series of 25 daily programming challenge, with each problem adding to a Christmas-themed narrative. I think I'd seen references to AoC in the past but hadn't paid it any mind. This year, my team at work is evaluating Kotlin for adoption in our Android Java codebase, so a small daily excuse to get experience with the language seemed promising. Plus, there's a global pandemic so it's not like I've got any holiday parties to attend.<br /><br />The event was more fun than I'd anticipated. Challenges are released at midnight America/New_York each night, and there's a time-to-completion leaderboard, so there's a competitive challenge aspect to get the juices flowing. This wasn't great for health, though—on a couple nights I started programming at 10pm America/Denver while already tired and didn't go to bed until 3am, whether because I was too sleep deprived to effectively debug or because I was having fun giving hints on the <a href="http://reddit.com/r/adventofcode/">contest's subreddit</a>. Mostly it was fun because the problems are small enough to do in one sitting and often involve an interesting algorithm. Lots of participants give themselves an additional challenge, like using a different programming language each day or using an unusual or challenging language—I saw someone posting solutions in the <a href="https://en.wikipedia.org/wiki/M4_(computer_language)">m4 macro language</a> and some folks using Excel. Lots of folks create visualizations of their algorithm solving the problem; this year's challenges involved several which were based on <a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life">Conway's Game of Life</a> which naturally offer interesting visualizations.<br /><br />My experience with Kotlin was a bit mixed. Kotlin is a programming language designed to run on the Java Virtual Machine and play well with Java code, but with a more expressive syntax and some features informed by two decades of programming language evolution since Java came into the world. It is perhaps most widely used in the Android ecosystem where some of its features help cover for poor Android framework design and API choices and where its coroutine concurrency model is a better fit for client application programming than Java is. Kotlin can also run in JavaScript and iOS environments, offering a hope of cross-platform shared logic. I've seen enough cross-platform efforts fail to be widely adopted to be skeptical on this front, though.<br /><br />Using Kotlin for Advent of Code offered several benefits over Java. First, the heavy type inference and lower repetition and boilerplate reduced the number of symbols that had to be typed, which is nice for short programs, particularly one with Fake Internet Points for programming quickly. The standard library provides a lot of handy utilities like ranges, a typed <code>Pair</code> class and <code>check</code>/<code>require</code> (functions which concisely throw an exception if the program is in an invalid state) for which Java needs a library like <a href="https://github.com/google/guava/wiki">Guava</a>. <code>when</code> blocks were also handy in many AoC puzzles, and a lot friendlier than a chain of if/else conditions. Kotlin's fluent collection transformations (<code>filter</code>, <code>map</code>, <code>sum</code>, and friends) feel a little more expressive than Java Streams, and I found multiple occasions where "potentially infinite sequences" were helpful. Coroutines (which power sequences) are, I think, Kotlin's biggest selling point, and while most Advent of Code problems don't particularly benefit from concurrency, I found yielding values from a recursive function easier to implement and reason about than accumulating a list that gets returned up the chain.<br /><br />I'm not entirely won over on Kotlin, though. My first gripe is that the language is at risk of falling into the C++ and Perl trap wherein the language provides multiple ways to do very similar things and two pieces of code which do the same thing look very different. This in turn can create a cognitive impediment when reading code written by a different programmer or team. One example of this is the distinction between properties and no-arg methods. In Kotlin, one writes <code>list.size</code> as a property but <code>list.isEmpty()</code> as a method and I've been unable to find guidance on when to use one rather than the other for read-only state.<br /><br />Second, one of Kotlin's selling points is nicer handling of nulls, since nullability is part of a type definition (<code>String?</code> is nullable, <code>String</code> is not). This is handy, and reduces boilerplate, particularly with null-happy APIs like Android. But it also means the compiler forces you to handle null cases which you know semantically can't occur, such as calling <code>.max()</code> on a collection that you know is not empty. This leads to a proliferation of method name pairs, one of which throws an exception and one of which returns null (elementAt/elementAtOrNull/elementAtOrDefault, getValue/get/getOrDefault, maxBy/maxByOrNull, maxWith/maxWithOrNull…). This also isn't entirely consistent within the standard library: <code>list[5]</code> throws an exception if the list has fewer than six elements, but <code>map[5]</code> returns null if that key is not present. The need for "OrDefault" method variants also seems a bit odd when the language also provides the Elvis operator (<code>?:</code>) for null-coalescing.<br /><br />Third, the impression that Kotlin is basically Java with nicer syntax can lead to unpleasant surprises when the Kotlin standard library has a slightly different implementation to a similar method in Java. For example, in Java, String.split with an empty argument returns an array with one character per string: <code>"cake".split("")</code> is the same as <code>new String[] {"c", "a", "k", "e"}</code>. The same behavior holds true in JavaScript, Python, Perl, and perhaps dates back to <a href="https://en.wikipedia.org/wiki/AWK">AWK</a>. Kotlin, on the other hand, returns an array with empty strings at the beginning and end: <code>"cake".split("")</code> is the same as <code>arrayOf("", "c", "a", "k", "e", "")</code>. What's worse, the behavior of splitting on an empty string or pattern is not documented in Kotlin, so I don't know if it's a bug or an intentional choice.<br /><br />This brings up another of my Kotlin complaints: documentation. There are plenty of valid complaints about Java's verbosity, but the clarity and completeness of Javadoc in the Java world is wonderful. I very rarely have to read the code in the JDK or a widely-used library to understand how it will handle a particular input. (The same cannot be said for Ruby, for example.) Kotlin seems to prefer more terse documentation and rarely gives sample code, so you're often left to figure it out yourself, experimentally. The Kotlin web interface for API documentation also has some notable room for improvement, like proper handling of "Open in new tab" clicks.<br /><br />My final Kotlin complaint that cropped up during Advent of Code is a sneaky one. One of Kotlin's neat features is extension methods: you can define a method on a type defined by someone else, like <code>operator fun Pair<Int, Int>.plus(other: Pair<Int, Int>) = Pair(first + other.first, second + other.second)</code>. This can help the readability of code with several steps by chaining all method calls from top to bottom, whereas Java would end up with a mix of static method calls wrapped around method chains. This feature, however, comes with a major downside: extension methods are resolved statically against the declared type of the receiver. They are <em>not</em> dispatched dynamically, despite having identical syntax as dynamically dispatched methods. A concrete example I ran into: a function which checks the neighboring cells of a 2-D grid used the following code:<br /><pre>fun checkNeighbors(x: Int, y: Int) {
for (i in (x-1..x+1).intersect(0 until height)) {
for (j in (y-1..y+1).intersect(0 until width)) {
// do something with grid[i][j]
}
}
}</pre><br />This expresses "go through all the cells from above left to below right while staying inside the grid bounds" by using the intersection of pairs of ranges. Unfortunately, this is an O(n^2) algorithm because <code>intersect</code> is defined as an extension method of <code>Iterable</code>, so it runs through all <var>width</var> columns for each <var>height</var> row, even though at most three of each are relevant. I could write a specialized <code>IntRange.intersect(other: IntRange) = IntRange(max(start, other.start), min(endInclusive, other.endInclusive))</code> extension method, and it would improve the complexity in this code to O(1). But if someone passed an IntRange to a method declared to take an Iterable or a ClosedRange, an <code>intersect</code> call on that argument, the inefficient generic version would be used. This contrasts with Java 8's similar mechanism, <code>default</code> methods on an interface, which allow implementations to provide a specialized version dispatched at runtime.<br /><br />Returning circularly to the "too many ways to do the same thing" problem, here are some efficient ways to write that grid code in Kotlin:<br /><pre>for (i in (x-1).coerceAtLeast(0)..(x+1).coerceAtMost(height-1)) {
for (j in (y-1).coerceAtLeast(0)..(y+1).coerceAtMost(height-1)) {
for (i in (0 until height).let { (x-1).coerceIn(it)..(x+1).coerceIn(it) }) {
for (j in (0 until width).let { (y-1).coerceIn(it)..(y+1).coerceIn(it) }) {
for (i in x-1..x+1) {
if (i in 0 until height) {
for (j in y-1..y+1) {
if (j in 0 until width) {
for (i in (x-1..x+1).filter((0 until height).contains)) {
for (j in (y-1..y+1).filter((0 until width).contains)) {</pre><br />but I'm really not sure which is the most idiomatic.<br /><br /><img src="https://www.dreamwidth.org/tools/commentcount?user=flwyd&ditemid=396527" width="30" height="12" alt="comment count unavailable" style="vertical-align: middle;"/> comments