syntax

TextMate TODO tag syntax highlighting

As a brief note to self, I like the way the TODO tag is highlighted when using TextMate, so I dug around to see how it worked so I can make other words be highlighted the same way it is. The short answer is that in TextMate, click the Bundles menu, select Edit Bundles, then scroll down to select TODO near the bottom of the list, then Language Grammars and TODO. The last few steps are shown in the image.

An example of the syntax of Intersection Types in Scala 3 (Dotty) alvin December 16, 2018 - 4:22pm

As a brief note to self, here’s an early example of the syntax of Intersection Types in Scala 3 (Dotty):

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:

Kotlin groupBy syntax and example

Here’s a quick example of how to use the Kotlin groupBy syntax on a list, with the result of each example shown in the comments:

val names = listOf("kim", "julia", "jim", "hala")

names.groupBy { it -> it.length }  //LinkedHashMap: {3=[kim, jim], 5=[julia], 4=[hala]}
names.groupBy({it}, {it.length})   //LinkedHashMap: {kim=[3], julia=[5], jim=[3], hala=[4]}
Kotlin secondary (auxiliary) class constructors (syntax, examples) alvin August 6, 2018 - 10:35am

This page is a work in progress, but if you need to create a secondary class constructor in Kotlin, I hope it’s helpful.

Scala while and do/while loops (syntax, examples)

Once I got into functional programming I generally quit using while loops and do/while loops in Scala, but the language has both of them. Here’s the general syntax:

// while loop
while(condition) {
    statement(a)
    statement(b)
}

// do-while
do {
   statement(a)
   statement(b)
} 
while(condition)

Scala while loop syntax

Here’s an example of a while loop: