anonymous function

Kotlin sortedWith syntax and lambda examples

As a quick note, here’s an example of how to use the Kotlin sortedWith syntax with an anonymous function (lambda). Given this list of integers:

val list = listOf(7,3,5,9,1,3)

Here’s an example of how to use sortedWith using a Comparator and lambda:

Kotlin sortedBy syntax and examples

As a quick note, here’s an example of the Kotlin sortedBy syntax. Given this list of strings:

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

the comments after these examples show how the Kotlin sortedBy function works:

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

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 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
How to use function literals (anonymous functions) in Scala alvin June 14, 2015 - 6:43pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 9.1, “How to use function literals (anonymous functions) in Scala.”

Problem

You want to use an anonymous function — also known as a function literal — so you can pass it into a method that takes a function, or to assign it to a variable.

Solution

Given this List:

An introduction to Scala’s collection classes (and methods)

This post is an excerpt from the Scala Cookbook. (This is the introduction to Chapter 10, Collections.)

The Scala collection classes are rich, deep, and differ significantly from the Java collections, all of which makes learning them a bit of a speed bump for developers coming to Scala from Java.

Scala - Using tuples in an anonymous function

I just ran across this scala-lang.org post about using tuples in an anonymous function, and thought it was good enough to reproduce here, with only the solution part.

In essence, the question is, if I have a Map like this, which is really composed of tuples: