I have an Array[Any]
from Java JPA containing (two in this case, but consider any a small number of) differently-typed things. I would like to represent these as tuples instead.
I have some quick and dirty conversion code, and wondered how it could be improved and perhaps made more generic.
val pair = query.getSingleOrNone // returns Option[Any] (actually a Java array) pair collect { case array: Array[Any] => (array(0).asInstanceOf[MyClass1], array(1).asInstanceOf[MyClass2]) }
To convert a tuple of lists into an array, use the np. asarray() function and then flatten the array using the flatten() method to convert it into a one-dimensional array.
You can convert a list into a tuple simply by passing to the tuple function.
To convert a list of lists to a list of tuples: Pass the tuple() class and the list of lists to the map() function. The map() function will pass each nested list to the tuple() class. The new list will only contain tuple objects.
How about this?
val pair = query.getSingleOrNone pair collect { case Array(x: MyClass1, y: MyClass2, _*) => (x,y) } // result would be Option[(MyClass1, MyClass2)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With