scala compare elements in two sequences considering two arguments in element [closed] - scala

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
is there any way to get distinct elements by comparing below two sequences only considering 2nd and 3rd arguments of Person element
case class Person(i:Long,name:String,uname2:String,uname:String)
val firstSeq = Seq(Person(null,"aaa","bbb",null),Person(null,"bbb","ccc",null))
val secondSeq = Seq(Person(123456,"aaa","bbb","Bob"),Person(2345678,"ccc","bbb","John"),
Person(34567890,"bbb","ccc","Mike"))
Excepting result from firstSeq perceptive after comparsion Seq(Person(null,"bbb","ccc",null))
Excepting result from secondSeq perceptive after comparsion Seq(Person(2345678,"ccc","bbb","John"))

The question is not that clear, but this code removes elements from two lists where they share the same values of name and uname2, or where there is no corresponding value in the other list.
val (r1, r2) = firstSeq.zip(secondSeq).filterNot {
case (a, b) => a.name == b.name && a.uname2 == b.uname2
}.unzip
Also note that it is much safer to use Option[Int] and Option[String] rather using null to indicate missing values.

Related

How can i derive values list of values from list of either using flatmap for fold? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
im trying to convert a scala list of eithers, as such:
List(Left(3),Left(4),Left(1),Left(5))
Into a either of a list Either[List[Int],Int] like this?
Left(3,4,1,5)
Only using flatmap, map or fold?
ive been hammering at it for a while now and can simply not make it work
Assumed:
val a = List(Left(1), Left(2), Left(3)) // for example
Then following will return a Left[List[Int]]:
Left(a.map(_.value))
// Left(List(1, 2, 3))
Then you can extract the values out of the list, which I don't thin is generally a good idea.

scala get vs match which one to use, given that restult will always be same type? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Given that match will never be None,
Which style is better or can I improve the first?
val tmp = (cols.find(_(0) == id).get)
SomeClass(tmp(0), tmp(1))
cols.find(_(0) == id) match {
case Some(value)=> SomeClass(value(0), value(1))
case None=> NotFound("Given id not found")
}
Since this a question about style, my answer is that neither of these is the best style. Instead, just keep the value in the Option
val opt: Option[SomeClass] = cols.find(_(0) == id).map(v => SomeClass(v(0), v(1)))
Keep processing/testing inside the Option using foreach/exists etc. until you really need the bare value. There is a very rich set of methods on Option that covers most of the things that are needed.
If the rest of the code is structured well, you will likely find that the value never needs to be extracted in a separate operation.

Query on scala Collections [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Both tuples and list are accepting the different type values as attached in snippet, then what is the main difference?
Scala is a typed language. You want your program to know as much as possible about the shape of the data it is working with.
List[Any] is not a very useful type. It does not tell you that there are three elements in there, and what their respective types are.
(Int, Double, String) tells you much more.
Lists are for collections of elements of all the same type (but unknown number).
Tuples are for the combination of a fixed number of elements (that can each have their own type).
Tuples can also be seen as ad-hoc versions of case classes (which you have to define first, but then give you named fields, and methods and all that):
val myData = FooDataRecord(id = 1, amount = 1.1, name = "a")

Add a number to every element in an Array[Double] [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have the following:
var x = some array Array[Double]
val n = 3
I want to add n to every element in x, how? I tried:
x.map(v => v + n)
But that doesnt work.
You have to use a third var like this :
val x2 = x.map(_ + n)
If you don't want to construct a new Array but just want to modify the values in the existing one, then:
for (i <- x.indices)
x(i) += n
In general, the Scala collections API doesn't currently offer higher-level operations that do in-place mutation, so you have to resort to lower-level code like this. (That will change in Scala 2.13.)
You will have to write the return output of map into a variable to get the updated array.

How to best way to parse args yet make it clear and easy to understand [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm putting some test cases and having lots of parameters yet it is cumbersome and not so easy to understand putting them into a liner in scala. I rather specify in a visually acceptable manner but correctly parse them in scala...
I would use String Interpolation to define the arguments such as below
val kafkaServers = "server1:9092,server2:9092"
val pleasentArgs = s"""
--master='spark://someserver'
--metricWindowSize=50
--outputDirectory='/tmp/someGoodDirectory'
--zkQuorum=$zkQuorum
--saveToHDF=false
--userName='jack#somewhereElse.com'
--kafkaServers=$kafkaServers
"""
And have a method to clean up all the newline, tab etc characters and then generate the args as shown below:
def cleanUp(str:String) = str.split('\n').map(_.trim.filter(_ >= ' ')).mkString(" ")
def genArgs(args:String): Array[String] =cleanUp(args).split(" ").filter(!_.trim.isEmpty)
So eventually we can just call genArgs method
val args = genArgs(pleasentArgs)