A Dart string capitalize function alvin October 24, 2019 - 4:03pm

I was surprised to learn that Dart doesn’t have a string capitalize method, so if you ever need a capitalize method/function, here’s a start towards a solution:

String capitalize(String s) => s[0].toUpperCase() + s.substring(1);

That function will work with Dart strings like these:

A Dart function to get the current date/time in a “seconds since the epoch” format

As a brief note, if you need a Dart function to get the current date/time in a “seconds since the epoch” format, I can confirm that this function works:

/// the current time, in “seconds since the epoch”
static int currentTimeInSeconds() {
    var ms = (new;
    return (ms / 1000).round();
Flutter: A few ways to simulate slow-responding functions and methods alvin September 21, 2019 - 2:57pm

As a quick note, here are a couple of examples of how to simulate a slow-responding Flutter method:

Scala style: Side-effect methods with no parameters should be declared with parentheses

As a note about Scala style, this Scala page strongly encourages that side-effect methods that takes no parameters should be declared with parentheses:

“However, this syntax (leaving off parentheses) should only be used when the method in question has no side-effects (purely-functional). In other words, it would be acceptable to omit parentheses when calling queue.size, but not when calling println() (or openGarageDoor()). This convention mirrors the method declaration convention given above.”

“Religiously observing this convention will dramatically improve code readability and will make it much easier to understand at a glance the most basic operation of any given method. Resist the urge to omit parentheses simply to save two characters!”

Explaining Scala’s `val` function syntax alvin July 23, 2019 - 7:41pm

This is an excerpt from my book on Functional Programming in Scala. It’s an appendix that “explains and explores” Scala’s function syntax.


I wrote in the “Functions are Values” lesson that most developers prefer to use the def syntax to define methods — as opposed to writing functions using val — because they find the method syntax easier to read than the function syntax. When you write methods, you let the compiler convert them into functions with its built-in “Eta Expansion” capability. There’s nothing wrong with this. Speaking as someone who used Java for 15+ years, the def syntax was easier for me to read at first, and I still use it a lot.

A Java method that returns a random boolean value based on a probability alvin June 30, 2019 - 8:14pm

If you ever need a Java method that returns a boolean value based on a given probability, I can confirm that this method works:

 * `probability` should be given as a percentage, such as
 * 10.0 (10.0%) or 25.5 (25.5%). As an example, if `probability` 
 * is 60% (60.0), 100 calls to this function should return ~60 
 * `true` values.
 * (Note that Math.random returns a value >= 0.0 and < 1.0.)
static boolean getRandomBoolean(float probability) {
    double randomValue = Math.random()*100;  //0.0 to 99.9
    return randomValue <= probability;

Java: A Java list `tail` function (for ArrayList, LinkedList, etc.)

As a brief note today, I was working on a Java/Android application recently, and I needed a “tail” function when I was working on a Java list. What I mean by that is that Scala has a tail function that returns all elements of the list except for the head element, like this:

scala> val x = List(1,2,3,4)
x: List[Int] = List(1, 2, 3, 4)

scala> x.tail
res1: List[Int] = List(2, 3, 4)  //head element removed

and I wanted the same thing in Java.