Showing posts with label scala function. Show all posts
Showing posts with label scala function. Show all posts

Monday, 16 May 2022

Scala Function - distinct and distinctBy

 Distinct - It creates a copy of a given collection without duplicated elements

DistinctBy - It also creates a copy given collection by keeping only one of the elements that produce the same key after applying the given anonymous function.

In below example:

if we apply isGreaterThan2 function to List(5,4,3,33,2,1,1), the keys will be generated as (true, true, true,true,false,false,false)

Thus it will only keep first element "5" which true and another element "2" which is false and rest will be ignored.


def distinctExample(): Unit = {
    val input1 = List(5,4,3,33,2,1,1)
    val distinctInput1 = input1.distinct
    println(s"distinctInput1 = $distinctInput1") // List(5, 4, 3, 33, 2, 1)

    val multiplyBy2 = ( num: Int ) => num * 2
    val distinctByInput1 = input1.distinctBy(multiplyBy2)
    println(s"distinctByInput1 = $distinctByInput1") // List(5, 4, 3, 33, 2, 1)

    val isGreaterThan2 = ( num: Int ) => num > 2
    val distinctByInput1ForGreaterThan2 = input1.distinctBy(isGreaterThan2)
    println(s"distinctByInput1ForGreaterThan2 = $distinctByInput1ForGreaterThan2") //List(5, 2)

  }



Scala Function - Diff

 Diff computes the difference  between LHS and RHS collection


 def diffExample(): Unit = {
    val input1 = List(5,4,3,2,1)
    val input2 = List(1,2,3)
    println(s"input1 diff input2 ${input1 diff input2}")//5,4
    println(s"input2 diff input1 ${input2 diff input1}") //Empty
  }

Scala Function - Count

Count defines how many elements in a given collection satisfy the input predicate.

 def countExample(): Unit = {
    def isEven = (input: Int) => (input % 2 == 0)
    val numberList = List(1,2,3,4,5,6,7,8,9,10)
    val totalEvenNumbersInList =numberList.count(isEven)
    println(s"Total Even Numbers in List = ${totalEvenNumbersInList}") //5
  }





Scala Function - Corresponds

The corresponds checks two collection indexes by index using the given predicate. 

Here isEven is a predicate that checks whether input numbers are even or not.

If this predicate returns for all the elements then corresponds will return true else false.

  def correspondsExample(): Unit = {
    def isEven = (input: Int, otherInput: Int) => (input % 2 == 0 && otherInput %2 ==0)
    val numberList = List(2,4,6,7)
    val numberList1 = List(22,24,26,28)
    val output: Boolean = numberList.corresponds(numberList1)(isEven)
    println(s"Comparing two list using corresponds - example1 : ${output}") //false

    val numberList2 = List(2,4,6)
    val numberList3 = List(32,44,88)
    val output1: Boolean = numberList2.corresponds(numberList3)(isEven)
    println(s"Comparing two list using corresponds - example1 : ${output1}") // true
  }


Saturday, 7 May 2022

Scala Function - Compose

Scala compose creates an anonymous function which execute the function  from right hand side and pass output to left hand side function as input.

object MyBlog {
  def main(args: Array[String]): Unit = {
    composeExample()
  }

  def composeExample(): Unit = {
    def appendCharAB = (input: Int) => { println("Executing function appendCharAB"); input + "AB" }
    def appendCharXY = (input: String) => { println("Executing function appendCharAB"); input + "XY" }

    val composeAnonymousFunction: Int => String = appendCharXY compose(appendCharAB)
    val res = composeAnonymousFunction(999)
    println(res) //999ABXY
  }
}

Thursday, 5 May 2022

Scala Function - Combinations

 Scala Collection - Combinations

combinations creates the list of all the possible combinations from given collection by taking "n" number of element at a time.


object MyBlog {
  def main(args: Array[String]): Unit = {
    combinationExample()
  }

  def combinationExample(): Unit = {
    val input = List(1,2,3)
    val combinations = input.combinations(2)
    println(combinations.toList) //List(List(1, 2), List(1, 3), List(2, 3))
  }
}