[Solved] To check whether my list fulfils the parameters set out in nested loop in scala function


You can use pattern matching to determine which type of Array you’re dealing with.

def arrTest[A](a :Array[A]) :Boolean = a match {
  case sa :Array[String] =>
    sa.length < 2 || sa.sliding(2).forall(x => x(0) < x(1))
  case ia :Array[Int] =>
    ia.length > 2 &&
      ia(0) == 1 && ia(1) == 1 &&
      ia.sliding(3).forall(x => x(0) + x(1) == x(2))
  case _ => a.length == 3
}

usage:

arrTest(Array("John","Margaery"))          //true  (Strings in order)
arrTest(Array("John", "Basquiat", "Yoda")) //false (Strings out of order)
arrTest(Array(1,1,2,3,5,8,13))             //true  (Fibonacci Ints)
arrTest(Array('z', 'k', 'b'))              //true  (3 Chars, any order)
arrTest(Array(3.1, 4.4))                   //false (2 Doubles)

solved To check whether my list fulfils the parameters set out in nested loop in scala function