How can I overwrite the values of a tuple in scala? [duplicate] - scala

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.

Related

assign None to option when element is empty [duplicate]

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)

is tuple a value type or reference type in Swift? [duplicate]

This question already has answers here:
Is there a way to determine if a variable passed in is reference type or value type?
(2 answers)
Closed 5 years ago.
I know that in Swift struct and enum are value types, classes and functions are reference type. But what about tuple types? I searched Apple's book Swift programming language 3.0 but couldn't find an answer.
A simple test demonstrates that tuples are value types:
var tuple1 = (1, 2)
var tuple2 = tuple1
tuple1.0 = 3
print("t1 = \(tuple1), t2 = \(tuple2)")
Output:
t1 = (3, 2), t2 = (1, 2)
If tuples were reference types, tuple2 would have been changed.

How can I get all the indexes of a Particular String in String vector in scala [duplicate]

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)

will 'val' produce mutable data? [duplicate]

This question already has answers here:
Why is it possible to declare variable with same name in the REPL?
(2 answers)
Closed 7 years ago.
In REPL when we type the below command
scala> val p = 1 << 1
p: Int = 2
again
scala> val p = 1 << 2
p: Int = 4
my question is , I read that val is immutable . but in this case the value is changing right . Well can someone tell me why . is this really an example of mutatating . Please help
This behaviour appears in REPL only. If you try to define val twice in Scala code you'll get compilation error. In REPL second definition of val just shadows previous value of p
yep, as nyavro said, in the REPL you can override vals. Just think that in IDE if you make a mistake typing a value you can fix, in the REPL how would you fix? you would need to close the session?

Idiomatic way of extracting lists from a list of tuples [duplicate]

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