How to use partially applied functions in Scala alvin March 7, 2018 - 8:16pm

Problem: You want to eliminate repetitively passing variables into a Scala function by (a) passing common variables into the function to (b) create a new function that’s pre-loaded with those values, and then (c) use the new function, passing it only the unique variables it needs.

Solution: The classic example of a partially applied function begins with a simple sum function:

How to define Scala methods that take complex functions as parameters (syntax) alvin March 7, 2018 - 8:14pm

Problem: You want to define a Scala method that takes a function as a parameter, and that function may have one or more input parameters, and may also return a value.

Solution: Following the approach described in the previous recipe, define a method that takes a function as a parameter. Specify the function signature you expect to receive, and then execute that function inside the body of the method.

Passing an anonymous function to SwingUtilities.invokeLater (in Scala)

As a quick note, if you ever need to call the invokeLater of the Java SwingUtilities class in Scala, you can pass it an anonymous function (lambda) like this:

SwingUtilities.invokeLater(() => {
    // your gui-updating code here, such as:
    for (c <- newsControllers) c.updateContent()

(I haven’t worked with Java 8 lambdas too much, but I assume that the Java lambda syntax is similar to that.)

The Java 8 lambda Thread and Runnable syntax and examples alvin August 19, 2017 - 12:55pm

As a quick note, here are some examples of the Java 8 lambda Thread and Runnable syntax.

Java 8 Thread/Runnable lambda syntax

First, here’s the Java 8 lambda syntax for a Runnable, where I create a Runnable and pass it to a Thread:

This is a page from my book, Functional Programming, Simplified

The “Great FP Terminology Barrier”

“They say no ship can survive this.”

Hikaru Sulu, talking about “The Great Barrier”
(Star Trek V: The Final Frontier)

A short excursion to ... The Twilight Zone

Hello, Rod Serling of The Twilight Zone here. Al will be back shortly, but for now, let me take you to another place and time ... an alternate universe ...

This is a page from my book, Functional Programming, Simplified

What is This “Lambda” You Speak Of?

“It takes a wise man to learn from his mistakes, but an even wiser man to learn from others.”

~ Zen Proverb


Once you get into FP, you’ll quickly start hearing the terms “lambda” and “lambda calculus.” The goal of this chapter is to provide background information on where those names come from, and what they mean.

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));

A real-world functional programming example in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 9.9, “A real-world functional programming example in Scala.”


Understanding functional programming concepts is one thing; putting them into practice in a real project is another. You’d like to see a real example of functional programming in Scala.