value

A Java method to round a float value to the nearest one-half value alvin April 24, 2018 - 10:43am

As a quick note, here’s a Java method that will round a float to the nearest half value, such as 1.0, 1.5, 2.0, 2.5, etc.:

/**
 * converts as follows:
 * 1.1  -> 1.0
 * 1.3  -> 1.5
 * 2.1  -> 2.0
 * 2.25 -> 2.5
 */
public static float roundToHalf(float f) {
    return Math.round(f * 2) / 2.0f;
}

The comments show how this function converts the example float values to their nearest half value, so I won’t add any more details here. I don’t remember the origin of this algorithm — I just found it in some old code, thought it was clever, and thought I’d share it here.

The meaning of “intrinsic value” in investing alvin February 1, 2018 - 11:33am

Here are a few definitions of the term “intrinsic value” in investing.

investopedia describes it as:

The intrinsic value is the actual value of a company or an asset based on an underlying perception of its true value including all aspects of the business, in terms of both tangible and intangible factors. This value may or may not be the same as the current market value. Additionally, intrinsic value is primarily used in options pricing to indicate the amount an option is in the money.

Functions Are Variables, Too alvin May 28, 2017 - 6:40pm

“A variable is a named entity that refers to an object. A variable is either a val or a var. Both vals and vars must be initialized when defined, but only vars can be later reassigned to refer to a different object.”

The Scala Glossary

How to iterate (loop) over the elements in a Map in Java 8

If you need to iterate over the elements in a Map in Java 8, this source code shows how to do it:

Map<String, String> map = new HashMap<String, String>();
map.put("first_name", "Alvin");
map.put("last_name",  "Alexander");

// java 8
map.forEach((k,v)->System.out.println("key: " + k + ", value: " + v));

How to write a Scala function that returns multiple values

As a quick note today, if you want to write a Scala function that returns multiple values, just return the values inside a tuple. For example, I just wrote a function to return radio station number and name, and the last part of that function looks like this:

def getRadioStationInfo(...) = {
   ...
   (104.3, "The Fan")
}

The two values are returned in an instance of a Scala Tuple2 class.

Scala programming best practice: Prefer immutable variables (values)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 20.2, “Scala programming best practice: Prefer immutable variables (values).”

Back to top

Problem

You want to reduce the use of mutable objects and data structures in your code.

Back to top

Solution

Begin with this simple philosophy, stated in the book, Programming in Scala:

“Prefer vals, immutable objects, and methods without side effects. Reach for them first.”

Table of Contents

  1. Problem
  2. Solution
Back to top
How to use functions as variables (values) in Scala alvin June 14, 2015 - 6:45pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 9.2, “How to use functions as variables (values) in Scala.”

Back to top

Problem

You want to pass a Scala function around like a variable, just like you pass String, Int, and other variables around in an object-oriented programming language.

Back to top

Solution

Use the syntax shown in Recipe 9.1 to define a function literal, and then assign that literal to a variable.

Table of Contents

  1. Problem
  2. Solution
Back to top