Java functions and functional interfaces

I have enough knowledge of Lambdas In Java to be able to use them where useful, but under the covers there’s more to how functions are defined and referenced using the interfaces in java.util.function. Here’s a few notes digging into more details on implementing functions in Java.

java.util.function interfaces

Java interfaces used to define types for Lambda expressions and method references:

interface Function<T, R>
Defines a function which takes a parameter of type T and returns a result of type R

Single argument functions:

interface Consumer<T>
A function that takes a parameter of type T but with no return value. Provides single function void accept(T)

interface Supplier<T>
A function that takes no argument but returns a result of type T. Provides single functional method T get()

To define a function that doesn’t take any parameters or return a result can be represented by interface Java.lang.Runnable

All other interfaces in java.util,function are variations of Provider and Supplier, such as …

Bi Function interfaces

BiFunction<T, U, R>
A function that takes 2 parameters of type T and U and returns result of type R

BiConsumer<T, U>
A function that takes 2 parameters of types T and U but no return type.

Other interfaces

Boolean Predicate<T>
A function that takes a parameter of type T and returns a Boolean result. Example usage is for a filter function on Streams

BiPredicate<T, U>

<T> UnaryOperator<T>
Special case of Function that takes an argument and returns the result of the same type

<T> BinaryOperator<T>

Takes 2 parameters of the type and results a result of the same type

Method references

Method references use the :: syntax to refer to a static method that exists on a Class and pass this as a reference in place of a Lambda. For example System.out::println

To print each element of a list you could pass a Lambda calling println for each item:

list.forEach( item -> System.out.println(item)):

but using a method reference simplifies this to:

list.forEach(System.out::println);

Method references implement a functional interface but instead of implementing a new method like with a Lambda, a Method reference points to a method that already exists in a Class.

Mwthod references can be used to a static method with

Typename::staticMethodName

or to a method on an instance with

instanceName::instanceMethodName

Use every situation as an opportunity to learn

Tip for the day:

Software development is a career of continual learning. There’s always something new to learn, and always someone to learn from. Use every situation you find yourself in as a new opportunity to learn. Everything you learn now will at some point be useful to you at some point in the future, even if it’s not immediately obvious right now.

What excites prospective CS students nowadays?

When I went to tour prospective uni and poly campuses in the UK in 1989 the thing that excited me most was to tour their data center and lab rooms to see what facilities they had onsite. A lot has changed nowadays now that we rely on cloud services instead of on-prem, but as far as I know most unis still run their own on-prem data centers.

When I was visiting UK unis and polys I was excited to see labs of Sun workstations with high res 21″ monitors, Apollo workstations for CAD work, and large rooms of terminals to access the campus mini, like a Prime.

I was just browsing the University of Cambridge website for Computer Science and spent 30 minutes trying to find out what facilities they have on campus … and couldn’t find out anything about what hardware they have onsite. I know times have changed but this seemed strange to me. What excites you nowadays about joining a particular university if you are about to sign up for a CS course?

serverless framework remove error: “An error occurred: xyzRole – Cannot delete entity, must detach all policies first.”

Attempting a ‘serverless remove’ on a deployed error, I got this unexpected error I haven’t seen before:

An error occurred: xyzRole - Cannot delete entity, must detach all policies first.

A quick Google found an issue with the same error and an explanation. There is another policy attached to this Lambda that was not added via the serverless.yml for this stack, so CloudFormation is refusing to delete it. This answer describes exactly what I had done to add the new policy:

I also had added XRay to this Lambda via the Console, and this added an additional managed policy to enable Xray. Checking the IAM Role, here’s the XRay related policy that was added:

I deleted the XRay policy, but at this point serverless has already removed most of the stack but left the IAM role in place, but won’t delete the stack itself, so a manual delete from the Console completed the cleanup.