Thursday 5 May 2022

Scala Function - Chain

 Scala Collection - Chain:

Scala chain execute multiple function in sequence while taking same input type data as input.


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

  def chainExample() : Unit = {
    def appendCharAB = (input: String) => input + "AB"
    def appendCharXY = (input: String) => input + "XY"
    val res = Function.chain(List(appendCharAB, appendCharXY))("Prashant")
    println(s"Res = ${res}" ) //PrashantABXY

    def appendCharAB1 = (input: Int) => input + "AB1"
    def appendCharXY1 = (input: String) => input + "XY1"
    /*
      Below is not allowed as function appendCharAB1 should accept string not int
      val res1 = Function.chain(List(appendCharAB1, appendCharXY1))("Prashant")
      This is okay in andThen
    */
  }

}





No comments:

Post a Comment