Scala generic function

In this article let’s discuss about Scala generic function. Let’s go through the following methods without any delay 🙂

Method 1:

def listOfDuplicates[A](x: A, length: Int): List[A] = {
  if (length < 1)
    Nil
  else
    x :: listOfDuplicates(x, length - 1)
}
println(listOfDuplicates[Int](3, 4))  // List(3, 3, 3, 3)
println(listOfDuplicates("La", 8))  // List(La, La, La, La, La, La, La, La)

Hope the above methods works for you. Happy Learning and get back to us Anytime.

Realted Posts : Scala hello world