This question already has answers here:
Convert a List into an Option if it is populated
(5 answers)
Closed 3 years ago.
I have a list of Integer which I need to convert to Option, however when List is empty i need to assign it as none.
please suggest
val list: List[Int] = List()
val x = option(list)
if list is empty x should be none and if list has some value say List(1,2) then x should be Some(List(1,2))
please suggest
Option(list).filter(_.nonEmpty)
Related
This question already has answers here:
Are there any methods included in Scala to convert tuples to lists?
(2 answers)
Closed 3 years ago.
I get the counts of dataframe columns from spark to scala variable as below
scala> col_counts
res38: (Long, Long, Long) = (3,3,0)
scala>
Now, I want to convert this to Array(3,3,0). I'm doing a roundabout way like
scala> col_counts.toString.replaceAll("""\)|\(""","").split(",")
res47: Array[String] = Array(3, 3, 0)
scala>
But it looks ugly. Is there an elegant way of getting it? I'm looking for a generic solution to convert any n - Long tuple to Array.
You can do this:
val tuple :(Long,Long,Long) = (3,3,0)
tuple.productIterator.toArray
This question already has an answer here:
Underscores in a Scala map/foreach
(1 answer)
Closed 4 years ago.
I'm trying to understand the use of map function and that underscore _ in the code below. keys is a List[String] and df is a DateFrame. I run an sample and found out listOfVal is a list of column type, but could someone help to explain how this works? What does _ mean in this case and what gets applied by map fuction? Many thanks
val listOfVal = keys.map(df(_))
ps: I've read the two questions suggested but I think they are different use cases
In Scala, _ can act as a place-holder for an anonymous function. For example:
List("A", "B", "C").map(_.toLowerCase)
// `_.toLowerCase` represents anonymous function `x => x.toLowerCase`
// res1: List[String] = List(a, b, c)
List(1, 2, 3, 4, 5).foreach(print(_))
// `print(_)` represents anonymous function `x => print(x)`
// res2: 12345
In your sample code, keys.map(df(_)) is equivalent to:
keys.map(c => df(c))
Let's say your keys is a list of column names:
List[String]("col1", "col2", "col3")
Then it simply gets mapped to:
List[Column](df("col1"), df("col2"), df("col3"))
This question already has an answer here:
Find Indexes *Where*
(1 answer)
Closed 6 years ago.
I am new to scala,
How can i get all the indexs for particular string.
for example:
var taluk = List("Hyderabad", "Nampally", "Hyderabad" ,"Khairatabad")
taluk.indexOf("Hyderabad")
output is 0
But I want
output as 0,2
because there are two string match in vector.
One way to do this: zipWithIndex and then collect the indices for values matching yours:
scala> taluk.zipWithIndex.collect { case ("Hyderabad", i) => i }
res0: List[Int] = List(0, 2)
This question already has answers here:
In Scala, how can I reassign tuple values?
(2 answers)
Closed 7 years ago.
I tried to overwrite the tuple values in scala using
val item = (1,'A',1);
item._1=2;
But I got an error 'reassignment to value'. Then I used stackoverflow "In Scala, how can I reassign tuple values?" and found the following solution:
val item = (1,'A',1);
item = item.copy(_1,2);
But I am getting the same error 'reassignment to value'. I tried with both val and var keyword.
A tuple in Scala is immutable, i.e. you can't change it values.
The second version goes in the correct direction, but you should use it like this:
var item = (1, 'A',1);
item = item.copy(_1 = 2);
I.e. you make it a var not a val so you can reassign it.
This question already has answers here:
Scala: How to convert tuple elements to lists
(5 answers)
Closed 8 years ago.
Is there a concise way of performing the following mapping in Scala?
val listOfTuples: List[Tuple2[Foo, Bar]] = ???
val (foos, bars) = listOfTuples // foo:List[Foo], bar:List[Bar]
I have seen others map the List[Tuple[X, X]] onto a List[List[X]] and then transpose the list, although this only works with tuples composed from homogeneous type parameters.
You should use method unzip like this:
val (foos, bars) = listOfTuples.unzip
There is also a method unzip3 for collections of Tuple3.
And if for arity 3 to 22 you could do this with product-collections:
val foos = listOfTuples._1
val bars = listOfTuples._2