Scala Tutorial - Learn How To Use MinBy Function With Examples
Overview
In this tutorial, we will learn how to use the minBy function with examples on collection data structures in Scala. The minBy function is applicable to both Scala's Mutable and Immutable collection data structures.
The minBy method takes a predicate function as its parameter and applies it to every element in the collection to return the smallest element.
As per the Scala documentation, the definition of the minBy method is as follows:
def minBy[B](f: (A) ⇒ B): A
The minBy method is a member of the TraversableOnce trait.
Steps
1. How to create case class to represent Donut object
The code below shows how to create a Case Class to represent a Donut object.
println("Step 1: How to create case class to represent Donut object")
case class Donut(name: String, price: Double)
2. How to create a Sequence of type Donut
The code below shows how to define a Sequence of Donut case class elements.
println("\nStep 2: How to create a Sequence of type Donut")
val donuts: Seq[Donut] = Seq(Donut("Plain Donut", 1.5), Donut("Strawberry Donut", 2.0), Donut("Glazed Donut", 2.5))
println(s"Elements of donuts = $donuts")
You should see the following output when you run your Scala application in IntelliJ:
Step 2: How to create a Sequence of type Donut
Elements of donuts = List(Donut(Plain Donut,1.5), Donut(Strawberry Donut,2.0), Donut(Glazed Donut,2.5))
3. How to find the minimum element in a sequence of case classes using the minBy function
The code below shows how to use the minBy method to find the minimum donut element in the donut sequence by using the donut's price property as the predicate.
println("\nStep 3: How to find the minimum element in a sequence of case classes using the minBy function")
println(s"Minimum element in sequence of case class of type Donut, ordered by price = ${donuts.minBy(donut => donut.price)}")
You should see the following output when you run your Scala application in IntelliJ:
Step 3: How to find the minimum element in a sequence of case classes using the minBy function
Minimum element in sequence of case class of type Donut, ordered by price = Donut(Plain Donut,1.5)
4. How to declare a value predicate function for minBy function
The code below shows how to create a Value Function which will represent the predicate function for finding the cheapest donut. Similar to the Step 3, we will use the donut's price property.
println("\nStep 4: How to declare a value predicate function for minBy function")
val donutsMinBy: (Donut) => Double = (donut) => donut.price
println(s"Value function donutMinBy = $donutsMinBy")
You should see the following output when you run your Scala application in IntelliJ:
Step 4: How to declare a value predicate function for minBy function
Value function donutMinBy = <function1>
5. How to find the minimum element using minBy function and passing through the predicate function from Step 4
With the predicate function from Step 4, we can now pass it through to the minBy method in order the find the cheapest donut in the sequence of donuts.
println("\nStep 5: How to find the minimum element using minBy function and passing through the predicate function from Step 4")
println(s"Minimum element in sequence using function from Step 3 = ${donuts.minBy(donutsMinBy)}")
You should see the following output when you run your Scala application in IntelliJ:
Step 5: How to find the minimum element using minBy function and passing through the predicate function from Step 4
Minimum element in sequence using function from Step 3 = Donut(Plain Donut,1.5)
Summary
In this tutorial, we went over the following:
- How to create case class to represent Donut object
- How to create a Sequence of type Donut
- How to find the minimum element in a sequence of case classes using the minBy function
- How to declare a value predicate function for minBy function
- How to find the minimum element using minBy function and passing through the predicate function from Step 4
Tip
Source Code
The source code is available on the allaboutscala GitHub repository.
What's Next
In the next tutorial, I will show you how to use mkString function.