One thing Scala: Six — Java function package equivalents

Learn
1 min readJun 16, 2021

--

Java has a function package with a standard set of functional interfaces.
It is easy to define each of these in Scala as well. Examples of some of these as below.

Predicate

def greaterThanTwo: Int => Boolean = (x : Int) => x > 2
println(greaterThanTwo(3))

Supplier

def booleanSupplier: Unit => Boolean = _ => true
println(booleanSupplier())

Consumer

def stringConsumer : String => Unit
= (s : String) => println(“Consumed “ + s)
stringConsumer(“ the burger word”)

Binary Operator

def sum : (Int , Int) => Int = (x : Int, y : Int) => x + y
println(sum(10,20))

Function

def physical : ( Int , Double) => String = ( age : Int, weight : Double ) => s”Aged ${age} weighting ${weight} pounds”
println(physical(35, 102))

Other specialized variants of the above types are present for Double type, Int type etc are just as easily definable in Scala.

--

--

Learn
Learn

Written by Learn

On a continuing learning journey..

No responses yet