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
  }
}

No comments:

Post a Comment