Scala foldLeft and foldRight functions

In this post, I am going to give some examples which explain how to use foldLeft and foldRight functions.

def foldLeft[B](z: B)(op: (B, A) ⇒ B): B – Applies a binary operator to a start value and all elements of this sequence, going left to right.

def foldRight[B](z: B)(op: (A, B) ⇒ B): B – Applies a binary operator to all elements of this list and a start value, going right to left.

Assume that you have a list and you want to add/multiply all the elements. We can use foldLeft function to do that. The order of traversing a list is from left to right. We can specify the initial value as well. Refer the below example.


object ScalaEx {
  def main(args: Array[String]): Unit = {

    val list = List(10, 10, 5)
    val total = list.foldLeft(1) {
      _ * _
    }
    println(s"Total::$total")

    val names = List("name1", "name2", "name3")
    val allNames = names.foldLeft("")((x, y) => x + " " + y)
    println(s"allNames::$allNames")
  }

}

The output is given below,



Total::500
allNames:: name1 name2 name3

In the above example, we have used foldLeft to multiply the value of a list and then reduce it to a single value and join a list.

The foldRight works the same way but the order of traversing a list is from right to left. Refer the below example to know it.



object ScalaEx {
  def main(args: Array[String]): Unit = {

    val names = List("name1", "name2", "name3")
    val allNamesFR = names.foldRight("")( (elem, acc) => acc + " " + elem )
    println(s"allNames with foldRight::$allNamesFR")

    val allNamesFL = names.foldLeft("")( (acc, elem) => acc + " " + elem )
    println(s"allNames with foldLeft::$allNamesFL")

  }

}


The output is given below,


allNames with foldRight:: name3 name2 name1
allNames with foldLeft:: name1 name2 name3


Advertisements

Scala List

In this post, we are going to see how we can merge two lists, append/prepend an element to a list.

We have various functions available to play with Scala List.

1. ::: or ++ operator to join two lists.
2. :+ and +: operators are used for append/prepend a value to a list(This will return a new list instance)


object Scala {

  def main(args: Array[String]): Unit = {

    //With ::: operator
    val list1 = List(1, 2, 3)
    val list2 = List(4, 5, 6)

    val joinList1 = list1 ::: list2
    println("joinList1::" + joinList1)


    //With ++ operator
    val joinList2 = list1 ++ list2
    println("joinList2::" + joinList2)


    //To append a value to an existing list use :+
    val appendList = list2 :+ 7
    println("appendList::" + appendList)


    //To prepend a value to an existing list
    val prependList =  3 +: list2
    println("prependList::" + prependList)

  }

}


The output is given below,


joinList1::List(1, 2, 3, 4, 5, 6)
joinList2::List(1, 2, 3, 4, 5, 6)
appendList::List(4, 5, 6, 7)
prependList::List(3, 4, 5, 6)

Scala zip/unzip function

Zip function is used to aggregate the contents of two lists into a single list of pairs.

Consider that, we have two lists one contain the list of tasks and another contains list of person names. We want to create another list contains person and task pair. Let see how we can use zip function to do this.

Refer the below example,


object ScalaMain {

  def main(args: Array[String]): Unit = {

    val taskList = List("task1", "task2", "task3")
    val personList = List("Peter", "John", "Patrick")

    val personWithTaskList = personList zip taskList

    personWithTaskList.foreach {
      case (person, task) =>
        println(s"$task is assigned to $person")
    }
  }
}


The output is given below,


task1 is assigned to Peter
task2 is assigned to John
task3 is assigned to Patrick

Unzip function will work as reverse of zip. Refer the below code and its output.



    val unzippedList = personWithTaskList unzip

    println(s"list of persons:${unzippedList._1}")
    println(s"list of tasks:${unzippedList._2}")


Output:


list of persons:List(Peter, John, Patrick)
list of tasks:List(task1, task2, task3)

As the “zip” function creates a pair, it will omit all the unpaired elements. Refer the below example,


object ScalaMain {

  def main(args: Array[String]): Unit = {

    val taskList = List("task1", "task2", "task3")
    val personList = List("Peter", "John", "Patrick", "Joe")

    val personWithTaskList = personList zip taskList

    personWithTaskList.foreach {
      case (person, task) =>
        println(s"$task is assigned to $person")
    }
  }

}


The output of the above program is given below,


task1 is assigned to Peter
task2 is assigned to John
task3 is assigned to Patrick


Scala Implicit Function – How to use

In this post, we are going to see how to use Scala Implicit function.

Assume we have a case class “Employee” class which contains name, age, role and location fields and we have a list of Employee case class objects. Lets see how to use Implicit function to find out the employee details by age and also by location.



case class Employee(name: String, age: Int, role: String, location: String)

class EmployeeFunction(employees: List[Employee]) {

    def findEmployeesByRole(role: String): List[Employee] ={
        employees
          .filter(e  => e.role == role)
    }

    def findEmployeesByLocation(location: String): List[Employee] ={
        employees
            .filter(e  => e.location == location)
    }

}

object EmployeeFunction {
    implicit def employeeFunction(employees: List[Employee]):EmployeeFunction = new EmployeeFunction(employees)
}


In the above code, The “EmployeeFunction” contains an implicit function “employeeFunction” defined with “implicit” keyword. So the employee list is implicitly passed to this function.

Now, to use this function in other file, we have to import this function.
Here in the below code, we import the function and then create a list of employee objects and then call “findEmployeesByRole” and “findEmployeesByLocation” methods on it.

Even though the methods findEmployeesByRole” and “findEmployeesByLocation” are not available on the List, it will call the “EmployeeFunction” class methods because of the implicit function.



import EmployeeFunction.employeeFunction

object EmployeeMain {

    def main(args: Array[String]): Unit = {

        val employees = List(Employee(name = "name0", age = 34, role = "programmer", location = "USA"),
                             Employee(name = "name1", age = 34, role = "manager", location = "USA"),
                             Employee(name = "name2", age = 34, role = "analyst", location = "USA"),
                             Employee(name = "name3", age = 33, role = "manager", location = "Canada"),
                             Employee(name = "name4", age = 34, role = "tech lead", location = "India"),
                             Employee(name = "name5", age = 37, role = "manager", location = "India"))

        println("All USA employees are\n" + employees.findEmployeesByLocation("USA"))
        println("All managers are:\n"+employees.findEmployeesByRole("manager"))

    }

}


Refer below the output of the above program



All USA employees are
List(Employee(name0,34,programmer,USA), Employee(name1,34,manager,USA), Employee(name2,34,analyst,USA))
All managers are:
List(Employee(name1,34,manager,USA), Employee(name3,33,manager,Canada), Employee(name5,37,manager,India))


Scala Try

The Try type in scala represents a computation that may either result in an exception, or return a successfully computed value.

In this post, We see how we can use Try type with an example.

Assume that we have a list of string and want to convert each value into number and If the value is not valid, then we have to throw an exception. We also do not want to break the program when an exception has been thrown.

Refer the below example to know how we can do that with Try type.



import scala.util.{Failure, Try}

object ScalaTryCatchExample {

    def main(args: Array[String]): Unit = {

        val values = List("1", "2", "3", "test", "5")

        values.foreach(value => {

            val tryOption = Try(convertValuesAsNumber(value))
            tryOption match {
                case Failure(e) =>
                    println(s" $value is not valid number. Exception is $e")
                case _ =>
            }
            println("Number:" + tryOption.toOption)
        }
        )

    }

    def convertValuesAsNumber(value: String): Integer = {
        if (value == null || !value.matches("[0-9]")) {
            throw new IllegalArgumentException()
        }
        Integer.valueOf(value)
    }
}


In the above Scala Object, we have “convertValuesAsNumber” method which is used to convert a string value into a number and also it throws an exception when if the value is null and not a valid number.

Here we wrap the convertValuesAsNumber method call into Try type. So when an exception throws, it would not break the program immediately. Instead the failure object is returned. So we can use match to find out this and finally call toOption method to get the value. If any exception throws then it will return a ‘None’ option.

We can also re-write the above program like below,


import scala.util.{Failure, Try}

object ScalaTryCatchExample {

    def main(args: Array[String]): Unit = {

        val values = List("1", "2", "3", "test", "5")

        values.foreach(value => {

            val tryOption = Try(Integer.valueOf(value))
            tryOption match {
                case Failure(e) =>
                    println(s" $value is not valid number. Exception is $e")
                case _ =>
            }
            println("Number:" + tryOption.toOption)
        }
        )

    }
}


The output of the above example is given below


Number:Some(1)
Number:Some(2)
Number:Some(3)
 test is not valid number. Exception is java.lang.IllegalArgumentException
Number:None
Number:Some(5)

How to use scopt(https://github.com/scopt/scopt)

In this post, we are going to see how we can use scopt which is a scala library
used to parse the command line options.

The github URL is https://github.com/scopt/scopt

Lot of time, We may need to read and parse the command line arguments. So this simple command line parser used to reduce our burden. Let see how we can use this with a simple example.

Assume that you want to read two parameters from command line, they are input and output directory respectively. You want to throw an error if those parameters are not available.



package com

object ScalaApp extends App {

    case class Arguments(inputDir: String = "",
                         outputDir: String = "")

    val parser = new scopt.OptionParser[Arguments]("Parsing application") {

        opt[String]('i', "inputDir").
            required().valueName("").action((value, arguments) => arguments.copy(inputDir = value))

        opt[String]('o', "outputDir").
            required().valueName("").action((value, arguments) => arguments.copy(outputDir = value))

    }

    def run(arguments: Arguments): Unit = {
        println("Input Dir:" + arguments.inputDir)
        println("Output Dir:" + arguments.outputDir)
    }

    parser.parse(args, Arguments()) match {
        case Some(arguments) => run(arguments)
        case None =>
    }

}


In the above example, I have created a case class Arguments has two fields, input and output directory and I parse the input arguments, if the value is available, then I assign the value to the corresponding field in the case class.

When i run the program with the below command line arguments, then the output will look below,

-i /home/bala/input -o /home/bala/output


com.ScalaApp -i /home/bala/input -o /home/bala/output
Input Dir:/home/bala/input
Output Dir:/home/bala/output

When i run the program without any command line arguments, then it will show the below error as i specified those arguments as required arguments.



com.ScalaApp

Error: Missing option --inputDir
Error: Missing option --outputDir
Usage: test [options]

  -i, --inputDir    
  -o, --outputDir   


Scala Partial Function

A partial function is a function that is valid for only a subset of values of those types you might pass in to it.

Partial function can be defined by using ‘case’ statement.

Let us define a partial function and to know how to use it.


  val multiplyBy100: PartialFunction[Int, Int] = {
    case x if x > 0 => x * 100
  }

The above partial function gets an integer value as input and process it and returns another integer value. Here, we check the value is negative or not before applying multiple operation.

Consider that we have a list of numbers and want to multiply each value by value 100, then we can make use of the above function.

Partial functions can help us to get rid of any side effects for example avoiding the negative number in list.

Refer the below complete code and its output.



object ScalaExample {

  //Partial function: MUltiply the input by 100 and return it.
  val multiplyBy100: PartialFunction[Int, Int] = {
    case x if x > 0 => x * 100
  }

  def main(args: Array[String]): Unit = {

    //List of numbers
    val numbers1 = List(1, 4, 5, 6, 7)
    //Use the partial function and coll
    println("numbers1::" + (numbers1 collect multiplyBy100))

    //List contains negative numbers as well
    val numbers2 = List(-1, -2, -3, 10)
    println("numbers2::" + (numbers2 collect multiplyBy100))

  }
}



numbers1::List(100, 400, 500, 600, 700)
numbers2::List(1000)


Scala’s PartialFunction trait contains the isDefinedAt method which can be called to check whether it handles the given value.

For example, We can call like this multiplyBy100.isDefinedAt(-1) which returns false. whereas this one multiplyBy100.isDefinedAt(1) returns true.