score:0

scala> val a = Array(1,2,3)
a:Array[Int] = Array(1,2,3)

scala> val b = Array(1,2,3,4)
b:Array[Int] = Array(1,2,3,4)

scala> a.size min b.size
res0: Int = 3

The size method gets the size of the Array and min is a comparator function between the two sizes. As with any function taking two parameters, you can call it by putting the function name between the parameters.

min is doing an implicit conversion (a set of methods that Scala tries to apply when it encounters an object of the wrong type being used) from Int to RichInt.

score:0

A more generic approach, assuming you want to compare Sequences of the same type.

 def getSmallerCollectionSize[T](a:Seq[T],b:Seq[T]) =
      Math.min(a.size, b.size)

score:3

scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)

scala> val b = Array(1,2,3,4)
b: Array[Int] = Array(1, 2, 3, 4)

scala> math.min(a.length, b.length)
res0: Int = 3

More Query from same tag