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?

Differences between US and UK Amateur Radio License rules and restrictions

I currently hold a US Amateur Extra license (my callsign is KK6DCT) which in the UK would be equivalent to the Full license. I’m curious what the similarities and difference are between US licenses and UK licenses so decided to take a look. This is just a casual observation so please do not refer to anything here as fact, I may and probably do have some things wrong.

In the US the 3 license classes are Technician, General and Extra, where Extra has full privileges. In the UK the equivalents are Foundation, Intermediate and Full. In the US each license level gives you access to additional bands and ranges within each band. Technician in summary gives you VHF and above plus part of 10m, General adds parts of the HF bands and Extra gives you full access to everything. With the UK licenses, privileges are by allowed TX power, with Foundation restricting you to 10w access to parts of most bands HF and VHF, Intermediate raises the allowed power to 50w, and Full to 400w.

For US licenses you are required to identify your station at least once every 10 minutes. For the UK license you’re required to identify ‘as frequently as is practicable during transmissions‘ but there’s no time requirement specified.

I’m curious what other differences there are – I may come back and add to this post later. If you’re aware of more differences leave a comment below.