# method

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:

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:

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.

## Background

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.