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
Source: stackoverflow.com
Related Query
- Compare size of 2 Arrays in Scala
- Deep compare arrays in tuples in Scala
- How to compare String Arrays with JUnit in Scala
- Compare 2 arrays in Scala Spark Dataframe
- Scala - What is the difference between size and length of a Seq?
- How do I compare two arrays in scala?
- Scala - printing arrays
- Why no immutable arrays in scala standard library?
- Scala macros and the JVM's method size limit
- Why doesn't the Scala List have a size field?
- How to compare two dataframe and print columns that are different in scala
- How do Scala parser combinators compare to Haskell's Parsec?
- Combining Arrays in Scala
- Compare json equality in Scala
- How to lexicographically compare scala tuples?
- Accessing the next element in list to compare in Scala
- Compare two Maps in Scala
- Decoding structured JSON arrays with circe in Scala
- scala mailbox size limit
- Scala Buffer: Size or Length?
- How to compare Scala function values for equality
- Specifying the size of a HashMap in Scala
- Interesting findings when combining Arrays in Scala
- Why is the Scala for-loop (and internals) NumericRange restricted to Int size and how to elaborate the functionality?
- Scala - Enforcing size of Vector at compile time
- scala dynamic multi dimensional mutable arrays like datastructures
- Element-wise sum of arrays in Scala
- Scala Arrays vs Vectors
- Match a tuple of unknown size in scala
- Scala - Pad an array to a certain size
More Query from same tag
- Discussing implementation of list flattener function in scala
- Composing and outputting two future asynchronous actions on screen
- put value from one row to another scala dataaframe
- Regex: extract last occurence of pattern
- Implementing jquery-ui in scala.js
- IntelliJ and Play framework
- Apache Spark MLLib get maximum value
- Understand Stream scala interleaved transformations behavior
- Understanding Gen.unit(x)
- Assigning each value as next item in Array in Scala
- How to use ValueMapper to change value type in Kafka Streams 10.2 using Scala
- stop all async Task when they fails over threshold?
- Distributed DDD entities with Akka
- Why scala cannot resolve the T parameter
- Spark: RuntimeException: java.lang.String is not a valid external type for schema of date
- Converting Dataset[Array[String]] to Dataset[MyCaseClass]
- Mahout spark-shell Initial job has not accepted any resources
- How to use apache commons (or any other library not part of the jdk) from Scala
- scala range split missing the last one
- How to convert Java Collection[Int] to Array[Int] in Scala
- How to define a default Action for any http method in Play 2.1 (Scala)?
- Map key not found error despite using option classes
- Reconstructing a pattern match so that I can call methods on it?
- Spark MLLib exception LDA.class compiled against incompatible version
- Creating a HashMap from a file
- Cannot access Spark dataframe methods
- Add new column in the window function in spark
- Why don't specs2 + scalamock tests run in IntelliJ? Multiple suite traits detected
- Translating this monadic counter from Haskell to Scala
- How to fetch records from the database using Play with Scala and Slick